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

Range:ATR Filter Using Calculate OnBarClose

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

    Range:ATR Filter Using Calculate OnBarClose

    Good morning,


    I am working on trying to change some code from requiring TickReplay in order to backtest to not requiring it. My requirement for Tick Replay comes in when I try to compare today's current range on daily bars while submitting orders on a tick bar data series. The logic of OnBarClose combined with an added daily bar series made me think I needed Tick Replay and Calculate.OnEachTick for this to work. The reason is because the OnBarClose logic makes YESTERDAY'S index number [0], whereas the OnEachTick logic allows for today's daily bar index to be [0].


    However, I wanted to ask if it is possible to use the following script to access today's range while using Calculate.OnBarClose:


    Code:
    if (Highs[-1] - Lows[-1] > ATR1[0])
    {
    /do something
    }

    Will a -1 index while using Calculate.OnBarClose access today's data?

    #2
    Hello liquid150,
    Thanks for your post.

    It will not be possible to use a negative number for a BarsAgo value. However, it sounds like you just need to separate some logic to calculate on bar close and some on each tick. The following reference sample shows how this can be accomplished.

    Reference Sample - Separating logic to either calculate once on bar close or on every tick
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_JoshG View Post
      Hello liquid150,
      Thanks for your post.

      It will not be possible to use a negative number for a BarsAgo value. However, it sounds like you just need to separate some logic to calculate on bar close and some on each tick. The following reference sample shows how this can be accomplished.

      Reference Sample - Separating logic to either calculate once on bar close or on every tick
      Thanks very much Josh,
      I will review this sample. If implemented, would this need to backtest using Tick Replay? Or could I get by without it? The goal is to eliminate the requirement in backtest.
      Thanks for your time in advance.


      Edit: It appears this requires me to Calculate.OnEachTick. I don't think this will get me where I want to be. I just want to be able to access today's range without having to calculate on each tick. Is this even possible in NT8?
      Last edited by liquid150; 07-23-2018, 08:54 AM.

      Comment


        #4
        That's a good point. I should have caught that (its Monday, forgive me).
        Given that you require backtesting it will likely be best to approach this as a multi time frame strategy.

        Coding a multi time frame or multi series strategy requires a number of coding considerations to correctly address the various data series. These are well detailed in this reference: https://ninjatrader.com/support/help...nstruments.htm
        Please review carefully as the examples build on each section and should be read start to finish.

        In addition, for an example of a Multi time frame strategy, please review the script SampleMultiTimeFrame that is provided with your NinjaTrader installation.
        Josh G.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_JoshG View Post
          That's a good point. I should have caught that (its Monday, forgive me).
          Given that you require backtesting it will likely be best to approach this as a multi time frame strategy.

          Coding a multi time frame or multi series strategy requires a number of coding considerations to correctly address the various data series. These are well detailed in this reference: https://ninjatrader.com/support/help...nstruments.htm
          Please review carefully as the examples build on each section and should be read start to finish.

          In addition, for an example of a Multi time frame strategy, please review the script SampleMultiTimeFrame that is provided with your NinjaTrader installation.
          I totally understand Mondays.



          I've reviewed all the relevant documentation a long time ago. I already understand the multi-time frame coding considerations. They are why I am here today asking this question, essentially. I have code which uses the linked information and the downloaded file to perform this operation.


          Due to the calculation sequence of a multi-time frame strategy, it seems the only way to access a "higher" time frame range is if Calculate.OnEachTick is used, which forces me into using tick replay to mimic the market conditions I want to actually test.


          I was hoping to get around this for two obvious reasons: backtesting and trading server resources. But if this is just impossible in NT8, I understand, and hope maybe the devs can consider making this a little less resource intensive for a daily filter. How to accomplish it, I have no idea.


          Edit: Reflecting a little further on this, I am thinking I might be able to pull this off using a pair of seriesT storing the high and low for the day and resetting on the first bar of the session. If nothing else, our conversation has gotten me to think outside the proverbial box.
          Last edited by liquid150; 07-23-2018, 09:20 AM.

          Comment


            #6
            Sometimes just bouncing ideas is all we need. Let me know if you want to toss any more around.

            Since its relevant to our conversation I will go ahead and have a vote added on your behalf to the feature request regarding the ability to use Tick Replay in the Strategy Analyzer.
            Josh G.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_JoshG View Post
              Sometimes just bouncing ideas is all we need. Let me know if you want to toss any more around.

              Since its relevant to our conversation I will go ahead and have a vote added on your behalf to the feature request regarding the ability to use Tick Replay in the Strategy Analyzer.
              Well, since I have your attention, I could use a second set of eyes.


              This code is, for some reason, returning a daily low of 0 (zero) for every bar. The daily High seems to be working, but Low doesn't.


              Code:
              //filter logic
                          if (Bars.IsFirstBarOfSession)
                          {
                              dailyhigh[0] = High[0];
                              dailylow[0] = Low[0];
                              tradefilter = false;
                              Print("Daily High = " + dailyhigh[0]);
                              Print("Daily Low = " + dailylow[0]);
                              Print("Trade Filter = " + tradefilter);
                           }
                           if (High[0] > dailyhigh[1])
                          {
                              dailyhigh[0] = High[0];
                              Print("Daily High = " + dailyhigh[0]);
                          }
                          if (High[0] < dailyhigh[1])
                          {
                              dailyhigh[0] = dailyhigh[1];
                              Print("Daily High = " + dailyhigh[0]);
                          }
                          if (Low[0] < dailylow[1])
                          {
                              dailylow[0] = Low[0];
                              Print("Daily Low = " + dailylow[0]);
                          }
                          if (Low[0] > dailylow[1])
                          {
                              dailylow[0] = dailylow[1];
                              Print("Daily Low = " + dailylow[0]);
                          }
                          if (dailyhigh[0] - dailylow[0] > ATR2[0])
                          {
                              tradefilter = true;
                              Print("Trade Filter = " + tradefilter);
                          }
              Last edited by liquid150; 07-23-2018, 02:28 PM.

              Comment


                #8
                So it seems I needed to include the possibility that the strategy begins on a bar not the first in the session, and that fixed the issue.

                Comment


                  #9
                  I was just about to write you back. Your are correct, you would need to either add !Bars.IsFirstBarOfSession to the beginning of every condition that wasnt intended for the first bar of the session, or add an else statement for everything else that wasnt the first bar.
                  Josh G.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Rapine Heihei, Today, 08:19 PM
                  1 response
                  8 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Started by Rapine Heihei, Today, 08:25 PM
                  0 responses
                  6 views
                  0 likes
                  Last Post Rapine Heihei  
                  Started by f.saeidi, Today, 08:01 PM
                  1 response
                  9 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Started by Rapine Heihei, Today, 07:51 PM
                  0 responses
                  8 views
                  0 likes
                  Last Post Rapine Heihei  
                  Started by frslvr, 04-11-2024, 07:26 AM
                  5 responses
                  98 views
                  1 like
                  Last Post caryc123  
                  Working...
                  X