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

Enter when the price of current bar crosses above/below 1 bar ago

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

    Enter when the price of current bar crosses above/below 1 bar ago

    Hello,

    I am trying to have the NT8 Strategy builder enter at the high/low price of a previous bar. What is the best way to accomplish this? I Set my strategy to "on tick" and used when price on the current bar crosses above the HIGH/LOW of the previous bar along with enabling tick data, It compiles correctly but doesn't generate any signals. If I use on bar close and less equal/greater equal it works but on the close.

    Thanks!

    #2
    Hello cerebraltrader,

    Thanks for your post.

    The CrossAbove/CrossBelow methods cannot detect if a crossover happens on the currently forming bar. This means a lookback period of 0 cannot be used with CrossAbove/CrossBelow methods. You must use a lookback period of at least 1 when using these methods.

    If you would like to check if a crossover occurs on the currently forming bar, you would need to make a Greater or Less comparison of two values for your condition.

    Let me know if I may further assist.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thanks Brandon.

      Comment


        #4
        Originally posted by NinjaTrader_BrandonH View Post
        Hello cerebraltrader,

        Thanks for your post.

        The CrossAbove/CrossBelow methods cannot detect if a crossover happens on the currently forming bar. This means a lookback period of 0 cannot be used with CrossAbove/CrossBelow methods. You must use a lookback period of at least 1 when using these methods.

        If you would like to check if a crossover occurs on the currently forming bar, you would need to make a Greater or Less comparison of two values for your condition.

        Let me know if I may further assist.

        I am having trouble with the same inquiry. Does this mean that the backtest will display incorrect results? If I have one of these conditions

        > High[0] > High[1]
        or
        > GetCurrentAsk(Default input) > High[1]

        and the action for do the following is

        > EnterLong(DefaultQuantity,@"Long1")

        I still see entries based on a candle close and not immediate upon a new high printing on the current bar. I changed calculation to on price change already.

        Would you have to run the strategy in real time to see if it is taking the correct entry?

        Comment


          #5
          Hello RallySquirrel,

          Thanks for your note.

          After further investigation, I found that you are able to use CrossAbove/CrossBelow conditions on the current bar by setting your Calculate mode to Calculate.OnPriceChange.

          Note that when backtesting a NinjaScript strategy in the Strategy Analyzer, a strategy running real-time (live brokerage account, live market simulation, Playback connection etc...) will produce different results than the performance results generated during a backtest.

          When in historical data (backtesting), only the Open, High, Low, and Close will be available and there will be no intra-bar data. This means actions cannot happen intra-bar, and fills cannot happen intra-bar. All prices and actions come from and occur when the bar closes as this is all the known information.

          Because of this, OnBarUpdate will only update 'On bar close' as it does not have the intra-bar information necessary for 'On price change' or 'On each tick' and the script will not have the intra-bar information to accurately fill an order at the exact price and time.

          Below is a link to the help guide on Calculate.
          https://ninjatrader.com/support/help.../calculate.htm

          To improve the accuracy of a backtest, you may use Tick Replay along with an added 1-tick series to have logic processed intra-bar and have orders filled intrabar.

          Tick Replay would be used to have the logic process OnEachTick or OnPriceChange with historical data, but this does not allow for intra-bar order fills. You would need to add a single tick data series and submit orders to that single tick data series for a strategy that uses Tick Replay.

          Please reference the SampleIntrabarBacktest example and the following Help Guide links for more information.

          SampleIntrabarBacktest 'Backtesting NinjaScript Strategies with an intrabar granularity' - https://ninjatrader.com/support/helpGuides/nt8/backtesting_ninjascript_strate.htm

          TickReplay — https://ninjatrader.com/support/help...ick_replay.htm

          Developing for Tick Replay -
          https://ninjatrader.com/support/helpGuides/nt8/developing_for__tick_replay.htm?zoom_highlightsub= developing+for+tick+replay

          Additional information may be found in this NinjaTrader Forum post —
          https://ninjatrader.com/support/foru...mance?t=102504


          Please let us know if we may be of further assistance to you.​
          Brandon H.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_BrandonH View Post
            Hello RallySquirrel,

            Thanks for your note.

            After further investigation, I found that you are able to use CrossAbove/CrossBelow conditions on the current bar by setting your Calculate mode to Calculate.OnPriceChange.

            Note that when backtesting a NinjaScript strategy in the Strategy Analyzer, a strategy running real-time (live brokerage account, live market simulation, Playback connection etc...) will produce different results than the performance results generated during a backtest.

            When in historical data (backtesting), only the Open, High, Low, and Close will be available and there will be no intra-bar data. This means actions cannot happen intra-bar, and fills cannot happen intra-bar. All prices and actions come from and occur when the bar closes as this is all the known information.

            Because of this, OnBarUpdate will only update 'On bar close' as it does not have the intra-bar information necessary for 'On price change' or 'On each tick' and the script will not have the intra-bar information to accurately fill an order at the exact price and time.

            Below is a link to the help guide on Calculate.
            https://ninjatrader.com/support/help.../calculate.htm

            To improve the accuracy of a backtest, you may use Tick Replay along with an added 1-tick series to have logic processed intra-bar and have orders filled intrabar.

            Tick Replay would be used to have the logic process OnEachTick or OnPriceChange with historical data, but this does not allow for intra-bar order fills. You would need to add a single tick data series and submit orders to that single tick data series for a strategy that uses Tick Replay.

            Please reference the SampleIntrabarBacktest example and the following Help Guide links for more information.

            SampleIntrabarBacktest 'Backtesting NinjaScript Strategies with an intrabar granularity' - https://ninjatrader.com/support/helpGuides/nt8/backtesting_ninjascript_strate.htm

            TickReplay — https://ninjatrader.com/support/help...ick_replay.htm

            Developing for Tick Replay -
            https://ninjatrader.com/support/helpGuides/nt8/developing_for__tick_replay.htm?zoom_highlightsub= developing+for+tick+replay

            Additional information may be found in this NinjaTrader Forum post —
            https://ninjatrader.com/support/foru...mance?t=102504


            Please let us know if we may be of further assistance to you.​


            Can CrossAbove or Crossbelow be used when the price of a currently forming bar crosses the indicator of a currently forming bar when set to on price change? For example, go long when price of the current bar crosses above SMA (CURRENT forming bar SMA value not prev bar SMA value). If so, what should the lookback period be? From my understanding, a lookback period of 1 would look at the SMA value of the prev bar, and a lookback period of zero can't be used.

            Comment


              #7
              Hello burmaz,

              Thanks for your notes.

              Yes, you could use Calculate.OnPriceChange to detect if the price of the currently forming bar on the chart crosses above or below an indicator's plot value. Calculate would need to be set to OnPriceChange and you would set the LookBackPeriod of the CrossAbove()/CrossBelow() condition to 1.

              This is because Calculate.OnPriceChange allows OnBarUpdate() logic to process for each change in price that occurs instead of only at the close of a bar.

              The Strategy Builder code in the Conditions and Actions is evaluated based on the Calculate setting. When Calculate is OnPriceChange this allows for the condition to be evaluated for every price change, intra-bar.​

              See this help guide page for more information about Calculate: https://ninjatrader.com/support/help.../calculate.htm
              Brandon H.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_BrandonH View Post
                Hello burmaz,

                Thanks for your notes.

                Yes, you could use Calculate.OnPriceChange to detect if the price of the currently forming bar on the chart crosses above or below an indicator's plot value. Calculate would need to be set to OnPriceChange and you would set the LookBackPeriod of the CrossAbove()/CrossBelow() condition to 1.

                This is because Calculate.OnPriceChange allows OnBarUpdate() logic to process for each change in price that occurs instead of only at the close of a bar.

                The Strategy Builder code in the Conditions and Actions is evaluated based on the Calculate setting. When Calculate is OnPriceChange this allows for the condition to be evaluated for every price change, intra-bar.​

                See this help guide page for more information about Calculate: https://ninjatrader.com/support/help.../calculate.htm
                Expanding on this,

                When calculate is set to on price change, and cross above or cross below is used, does it return a true value for the entire building bar once the condition has been met? Or does it just return a true value immediately when the condition is found and reverts to false when the condition is no longer true?

                If it is the former, how can I reset a 'true' value to back false intrabar instead of waiting for a new bar to form?

                Comment


                  #9
                  Hello burmaz,

                  Thanks for your notes.

                  If you use CrossAbove() and CrossBelow() in a strategy with Calculate.OnPriceChange, the CrossAbove() condition will become true when the first value crosses above the second value. Then, it will become true again in the same bar if the first value crosses below the second value and then the first value crosses above the second value again.

                  For example, say there are 2 SMAs. When the first SMA crosses above the second SMA, the condition is true. Then, in the same bar say the first SMA crosses below the second SMA and then the first SMA crosses above the second SMA again. The condition would become true a second time.

                  There would be no means available to 'reset' a CrossAbove()/CrossBelow() condition. The CrossAbove() condition would become true any time a cross above condition occurs for the values you are passing into the method. The CrossBelow() condition would become true any time a cross below condition occurs for the values you are passing into the method.

                  CrossAbove(): https://ninjatrader.com/support/help...crossabove.htm
                  CrossBelow(): https://ninjatrader.com/support/help...crossbelow.htm
                  Brandon H.NinjaTrader Customer Service

                  Comment


                    #10

                    Hello, I'm trying to test a very simple strategy for study reasons but it doesn't work and I can't figure out where the bug is. The strategy is very simple: if the bars in a row are in a defined range (a counter resets at each intersection of 2 emas) long and short orders are enabled otherwise not. Example: I want trades to be executed only if ema1 cross above/below ema2 when the counter is between 100 and 200 bars in a row after the previous cross. I think I've put print statements for debug almost everywhere...the range conditions are satisfied but the EnterLong-EnterShort are never reached! This is the interested part of the code:

                    // Check if Counter or Counter1 is within the user-defined range

                    if ((Counter[0] >= CounterRangeStart && Counter[0] <= CounterRangeEnd) ||
                    (Counter1[0] >= CounterRangeStart && Counter1[0] <= CounterRangeEnd))


                    {
                    if (BarsInProgress != 0)
                    return;

                    if (CurrentBars[0] < 1)
                    return;

                    // Set 1
                    if (CrossBelow(EMA1, EMA2, 1))
                    {
                    EnterShort(Convert.ToInt32(DefaultQuantity), "");
                    }

                    // Set 2
                    if (CrossAbove(EMA1, EMA2, 1))
                    {
                    EnterLong(Convert.ToInt32(DefaultQuantity), "");
                    }

                    }​

                    any suggestion really really appreciated!

                    Comment


                      #11
                      Hello Alex Al23,

                      The code you extracted is out of context, we cant tell what Counter[0] is set to or what CounterRangeStart, CounterRangeEnd are. You would need to print those values if that part of the logic not working in some way. If that condition is working you would also need to add a print to output the ema values to make sure those are crossing when you think they are.
                      JesseNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by agclub, 04-21-2024, 08:57 PM
                      5 responses
                      34 views
                      0 likes
                      Last Post agclub
                      by agclub
                       
                      Started by ESHunter, Today, 08:06 PM
                      2 responses
                      18 views
                      0 likes
                      Last Post ESHunter  
                      Started by ETFVoyageur, 05-07-2024, 07:05 PM
                      19 responses
                      150 views
                      0 likes
                      Last Post ETFVoyageur  
                      Started by ETFVoyageur, Yesterday, 10:13 PM
                      3 responses
                      26 views
                      0 likes
                      Last Post ETFVoyageur  
                      Started by ETFVoyageur, Yesterday, 12:52 AM
                      3 responses
                      33 views
                      0 likes
                      Last Post ETFVoyageur  
                      Working...
                      X