Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Crossing a Swing

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Crossing a Swing

    Hi,
    i'm trying to develop a strategy where the order starts as soon the current bar touches/crosses the swing high/low. So far i only get that the order starts when a bar is actually above the swing high or below the swing low but not while crossing it.

    could you help me?
    thank you
    Giuditta

    #2
    Hello Giudiver,

    Thanks for your post and welcome to the NinjaTrader forums!

    To accomplish your goal of the strategy reacting to a touch of a price to a level, you would need to run your strategy using Calculate,OnPriceChange (or Calculate.OnEachTick) as either of those modes allows your code to run on real time data (and not once per bar at the end of the bar as is the default in most cases).
    Reference: https://ninjatrader.com/support/help...?calculate.htm

    In those calculate modes, you can use Close[0] as the current price which means you can compare Close[0] to the swing level you are interested in. For reference, Close[1] will be the close price of the previous bar.

    In either of those calculate modes you would need additional logic in your strategy so that your strategy does not place multiple orders at once. Multiple orders can happen because the code will run many times per bar and the entry conditions can be true multiple times in a short span of time, shorter than the time it takes to actually transmit the first order, for it to get filled, and then for the position to be updated.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi Paul,

      thanks so much for your help.

      It is already configured as calculate on each tick. I used the condition below

      CrossBelow (Low, Swing(Close, 5).Swing.Low, 1)

      but i get what is in the picture attached: the order starts when the bar is below the swing and it does not consider the bar the crossed the swing (see blue arrow).


      Another question: how do i configure the strategy so that i get only one order per swing (if the conditions are met)?

      thank you!

      Comment


        #4
        Hi Paul,

        i found this:
        Running a Strategy at the Close of a Bar or Tick by Tick

        •During backtest, strategies can ONLY be processed at the close of each bar.

        •During real-time operation, you have a choice to run a strategy tick by tick (Calculate set to 'On Each Tick') which can produce different results. This is because you can have a signal that executes an order at the close of a bar but when running tick by tick, while in a bar a signal condition can be true although its false at the close of the same bar.



        is it true? does it mean that this is the reason why during backtesting the order just activates when the next bar opens?

        thanks!

        Comment


          #5
          Hello Giudiver,

          Thanks for your replies.

          Correct, when backtesting the historical bars do not include all of the ticks that built the bar so the strategy can only be executed once per bar (same as Calculate.OnBarClose). When the strategy determines an entry condition has occurred then the order is placed and as it is at the end of the bar the order can only be filled on the next bar.

          If you are coding in Ninjascript, you can modify your strategy to achieve the same performance in the strategy analyzer by adding a 1 tick data series, submitting your orders to that 1 tick data series and using TickReplay. This would mean that you must have tick data for the entire duration of the back test period. Please note that this will result in a slower strategy analyzer performance.

          Alternately, you can leave your strategy as is and run your backtests using the Playback connection with Market replay data. This will provide all of the ticks and builds the bars as they are in live data.

          To limit the strategy to one order per swing, you would need to add further logic involving bool variables (either true or false variables) or something to identify the swing, for example saving the swing High or swing Low bar number. Then use a check of the saved swing bar number to the current swing bar number as an added entry condition.
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Hi Paul,

            thanks a lot for your help!

            I am almost done with my strategy, however, I just realized that, given that my other conditions are pretty aggressive, if I use either crossbelow or less than tot ticks from the swing low (or viceversa for swing high) to activate my order sometimes all the other conditions are met 5 (or more) bars after the swing therefore the condition related to the swing becomes pretty much useless.

            I am trying to set up a range of 0.5 to 1 ticks after the swing for the order to get activated and I came up with this:

            if ((Low[0] < (Swing1.SwingLow[1] + (-0.5 * TickSize)) )
            && (Low[0] > (Swing1.SwingLow[1] + (-1 * TickSize)) ))

            but i do not get any trades.

            Can you help me?

            thank you

            Comment


              #7
              Hello Giudiver,

              Thanks for your reply.

              The code example is checking to see if the Low of the current bar is both greater than the swing low - 1 tick and less than the swingLow + 0.5 tick. Please note that a tick is the smallest price movement for the instrument so a 0.5 tick would effectively be a zero value.

              Another approach would be to see if the Low[1] is greater than the swing low and the Low[0] is less than the swing low, as you would be looking on each tick this would catch the first cross without having to fit within a narrow window of +/-1 tick.

              I would suggest debugging by printing the values of Low[0] along with the swing values +/- 1 tick on a bar by bar basis. You could also add a couple of plots that would plot on the chart those conditions of Swing1.SwingLow[1] + (-1 * TickSize)).



              Paul H.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by knowmad, Today, 03:52 AM
              0 responses
              6 views
              0 likes
              Last Post knowmad
              by knowmad
               
              Started by tradingnasdaqprueba, Today, 03:42 AM
              0 responses
              5 views
              0 likes
              Last Post tradingnasdaqprueba  
              Started by Haiasi, 04-25-2024, 06:53 PM
              4 responses
              63 views
              0 likes
              Last Post effraenk  
              Started by ccbiasi, 11-23-2017, 06:06 AM
              5 responses
              2,202 views
              0 likes
              Last Post leodavis  
              Started by kujista, Yesterday, 12:39 AM
              2 responses
              14 views
              0 likes
              Last Post kujista
              by kujista
               
              Working...
              X