Profile Picture

Backtesting individual futures contracts

Posted By Omega1 5 Years Ago
Message
Posted Friday April 20 2012
Hi,

I'm thinking to try to set up a backtest where the simulation rolls through the indivdual contracts of an instrument like the ES.

Has anyone ever done this before? I'd appreciate some pointers about how to go about this.

Regards,

David

Posted Monday April 23 2012
You need to set up a separate symbol for each expiration date and include them all in the simulation run.

In your system startup, you can have logic to order the symbols by expiration date and pick the first one as the current active symbol.  In NewBar(), you can check whether the current date is nearing the expiration date of the active symbol, and if so move to the next expiration date.

Here is some sample code for the MySystem class that does this.

public class MySystem : MySystemBase

{

         public List<Symbol> ESSymbols = new List<Symbol>();

 

         public Symbol ActiveSymbol;

        

         public override void Startup()

         {

                 //       Get all ES symbols ordered by expiration date

                 ESSymbols = Symbols.Where(s => s.Name == "ES").OrderBy(s => s.ExpirationDate).ToList();

 

                 //       Get first ES symbol (by expiration date) and remove it from the list

                 ActiveSymbol = ESSymbols.First();

                 ESSymbols.RemoveAt(0);

         }

 

 

         public override void NewBar()

         {

                 //       Check if current date is within 7 days of the active ES symbol's expiration date

                 //       If so, move to next symbol

                 while (ActiveSymbol != null &&

                                   ActiveSymbol.ExpirationDate < SystemData.CurrentDate.AddDays(7))

                 {

                          ActiveSymbol = ESSymbols.First();

                          ESSymbols.RemoveAt(0);

                  }

 

                 base.NewBar();

         }

}

In your MySymbolScript, you can have a check something like this to only trade the active symbol:

if (Symbol == TradingSystem.ActiveSymbol)
{
         //       Trading logic here
}

Thanks,
Daniel

Posted Friday May 11 2012
Daniel - That's genius! esp the sorting.

Question though. Indicators are still all unique instances for each symbol?

i.e. If you have a 20day MA for the first symbol, its last 19 days will not "roll" into the MA for the second symbol right?

thanks

Posted Friday May 11 2012
The indicators will be different for each symbol. However, even though there is only one active trading symbol at once, the indicators should be calculated for all the symbols that have data for a given bar. So when you roll to a new symbol, an SMA20 for that symbol would be the average of the prices from the last 19 days for the new symbol.

Basically yes, the indicators will be different, but if you have enough data for a symbol before you switch to it, the indicator values should be valid.

Does that make sense?

Thanks,
Daniel
Posted Friday May 11 2012
Yes, Understood
thanks


Similar Topics


Reading This Topic


2005-2017 © RightEdge Systems