Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

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 = self.AddFuture(Futures.Indices.SP500EMini)
        futureES.SetFilter(timedelta(0), timedelta(35))
        futureNQ = self.AddFuture(Futures.Indices.NASDAQ100EMini)
        futureES.SetFilter(timedelta(0), timedelta(35))
        benchmark = self.AddEquity("SPY");
        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))