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


Intrabar Backtesting

  • Filter
  • Time
  • Show
Clear All
new posts

    Intrabar Backtesting

    Hi everyone,

    From what I understand, Ninjatrader doesn't support intrabar. However, that is very hard to believe. Ninjatrader, which is one of the best trading platforms out there, is the only one that doesn't support true intrabar.

    For instance, with MT4, you can have a strategy using hourly candles and set the backtest for tick data. So let's say that your strategy is based on a moving average cross. Then, as soon as the moving averages cross, you'll instantly enter the trade. So you'll see actual entrance times as 11:28:17 or 2:48:21 or etc, instead of 2:00.

    So where I'm getting at, I feel that there has to be a way that I can program things to make my strategy do this during the backtest. I feel like I can make some very profitable strategies if I can just get my hands on the right technology. From looking at the many platforms out there, either they lack important features, don't support intrabar, or is very, very buggy.

    So I hope that Ninjatrader has something that could help me out. Also, I don't think many people wait until a bar closes to enter a trade. When something looks good, they get in early to take advantage of the big move. So for the life of me, I don't know why Ninjatrader doesn't support something so essential without the need of very complex coding (if even possible).

    Thanks for your assistance.

    Are you a programmer?

    The better you understand coding logic the better you'll understand this answer.

    You'll need to explicitly code for intra-bar granularity in your strategy.

    In NinjaTrader, everything revolves revolves around "bars". The bars you
    see on the chart is (by definition) always called the primary bar series. If
    you want a smaller time frame bar series, such as your question indicates,
    you can add one or more of these to the strategy, and they're all referred
    to as secondary bar series.

    However, only the primary bar series is ever drawn on the chart, all
    secondary bar series are invisible.

    But secondary bar series are not invisible to your code.

    All bar series are available to your code, in an array called "BarsArray".

    The primary bar series is always at index zero, which is BarsArray[0],
    exactly as you would expect.

    Secondary bar series start at index '1', but are only created when you
    add them explicitly via the Initialize method. For example,

    public override void Initialize()
        Add(PeriodType.Tick, 100);   // adds a 100-Tick secondary bar series
    So now your code has access to an invisible sequence of bar data where
    each bar is 100 ticks -- the 100 tick bars are the secondary bar series.

    Regardless if your chart is showing a 8-Range, a 5-Minute, a 30-Minute, a 4-Renko,
    a 500-Tick, a 1000-Volume -- it doesn't matter what the primary bar series is -- the
    code in your Initialize() method above installs a secondary bar series which is
    always a 100-Tick bar series.

    Now, you need to take a deep dive and understand BarsInProgress.

    This is a global integer variable provided by NinjaTrader that represents
    the index of that BarsArray thing I talked about above.

    And here's the magic: NinjaTrader calls your OnBarUpdate() method once
    for each bar series, every time a bar closes in that series.

    How do you know which bar series is being called? You explicitly check
    for that in your code, like this,

    public override void OnBarUpdate()
        if (BarsInProgress == 0)
            // primary bar series
        else if (BarsInProgress == 1)
           // 1st secondary bar series
           // for your question, your extra logic goes here
    Does that make sense?

    The Add() in the Initialize() is "linked" to the code section where BarsInProgress
    is equal to "1".

    It's not all that much extra code, really. And once you understand
    the multi-time frame model, it's not that hard to code for it.

    It's a very powerful model.
    Last edited by bltdavid; 09-20-2016, 09:23 AM.


      "Are you a programmer?"

      I'm a programmer at the intermediate level.

      Thanks so much for taking the time to write a thorough explanation. About a year ago, I was messing with everything you spoke of. Ninjatrader also did a good job providing documentation on how to use it. However, I just couldn't get it to work, couldn't get any of the samples that Ninjatrader provided to work, and I was never able to get back answers from the mods to make it work either. That was when NJ8 was in beta-testing mode and maybe, I can get it to work now. Based on how well you explained it, I'm assuming that you used intrabar before and got it to work. I will try the same thing.

      Thanks so much for reaching out.

      Also, I believe I can run one strategy on multiple instruments and run multiple strategies on assets. Right?


        Originally posted by BernWillChris View Post
        Also, I believe I can run one strategy on multiple instruments
        Sure. Say you have 5 different instruments.

        Start 5 different chart windows and select the 5 different instruments.

        In each window chart, run the same strategy 'A' in each chart window.


        Originally posted by BernWillChris View Post
        and run multiple strategies on assets. Right?
        Sure, but this is more tricky.

        Say you have 3 different strategies, A, B, and C.

        Start up 3 different chart windows, set each chart to the same instrument.

        Now start strategy 'A' in chart window 1, start 'B' in window 2, etc.


        But you'll have some potential issues (that unless you specifically code for) could ruin everything.

        If strategy 'A' goes 'LONG 1' while strategy 'B' goes 'SHORT 1', your account position becomes FLAT.

        This is where you need to understand the different between strategy position and account position -- it's a real distinction that affects NT7 and NT8.

        Strategy 'A' has a strategy position of LONG 1.
        Strategy 'B' has a strategy position of SHORT 1.

        The issue is: strategies run in a vacuum. That is, each strategy thinks it owns the world; they don't (intrinsically) know how to cooperate with other strategies running against the same instrument.

        Your broker is also an independent 'thing'. Your broker gets an order from 'A' to go 'LONG 1', and then the order from 'B' to go 'SHORT 1' which means you're now FLAT.

        Your broker doesn't know you're running a strategy, or trading manually. A request to buy 1 contract (aka go long) followed by a request to sell 1 contract (aka go short) means the broker has set your account to flat.

        Ok, so solve it. How? Well, you're the programmer here, you have total control, so you'll have to figure out what to do:

        1. Run A with different trading hours than B.
        2. Design your strategy to create a lock: first strategy to enter a position locks that instrument and prevents the other strategies from trading until the lock is released.
        3. Setup different accounts with your broker, run A in acct #1 and run B in acct #2.

        So, like I said, sure you can do it, but it requires extra care to do it right.
        Last edited by bltdavid; 09-19-2016, 08:17 AM.


          Hello BernWillChris,

          In addition to the excellent advice already posted I would like to invite you to review this section of the help guide,

          and to please ask us about any questions on any specific topics that come up.
          Last edited by NinjaTrader_JessicaP; 09-19-2016, 10:26 AM.
          Jessica P.NinjaTrader Customer Service


            Thanks for your reply. I wasn't clear when I was asking the question. What I meant, was can you run a strategy on multiple assets in the Strategy Analyzer? Why is this important? I plan to run a strategy on multiple assets when I go live, so it would be very helpful to see how this could respond in the backtester.

            If I was running a strategy on just on one asset and see a drawdown of just 10 percent over the span of a year, maybe if that strategy was ran on 8 assets, which I plan to do, it would've been of 80% DD.

            Now for running multiple strategies on the same asset, getting info about this can be significant too. For a reason that you brought up, I can have Strategy A requesting an instrument to go long and Strategy B telling the same instrument to go short, which isn't allowed at FXCM. There are more benefits too.

            From what I've read so far, I should be able to conduct a backtest similar to these.

            Thanks so much man!


              Hi Jessica,

              Thanks for the advice. I think what I'm concerned about right now, is the proper exit and I'm not sure why this is happening:

              As you can see, I'm entering correctly. The chart is on hourly candles and I programmed the strategy to enter the trade at the 7th minute; so that's working. However, it is very unlikely that my trades all hit their targets at the top of the hour.

              Also, I'm not seeing the rollover fees. I will be in trades for days and this would be good to know. Thanks so much in advanced.
              Last edited by BernWillChris; 09-19-2016, 10:22 AM.


                Originally posted by BernWillChris View Post
                What I meant, was can you run a strategy on multiple assets in the Strategy Analyzer?

                Easiest way is to create a new list in Instrument Manager.

                In NT7, click the New button in lower left of Instrument Manager.

                Call it, say, "TESTING", and then add your preferred instruments
                to this TESTING list.

                Then, go back to your Strategy Analyzer window and select the
                TESTING list when running a back test.

                Here is an NT8 link,

                Last edited by bltdavid; 09-19-2016, 10:31 AM.


                  Originally posted by BernWillChris View Post
                  I think what I'm concerned about right now, is the proper exit and I'm not sure why this is happening:

                  As you can see, I'm entering correctly. The chart is on hourly candles and I programmed the strategy to enter the trade at the 7th minute; so that's working. However, it is very unlikely that my trades all hit their targets at the top of the hour.
                  Ok, so how is your code exiting the trade?
                  What are you doing exactly to make NinjaTrader exit the trade?
                  Are you using a trailing stop loss?
                  Do you have a target set?

                  Come on, man, neither Jessica nor I nor anyone else can see the code on your monitor. How the heck are we supposed to know what your code is doing wrong?

                  Well, my guess is (drumroll please) you have a bug, plain and simple.

                  Want help finding it? Throw us a bone and attach some code!
                  Last edited by bltdavid; 09-19-2016, 10:42 AM.



                    Hi bltdavid,

                    Thanks again. Okay, you've a point and I decided to attach the code. It is very simple. Just trying to get the basic things working before I start creating my algorithms and stuff.

                    Basically, I did everything you mentioned in your first post. I set the chart to hourly candles and picked a random time within the bar to see if it's doing intrabar. So I set it up to go long and I added the SL and TP to it so that I can get a sample of multiple trades.

                    You've been very helpful and if you like, I can donate for your help to a Paypal or something. Just cannot thank you enough because I know there's money in algotrading. Just needed the technology to do it.


                    Thanks again.


                      Thank you for providing this code sample, BernWillChris . I would like to provide an excerpt from the help guide,

                      Originally posted by
                      It is suggested to call this method from within the strategy OnStateChange() method if your stop loss price/offset is static
                      protected override void OnStateChange()
                          if (State == State.Configure)
                              // Submits a stop loss of $500
                              SetStopLoss(CalculationMode.Currency, 500);
                      I would therefore recommend calling these methods immediately below your AddDataSeries call.

                      Please let us know if this doesn't give you the desired results, or if there are any other ways we can help.
                      Jessica P.NinjaTrader Customer Service


                        Hi Jessica,

                        I tried it:

                        else if (State == State.Configure)
                                        AddDataSeries(BarsPeriodType.Tick, 1);
                                        SetStopLoss(CalculationMode.Pips, 40);
                                        SetProfitTarget(CalculationMode.Pips, 40);
                                protected override void OnBarUpdate()
                                     if (BarsInProgress == 1){                
                                        if(Time[0].Minute == 7){
                        I just used pips instead and have an else-if statement. I even tried it with just the if-statement. Also, it is important that I know how to do this with adjusting my targets while in a trade based on market conditions too. So currently, I'm still not getting out as soon as I reached my target.



                          Hello BernWillChris,

                          So that I may have a more complete picture of what is happening on your end, would it be possible to use the freely and publicly available screen capture software Jing, , or a similar program which can record video of what is happening on your screen, so that you can show me what is occurring on your end? These quick instructions can get you started with Jing.

                          1. When you start Jing, a yellow half circle will appear near the top of your screen. Please hover over it with your mouse
                          2. A small + sign will extend out of this half circle. Please click on it
                          3. When your cursor becomes two intersecting lines, please click and drag your mouse over an area of your screen you would like to record
                          4. Please press the film strip button that appears to begin recording
                          5. When you are finished recording, please press the square stop button that appears
                          6. Please press the share button that appears. It consists of three vertical upward pointing arrows.

                          You will then have the option to view your video on . This is the link which can help me diagnose what is happening on your end. It is possible you will need to set up a free account in order to share videos.

                          Thank you very much in advance for providing us with this information. If this procedure is not an option for you, or if there are any other questions we can answer, please let us know.
                          Jessica P.NinjaTrader Customer Service


                            Hi Jessica,

                            Thanks for your response. I can do the Jing when I get time later today (I have a meeting soon). However, all I'm asking for is a way to backtest with intrabar. The only answer I'm looking for is a sample code that does what I'm asking. I cannot find anything that does it. Please remember that I want my strategy to execute exactly after the conditions are satisfied just like I would expect from a live account.

                            Let's say I have an hourly candle and the moving averages cross at 9:17:28 and hits its target of 20 pips at 9:48:53, then this is exactly what I would love to see in my backtest. I know everything is working and I'm getting back accurate results.

                            I know I have taken some heat from not being specific enough, but I'm not sure why. All I'm asking for is a sample that a member has already done for educational purposes or possibly a snippet of code with a screenshot of the settings that were used in the Strategy Analyzer and the trades taken so that we can confirmed that everything is working. Also, I believe the feature list of NJ8 said that I can use the Order Fill Resolution by setting it to high and not have to worry about tweaking the code, but that doesn't work either. And I cannot find the link to the intrabar sample code that was on the web, but that strategy isn't getting me in trades either intrabar.

                            Thanks so much.


                              Thank you for the additional information, BernWillChris .

                              I believe I misunderstood what you were asking; I was under the impression that SetProfitTarget and SetStopLoss were not working the way we expected, which is why I requested the video. When I tried to reproduce this on my end both those methods worked the way I expected them to, and so I was hoping to see how our systems were differing to clue me in as to why we were getting what I believed to be different results.

                              However, in light of the explanation you have just given, a video will no longer be necessary.

                              The best way to get a strategy back test to perform with precision the same way it would with live market data is with the Market Replay connection. I am including a link to the help guide section on Market Replay.


                              In NT8 this is the Playback Connection


                              The reason I am suggesting this approach is because backtesting using the Strategy Analyzer has two key differences which may be affecting your results :

                              • Ninja has to simulate the actions of the trade desk during historical fill processing in other than real time during backtesting. You can read up more on historical fill algorithms here,
                              • While NT8 has improvements in this area, generally Market Data events and other between OnBarUpdate events come in at different times during historical fill processing than they ordinarily would

                              With the Market Replay connection, the data coming into your script looks just like live data, and market volatility can be controlled in a much more intuitive manner with the playback speed.

                              Generally speaking, I typically use the strategy analyzer and the optimizer to ensure my script does what I expect it to do, and to come up with the best ways to configure it. When I want to test that it is performing correctly to real world conditions or specific scenarios, however, I usually use the Market Replay connection. The situation you are describing, where you have two known boundary conditions and a known crossover point, is ideal for testing with Market Replay.

                              If we are not getting expected results with Market Replay, if you could let me know which dates and times we are testing through and which instrument and expiry, I can attempt a test on my end and come back with more information.
                              Jessica P.NinjaTrader Customer Service


                              Latest Posts


                              Topics Statistics Last Post
                              Started by daniel_weber8, Today, 01:21 PM
                              1 response
                              Last Post NinjaTrader_ChristopherS  
                              Started by jungsoohuh, Today, 12:07 PM
                              1 response
                              Last Post NinjaTrader_PaulH  
                              Started by SharonSS, Today, 11:54 AM
                              1 response
                              Last Post NinjaTrader_Jim  
                              Started by crystalet, Today, 11:20 AM
                              3 responses
                              Last Post NinjaTrader_Jesse  
                              Started by SuperDude123, Today, 09:23 AM
                              1 response
                              Last Post NinjaTrader_JustinD