Code for automated rollover (Part 2)

Nov 3, 2022 | EasyLanguage, TradeStation

Automated Rollover: Can Futures Traders Trust Continuous Contracts?

This article was written by George Pruitt and published on his blog on September 9, 2022. As we have stated in previous articles, for a quantitative trader, understanding and programming trading systems is a common task. Here is a code that automatically allows you to switch contracts at the most opportune moment:

Summary of the first part

I had to finish Part 1 very quickly and probably didn’t fully convey my ideas. This is what was done in Part 1.

  1. I used my function to locate the date of the first notification in raw
  2. I used the same function to get it to print an exact syntax with EasyLanguage
  3. I chose to do an eight day roll before FND and had the function print the exact syntax with EasyLanguage
  4. the output created array mappings and loaded the calculated roll points in YYYMMDD format into the array
  5. visually inspected continuous unadjusted contracts that were butted eight days prior to FND
  6. dates are added to the matrix to match the roll points, as illustrated by the drop in open interest

The sixth step is very important as you need to make sure you are out of position on the correct renewal date. If you don’t, then you will absorb the discount between the contracts at your profit/loss when you exit the trade.

Step 2 – Create the code that executes the rollover operations

This is the code that handles the rollover operations.


//  If in a position and date + 1900000 (convert TS date format to YYYYMMDD),
//  then exit long or short on the current bar's close and then re-enter
//  on the next bar's open

if d+19000000 = rollArr[arrCnt] then
	condition1 = true; 
	arrCnt = arrCnt + 1;
	if marketPosition = 1 then
		sell("LongRollExit") this bar on close;
		buy("LongRollEntry") next bar at open;
	if marketPosition = -1 then
		buyToCover("ShrtRollExit") this bar on close;
		sellShort("ShrtRollEntry") next bar at open;

Code for the renewal of the open position

This code takes us out of an open position during the transition from the old contract to the new one. Remember that our function created and loaded the rollArr for us with the appropriate dates. This simulation is the best we can do – we would actually exit/enter the two different contracts at the same time. Waiting until the open of the next bar introduces slippage. However, in the long run this cost of slippage may disappear.

Step 3 – Create a trading system with entries and exits

    The system will be a simple Donchian in which the close is entered when the high/low of the bar penetrates the high/low of the last 40 bars. If it is long, it will exit at the close of the bar whose low is less than the low of the last 20 bars. If it is short, it will exit at the close of the bar that is greater than the highest high of the last twenty bars. The first test will show the result of using a continuous contract adjusted 8 days in advance to FND

    A nice trade. For the month of August 2014

    A nice trade. For the month of August 2014

    This test will use the exact same data to generate the signals, but the execution will take place on a continuous unadjusted contract with rollovers. Here data2 is the adjusted continuous contract and data1 is the unadjusted one.

    The same Trade but with rollovers

    The same Trade but with rollovers

    It’s still a very nice trade, but it would actually have to put up with six rollover trades and the associated execution costs.


    This is the mechanism of the rollover operation.

    Withdrawal of the old contract and transfer to the new one

    Withdrawal of the old contract and transfer to the new one

    Below are the performance results using $30 for the costs of running the rotations.


    Without rollover:

    Without Rollovers

    Without Rollovers

    Now, with rollovers:

    Many more operations with rollovers!

    Many more operations with rollovers!

    The results are very similar, if additional implementation costs are taken into account. Since TradeStation is not built around the concept of rollovers, many of the trade metrics are not accurate. Metrics such as Average Trades, Win Percentage, Average Win/Loss, and Max Drawdown of Trades will not reflect entries and exits purely based on the algorithm. These metrics take into account the entries and exits promoted by rollovers. The first trading chart where the short was held for several months should be considered an entry and an exit. Rollovers should be executed in real time, but performance metrics should ignore these intermediate operations.

    I’ll try these rollovers with different algorithms, and see if we still get similar results, and post them later. As you can see, testing on unadjusted data with rollovers is not a simple task.

    Article courtesy of George Pruitt

    To read our previous article on Part 1, click this link: Can Futures Traders Trust Continuous Contracts? [Part 1]

    If you want to see part of the code that has been used in this article, we have it available for anyone who wants it, to request it click on this link: Contact

    Quantified Models Youtube Channel

    On our YouTube channel we have several videos available that you may find very useful for developing trading systems. To access, click this link: Quantified Models YouTube Channel

    We hope this information has been useful to you.

    Subscribe to our Newsletter

    Join our mailing list to receive the latest news and updates from Quantified Models team.

    Subscribe to our Newsletter

    You have Successfully Subscribed!

    Skip to content