Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Find price difference between front and back month futures, and backtest successfully

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

    Find price difference between front and back month futures, and backtest successfully

    Hi there! In my strategy, I want to calculate the difference between the front and back month future for a particular instrument. For example, the price difference between GC Aug-24 and GC Dec-24.

    I've managed to add the back month contract as a secondary data series in OnStateChange().

    Code:
       AddDataSeries("GC DEC24", Data.BarsPeriodType.Minute, 240, Data.MarketDataType.Last);
    ​
    I've also got it working dynamically as such, but I've read that this may not be the recommended approach.

    Code:
      AddDataSeries($"{base_symbol} {next_expiry.ToString("MM-yy")}", BarsPeriodType.Minute, 240);
    In playback, I've successfully had both approaches fetch the back month price and appropriately calculate the difference. However, I can't seem to get this working in backtests through the Strategy Analyzer, where the difference between the front and back month is always 0.

    Am I missing something in my settings, or is there another recommended approach here?

    Any guidance would be much appreciated.

    #2
    Hello PhillyCheese,

    Welcome the NinjaTrader forums!

    Unfortunately, dynamically adding series by using variables in AddDataSeries() or putting this in a branching command is not supported.

    From the help guide:
    "Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result in an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner.​"


    These values will have to be hardcoded.

    Further, you will likely need to use the DoNotMerge merge policy.

    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    54 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