Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

My strategy won't backtest, how to fix

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

    My strategy won't backtest, how to fix

    I have a strategy that runs onBarUpdate on each tick. At a specific start time, it starts collecting price data, keeping the high and low that occurred since that start time. At a specific end time (say 2 minutes later), it stops collecting data. Sonow it has the high and low that the price was during that 2 minute period. It then sets stop entry orders a few ticks above the high and a few ticks above the low.

    The problem is that I cannot seem to backtest this. When I enable the strategy on a chart, it should show all my past trades. It certainly does this when I try to backtest other strategies, like a simple MA crossover. But my strategy never seems to fire.

    So what am I missing? Do I have to somehow tell NT8 that I want my backtested strategy to run onBarUpdate on every tick? Does the backtest have bad values for current time" when I try to get it? What am I doing wrong?

    Also, is there any way to see the print statements from historical data? If I could see print output from running the strategy on past data, I might be able to figure out what is wrong. Any way to do this?
    Last edited by westofpluto; 01-24-2023, 01:19 PM.

    #2
    Hello westofpluto,

    OnEachTick is a realtime only property so that would be one major part controlling why it wont backtest. Historical/backtests are always done OnBarClose so your logic will be executing differently if you normally use OnEachTick in realtime. If you need to use OnEachTick specifically you will need to test using the playback connection or realtime. If you can otherwise do whatever logic you described by using a 1 tick series you could use AddDataSeries to accomplish that. TickReplay can also be used for that purpose if you needed to use OnMarketData historically.

    Print statements work in both historical and realtime. If you are not seeing a print that would mean the print is not being called.



    Comment


      #3
      So it turns out my strategy was entering orders using a Custom Event Timer (System.Timers.Timer). This works fine in realtime, but does not trigger at all during backtesting.

      So, I added the tick data series as you suggested in order to get tick data. I also changed the entry method in the strategy to the same entry rules but get triggered in OnBarUpdate for the primary data series. Now the strategy backtests just fine.

      It's a shame that backtesting does not properly handle entries from within a function called by System.Timers.Timer.

      Thanks for the help.

      Comment


        #4
        Hello westofpluto,

        Just to clarify why that wont work, historical bars are processed all at once because the data is already present. That is how the backtest can run many months of data in a short period of time, its just doing the raw processing for each bar one after another until its done. A timer relies on the systems PC clock to move forward in time and process its elapsed event, that won't happen in historical because the bar/processing will be complete much sooner than the timer can start or your logic may reset the timer at a fast rate which effectively makes it ignored.

        For any kind of historical testing you want to always rely on the NinjaScript data events so that the logic you are trying to execute is executed in the same context as the strategy. The timer is in a separate context because it is running at its own pace so that won't sync up with how the strategy is expecting to submit historical orders. In realtime that would be fine to use for more advanced use cases because there is realtime granularity to keep driving the script and also the timer is working on its own based on the realtime pc clock.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        53 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        130 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        70 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        44 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        49 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X