Overall Statistics Total Trades0Average Win0%Average Loss0%Compounding Annual Return0%Drawdown0%Expectancy0Net Profit0%Sharpe Ratio0Loss Rate0%Win Rate0%Profit-Loss Ratio0Alpha0Beta0Annual Standard Deviation0Annual Variance0Information Ratio0Tracking Error0Treynor Ratio0Total Fees\$0.00
```from clr import AddReference

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Securities import *
from datetime import timedelta

### <summary>
### This example demonstrates how to add futures for a given underlying asset.
### It also shows how you can prefilter contracts easily based on expirations, and how you
### can inspect the futures chain to pick a specific contract to trade.
### </summary>
### <meta name="tag" content="using data" />
### <meta name="tag" content="benchmarks" />
### <meta name="tag" content="futures" />
class BasicTemplateFuturesAlgorithm(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2017, 1, 1)
self.SetEndDate(2018, 1, 1)
self.SetCash(1000000)

# Subscribe and set our expiry filter for the futures chain
futureES.SetFilter(timedelta(0), timedelta(35))
futureES.SetFilter(timedelta(0), timedelta(35))
self.SetBenchmark(benchmark.Symbol);
self.frontES = None
self.frontNQ = None

def OnData(self,slice):

for chain in slice.FutureChains:
if chain.Key.Value == Futures.Indices.SP500EMini:
if self.frontES is None :# Get contracts expiring no earlier than in 90 days
contracts = list(filter(lambda x: x.Expiry > self.Time + timedelta(10), chain.Value))
# if there is any contract, trade the front contract
if len(contracts) == 0: continue
frontES = sorted(contracts, key = lambda x: x.Expiry, reverse=True)[0]

if chain.Key.Value == Futures.Indices.NASDAQ100EMini:
if self.frontNQ is None:
# Get contracts expiring no earlier than in 90 days
contracts = list(filter(lambda x: x.Expiry > self.Time + timedelta(10), chain.Value))
# if there is any contract, trade the front contract
if len(contracts) == 0: continue
frontNQ = sorted(contracts, key = lambda x: x.Expiry, reverse=True)[0]
#if data.ContainsKey(self.vx1) and data.ContainsKey(self.es1):
# update the rolling window price and time-to-maturity series every day
self.Debug("self.frontES is " + str(self.frontES))
if self.frontES and self.frontNQ:

if not self.Portfolio.Invested:
self.SetHoldings(frontES.Symbol , 0.2)
self.SetHoldings(frontNQ.Symbol , -0.2)
else:
self.Liquidate()

def OnOrderEvent(self, orderEvent):
self.Log(str(orderEvent))```