Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Inconsistent order quantity between Strategy Analyzer and historical chart executions

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

    Inconsistent order quantity between Strategy Analyzer and historical chart executions

    Hey there all,

    I have a strategy I'm developing that is showing some promising results. While reviewing some backtesting results today via the Strategy Analyzer I noticed that was a couple records in there that entered trades with an outsized order quantity.

    The system trades MES, and the Strategy Analyzer has a record of this 11/14/2022 trade that took 100 contracts. When I go back to that date on the actual chart and run the strategy, the correct number of contracts (in this case 12) is entered/exited.

    My question is, what could be causing this discrepancy between the Strategy Analyzer and the historical chart executions? They're both running the exact same code, with the exact same settings in the strategy.

    I can't quite wrap my head around it, any help would be appreciated.

    Thank you!
    jaybedreamin
    NinjaTrader Ecosystem Vendor - Zion Trading Algos

    #2
    Hello, thanks for writing in. Is there any quantity calculation going on in your code that is causing the strategy to calculate different order sizes each time it enters or are you calling e.g. EnterLong(12);? This is not expected to happen, please also try and see if you can reproduce it on the SampleMaCrossover strategy.

    Comment


      #3
      Hi there Chris,

      Thanks for the response. Yes I am doing quantity calculation using the following code:

      var stopSize = Math.Round(entryPrice - stopPrice, 1);

      double accountValue = AccountSize > 0 ? AccountSize : Account.Get(AccountItem.CashValue, Currency.UsDollar);

      var dollarRisk = accountSize * riskPerTrade;

      var risk = dollarRisk / stopSize;

      var quantity = (int) Math.Round(risk / Instrument.MasterInstrument.PointValue, 1);

      var twoRTarget = (stopSize * 2) + entryPrice;

      var oneRTarget = (stopSize * 1) + entryPrice;

      This code seems to work fine in 99% of backtested cases, but there's just the occasional 1 or 2 backtest results in a given month that will have outsize returns(or losses) due to some large quantity of contracts being bought or sold, which in turn skews the data a bit.
      jaybedreamin
      NinjaTrader Ecosystem Vendor - Zion Trading Algos

      Comment


        #4
        Hello, thanks for the follow-up. This is definitely the source of the quantity issue you are seeing. The Account object does not change during a backtest, you need to simulate an account value within your code in order to keep track of the running cash value of an account during a backtest. This can be a simple double variable that you manage from within the script to represent an accounts cash value.

        Comment


          #5
          Thanks Chris,

          Just to clarify. I'm getting the quantity results during 2 kinds of backtests. One is just done inside the Strategy Analyzer, and the other is done on the chart itself with a custom data series start and end date selected. Are you saying that this code would still produce different results?

          I thought Strategy Analyzer and historical strategy performance via the chart did the same thing?
          jaybedreamin
          NinjaTrader Ecosystem Vendor - Zion Trading Algos

          Comment


            #6
            Hello, thanks for the follow up. The historical back test on the chart would be the same as running a script in the strategy analyzer. The only difference here is that the strategy analyzer does not go into the RealTime state. To debug this, you will need to use the Print() command to see what data the strategy is reading and what is different between backtests.

            Last edited by NinjaTrader_ChrisL; 02-22-2023, 12:16 PM.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Today, 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