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

Strategy OnBarUpdate() not called on last bar (When Calculate != OnPriceChange).

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

    Strategy OnBarUpdate() not called on last bar (When Calculate != OnPriceChange).

    Hi NinjaTrader support,

    When I run a back-test of a simple strategy on a stock provided by the free Kinetick data feed in Strategy Analyzer on a strategy whose Calculate property is set to Calculate.OnBarClose or Calculate.OnEachTick, OnBarUpdate() is not called on the last daily bar on the chart, even if the close time of the last daily bar on the chart is one or even multiple days in the past.

    For example, if the only line of code in the OnBarUpdate method of a NinjaScript strategy is:

    Print(Time[0].ToString());

    it will print the End Dates of all the daily bars in the back-test up to and including the second to last daily bar of the chart, but not the end date of the last daily bar on the chart.

    This occurs even if I am back-testing on daily bars whose last bar was on Friday and I run the back-test on Saturday. In the example in the previous sentence, the last End Date that the "Print(Time[0].ToString());" line will print will be for Thursday's daily bar, even though there is a daily bar on Friday and I ran the back-test on Saturday.

    I noticed that the last bar is not skipped in two scenarios:

    1. When back-testing in the Strategy Analyzer and the strategy's Calculate property is set to Calculate.OnPriceChange.

    2. When the strategy is applied to a chart, even if it is Saturday and there is no live data coming in.

    My strategy needs to do a lot of work on the last bar of the chart from a back-test or optimization from within Strategy Analyzer, for instance generate a file with a list of positions that should be entered the next day for all S&P 500 stocks via a back-test of the entire basket of S&P 500 stocks via the Strategy Analyzer.

    How can I do this on the actual last bar on the chart, not the second to last bar?


    If I just set the strategy's Calculate property to Calculate.OnPriceChange to gain access to the last bar, I am concerned that this may cause other issues for my strategy, which logically should only run on each bar close and should not skip bars for any reason.

    I've read that this might be doable in the OnStateChange() method when (State == State.Terminate) but this seems a bit risky and less straightforward than a solution whose code could be written in OnBarUpdate() if the bar is determined to be the last bar on the chart.

    Thanks in advance!

    EquityTrader

    #2
    Hello EquityTrader,

    Thank you for your post.

    The calculation mode cannot be set in the Strategy Analyzer itself and I am not seeing a difference in behavior whether or not this is set to OnPriceChange in the script itself.

    The Strategy Analyzer will not run OnBarUpdate for the final bar in available data. For example, if you had ES 12-21 data between midnight and 7 AM, and were running on 1 minute bars, the final Time[0] value would be 6:59 AM.

    This is a long time behavior of NinjaTrader, and changing this could impact existing customers. For this reason, I have added a vote on your behalf to an existing feature request to include the last historical bar in Strategy Analyzer Backtests. This request is being tracked under the number SFT-3395.

    As with all feature requests, interest is tracked before implementation is considered, so we cannot offer an ETA or promise of fulfillment. If implemented, it will be noted in the Release Notes page of the Help Guide.

    Release Notes — https://ninjatrader.com/support/help...ease_notes.htm

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hi NinjaTrader_Kate,

      Thanks for the quick and informative response, and for adding a vote on my behalf for this feature.

      If your dev team implemented this feature, you would sell more licenses to those who want to do what I wanted to do, which is "do a lot of work on the last bar of the chart from a back-test or optimization from within Strategy Analyzer, for instance generate a file with a list of positions that should be entered the next day for all S&P 500 stocks via a back-test of the entire basket of S&P 500 stocks via the Strategy Analyzer."

      I agree with you that "the calculation mode cannot be set in the Strategy Analyzer itself'. When I said "on a strategy whose Calculate property is set to Calculate.OnBarClose or Calculate.OnEachTick", I should have been more specific and said "on a strategy whose Calculate property is set to Calculate.OnBarClose or Calculate.OnEachTick in the OnStateChange() method".

      As to your comment that "I am not seeing a difference in behavior whether or not this is set to OnPriceChange in the script itself", perhaps our different observations have something to do with the fact that I was running these experiments early this morning, before the open, and you were running them during market hours, and NT8 correctly determined that the last bar should not yet be closed, or something along those lines, but that is just a guess.

      Anyway, thanks again for your help today.

      EquityTrader

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by cre8able, Today, 03:20 PM
      0 responses
      5 views
      0 likes
      Last Post cre8able  
      Started by Fran888, 02-16-2024, 10:48 AM
      3 responses
      47 views
      0 likes
      Last Post Sam2515
      by Sam2515
       
      Started by martin70, 03-24-2023, 04:58 AM
      15 responses
      114 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by The_Sec, Today, 02:29 PM
      1 response
      7 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by jeronymite, 04-12-2024, 04:26 PM
      2 responses
      31 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Working...
      X