Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Market Replay consistency, & live market strategy chart crashes

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

    Market Replay consistency, & live market strategy chart crashes

    Hi there,
    Love NT8. Absolute noob at coding, but I've spent the last 3-4 months doing almost nothing else...just learning how to code by making this strategy in NT8. Its been awesome.
    I have a few questions.

    I am running a strategy where most of the logic is happening in OnMarketData().
    I am currently trying to optimize my results by running market replay (without tick replay checked). I am playing back the same range of historical data each time and am getting different results on each replay, when I only really expected them to vary slightly.
    I am running around a month worth of data on each replay. (is this too much to try and run at once?)
    I was wondering if I needed to incorporate the 1 tick data series, like which has been recommended for increasing accuracy for strategy analyzer? I assumed that if I am using OnMarketData() and I am running the strategy in market replay that I wouldn't need to do that. Happy to be corrected though.

    Secondly, the chart that the strategy is running on crashes when high volatility enters the live market. But the rest of Ninjatrader performs just fine. For instance, I had three charts open: NQ DEC23 was running my strategy, ES DEC23 was running my strategy, and MNQ DEC23 was not running my strategy. NQ and ES charts both froze (100% unresponsive with no recovery, needed to force quit NT8 to close those instruments) and there were no print statements in my Output window (indicating there was no significant execution or logic taking place, as I have print statements for most of my methods), while the MNQ chart was fine and I was able to still trade on it, which leads me to believe that running two strategies that are both using OnMarketData() is murdering something here either in my computer or in Ninjatrader. I have successfully managed to run the strategy during volatility on just the one instrument in the past. Obviously this is a great opportunity to optimize and improve my code, but I had a general question about this behavior. Could this behavior be attributed to a computer's capabilities? Could we expect only the chart to freeze if a computer is struggling to process the data that a strategy is trying to process?

    A point to note here, although the strategy is set to OnBarClose and the bars imported are 15m bars, I ended up having the majority of the code running in OnMarketData.I thought this would give me more accurate backtest results, but i think its just giving me a headache.

    Here is an example of a portion of the code running in OnMarketData:

    Code:
    protected override void OnMarketData(MarketDataEventArgs e)
            {​
    
     List<KeyValuePair<DateTime, double>> nqSwingLowsToProcess = new List<KeyValuePair<DateTime, double>>(historicSwingLowsNQ);
    
                    foreach (var nqEntry in nqSwingLowsToProcess)
                    {
                        if (nqEntry.Value == DivergenceLow)
                        {
                            LogOnce("Skipping nqEntry with matching DivergenceLow: " + nqEntry);
                            continue; // Skip to next iteration
                        }
                        if (IsPotentialSwingLow(currentBidNQ, nqEntry.Value, previousBidNQ, detectedNQSwingLow))
                        {
                            if (nqEntry.Value > DivergenceLow && DivergenceLowSetForBar)
                            {
                                return;
                            }
                            else if ((nqEntry.Value < DivergenceLow && DivergenceLowSetForBar) ||
                                    !DivergenceLowSetForBar)
                            {
                                {
                                    hasCrossedOverLows = true;
                                    LogOnce("hasCrossedOverLows is " + hasCrossedOverLows + " for NQ Low: " + nqEntry);
                                    AssignNQSwingLow(nqEntry);
                                    DateTime matchingES = FindMatchingESLow(nqEntry.Key);
    
                                    if (matchingESLowFound)
                                    {
                                        LogOnce("matchingESLowFound is " + matchingESLowFound + " for ES Low: " + matchingES);
                                        AssignESSwingLow(matchingES);
                                        LogOnce("Attempting to tun CheckDivergenceLowLogic at " + CurrentBar);
                                        CheckDivergenceLowLogic(e.Time);
    
                                        if (DivergenceLowSetForBar)
                                        {
                                            LogOnce("(DivergenceLowSetForBar is " + (DivergenceLowSetForBar + " at " + CurrentBar));
                                            break; // Exit Loop
                                        }
                                    }
    
                                    else
                                    {
                                        LogOnce("Attempting to run ResetForNoMatchingNQLow at " + CurrentBar);
                                        matchingESLowFound = false;
                                        ResetForNoMatchingESLow(nqEntry.Key);
                                        TimeStampsToRemoveLows();
                                        break;
                                    }                      
                                }
                            }
                       }
                    }​
    //etc. etc. etc.
    }
    I only really post the code to point out that it is iterating through a set of swings (between 50 & 100) on every market data update....obviously this is insane and needs some drastic optimization (I have ideas for this, such as binning swings into price ranges, as well as converting strategy to OnPriceChange instead, but I haven't summoned the courage to pull any of that off).
    Would having a more powerful computer (Let's say, a new Ryzen 9 with 64gb ram) allow for Ninjatrader to process under these high volatility conditions while I go about optimizing? I realize this is a somewhat generalized question. I'm just unsure how to understand only specific charts crashing, and not the entire software. And I'd love to be able to collect live data from my strategy during trading sessions before going through whatever I am going to go through in order to optimize this strat.

    #2
    Hello kanjy,

    To know why the results are different between two tests you would have to check that all data was exactly identical between the tests and that your logic executed exactly the same. There is a post that goes over comparing results and how to debug a strategy in the following link. The same process would be required to compare two tests in the same tool as well.

    Citizens of the NinjaTrader Community, A common question we hear from clients is 'why are results from backtest different from real-time or from market replay?'. Live orders are filled on an exchange with a trading partner on an agreed upon price based on market dynamics. Backtest orders are not using these market dynamics.


    Regarding charts freezing, if you are only seeing that happen on charts where you applied your strategy that would indicate some problem in that strategies code. To better understand why that happens would require debugging the strategy. A first step would be to identify a use case where you can reliably recreate that happening. Once you have some general steps to see the problem you can comment out your strategies logic and then retest to make sure the freeze no longer happens. After doing that you can uncomment parts of your code and re test to find which part of the code contributes to the problem. Depending on what code is being used when you see the problem would determine the fix. Freezing in a chart is not an expected result so some part of the logic may be getting stuck, for example if you were using a while loop that never exited that would be a case where the script may get stuck and cause issues in a chart.

    Comment


      #3
      Hi there,
      Thanks for the response. Regarding the Market replay - nothing had changed in my code. All it was running was sequential runs of market replay to make sure that market replay produces consistent results, which I was not getting.
      I had seen that article you posted, thanks for mentioning it here again, because I did assume that that article was only relevant for the title of the article - comparing realtime, replay, and historical. I will attempt to incorporate the logic from that article for further testing.
      Thanks for clarification re the strategy freezes. I have done some faaaairly extensive debugging and played it over months of historical data without it freezing. But its time to figure out how to debug....live volatility.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      59 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      134 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      75 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      45 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      50 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X