Overall Statistics
Total Trades
19708
Average Win
0.00%
Average Loss
0.00%
Compounding Annual Return
-99.991%
Drawdown
22.400%
Expectancy
-0.849
Net Profit
-22.443%
Sharpe Ratio
-12.314
Loss Rate
90%
Win Rate
10%
Profit-Loss Ratio
0.55
Alpha
-6.261
Beta
0.428
Annual Standard Deviation
0.504
Annual Variance
0.254
Information Ratio
-12.584
Tracking Error
0.504
Treynor Ratio
-14.478
Total Fees
$36789.10
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.SetEndDate(2017, 1, 10)
        self.SetCash(1000000)

        # Subscribe and set our expiry filter for the futures chain
        futureES = self.AddFuture(Futures.Indices.SP500EMini)
        futureES.SetFilter(timedelta(0), timedelta(90))
        futureNQ = self.AddFuture(Futures.Indices.NASDAQ100EMini)
        futureNQ.SetFilter(timedelta(0), timedelta(90))
        benchmark = self.AddEquity("SPY");
        self.SetBenchmark(benchmark.Symbol);
        self.frontES = None
        self.frontNQ = None

    def OnData(self,slice):
        
        for kvp in slice.Bars:
            self.Debug("---> OnData: {}, {}, {}" .format(self.Time, kvp.Key.Value, kvp.Value.Close));

          
        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
                    self.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
                    self.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))
        self.Debug("self.frontNQ is " + str(self.frontNQ))
        if self.frontES and self.frontNQ:
                
            if not self.Portfolio.Invested:       
                self.SetHoldings(self.frontES.Symbol , 0.2)    
                self.SetHoldings(self.frontNQ.Symbol , -0.2)
            else:
                self.Liquidate()


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