Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trading Multiple Dynamic Contract Months Simultaneously in Backtest/Live

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

    Trading Multiple Dynamic Contract Months Simultaneously in Backtest/Live

    I have a strategy that trades multiple contract months.
    e.g. trading both ES 09-16 and ES 12-16 at the same time.

    During backtests and live trading, I would like to dynamically choose contract months.
    I hope to do this by setting the primary bar series as ES ##-##, but then having 2 dynamic bar series that will change depending on the date and spreads between contract months.

    For example...
    In January, I want to set my 2 dynamic series to ES 03-16 and ES 06-16.
    Once it hits March, I want to exit my trades, and set the 2 dynamic series to ES 06-16 and ES 09-16.

    How can I accomplish something like this?

    Thank you

    #2
    Hello,

    Thank you for the question.

    To dynamically change series like this would likely require adding all of the instruments or contract months using the Add() syntax and then to form a condition that checks the BarsInProgress against a variable so you can say which BIP to use.

    The Add syntax could not be controlled dynamically once the script passes Initialize, you would in essence need to know the span of what contract months or instruments to add when you start the strategy. Because you would need to have the data added at startup, logic in OnBarUpdate is likely the most simple solution.

    An example of this would be the following:

    Code:
    protected override void Initialize() 
            { 
    			Add("ES 09-16", PeriodType.Minute, 1);
    			Add("ES 12-16", PeriodType.Minute, 1);
            } 
    		
            protected override void OnBarUpdate() 
            { 
        		        if(BarsInProgress == myBipVariable)
    			{
    			   //Do logic A	
    			} 
                            else if(BarsInProgress == myBipVariable)
    			{
    			   //Do logic B
    			}
    			if(BarsInProgress == 0)
    			{
    				if(Close[0] > Open[0])
    				{
    					myBipVariable = 1;
    				} 
                                    else if(Close[0] < Open[0])
    				{
    					myBipVariable = 2;	
    				}
    			}
            }
    In this example the Primary bars are being checked for a condition that toggles myBipVariable to change the outcome of which BarsInProgress is used.

    This example assumes you add as many series as needed to cover the span in which the logic may change the contract month.

    I look forward to being of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    558 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    324 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    545 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    547 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X