Overall Statistics Total Trades396Average Win25.18%Average Loss-8.71%Compounding Annual Return-51.829%Drawdown60.200%Expectancy0.749Net Profit-51.800%Sharpe Ratio-0.749Loss Rate55%Win Rate45%Profit-Loss Ratio2.89Alpha-1.131Beta44.784Annual Standard Deviation0.562Annual Variance0.316Information Ratio-0.777Tracking Error0.562Treynor Ratio-0.009Total Fees\$732.60
```from datetime import timedelta
import decimal
class BasicTemplateFuturesAlgorithm(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2018, 6, 15)
self.SetEndDate(2019, 6, 15)
self.SetCash(25000)
slowperiod = 1720
self.SetWarmUp(slowperiod)

futureES.SetFilter(timedelta(0), timedelta(182))
futureNQ.SetFilter(timedelta(0), timedelta(182))
self.frontES = None
self.frontNQ = None

def OnData(self,slice):
if (self.frontES is not None) and (self.Time > self.frontES.Expiry):
self.frontES = None
if (self.frontNQ is not None) and (self.Time > self.frontNQ.Expiry):
self.frontNQ = None

for chain in slice.FutureChains:
contracts = list(filter(lambda x: x.Expiry > self.Time + timedelta(45), chain.Value))
for contract in contracts:
if ('ES ' in str(contract.Symbol)) and (self.frontES is None):
self.frontES = sorted(contracts, key=lambda x: x.OpenInterest, reverse=False)[0] #True is lowest to highest
self.Consolidate(contract.Symbol, timedelta(minutes=240), self.twoforty)
self.essma = self.SMA(self.frontES.Symbol, 400, Resolution.Minute)
## Same as above but for NASDAQ -- Also, be sure to wrap the Symbol object in str() when comparing it to a character string or else
## the comparison will fail since they won't be of the same type
if ('NQ ' in str(contract.Symbol)) and (self.frontNQ is None):
self.frontNQ = sorted(contracts, key=lambda x: x.OpenInterest, reverse=False)[0]#True is lowest to highest
self.nqsma = self.SMA(self.frontNQ.Symbol, 400, Resolution.Minute)
self.esnqsma = IndicatorExtensions.Minus(self.nqsma, self.essma)
def OnOrderEvent(self, orderEvent):
#self.Log(str(orderEvent))
pass
def twoforty(self, consolidated):
''' This is our event handler for our 45 minute consolidated defined using the Consolidate method'''
self.consolidated45Minute = True
#self.Log(f"{consolidated.EndTime} >> twoforty >> {consolidated.Close}")
tolerance = decimal.Decimal(18.25)
## Check to make sure both self.frontES and self.frontNQ are contracts that can be traded

if (self.frontES is not None) and (self.frontNQ is not None):
if not self.Portfolio.Invested and currentspread <= (self.esnqsma.Current.Value - tolerance):
self.MarketOrder(self.frontES.Symbol , -1)
self.MarketOrder(self.frontNQ.Symbol , 1)
#self.Log("We are Long, Total Margin Used is: " + str(self.Portfolio.TotalAbsoluteHoldingsCost))
#self.Log("currentspread is less than esnq tolerance: " + str(currentspread) + " < " + str(self.esnqsma.Current.Value - tolerance))
#self.Log("What contracts are available??" + str(self.frontES) + " and " + str(self.frontNQ))
self.Notify.Sms("+test", "LONG, Paper Forex");
if not self.Portfolio.Invested and currentspread >= (self.esnqsma.Current.Value + tolerance):
self.MarketOrder(self.frontES.Symbol , 1)
self.MarketOrder(self.frontNQ.Symbol , -1)
self.Notify.Sms("+test", "Liquidate, Paper Forex");
#self.Log("We are Short, Total Margin Used is: " + str(self.Portfolio.TotalAbsoluteHoldingsCost))
#self.Log("currentspread is greater than esnq tolerance: " + str(currentspread) + " > " + str(self.esnqsma.Current.Value + tolerance))
#self.Log("Did we purchase any contracts??" + str(self.frontES.Symbol) + " and " + str(self.frontNQ.Symbol))
if self.Portfolio.Invested:
if (self.esnqsma.Current.Value + 2.00) >= currentspread >= (self.esnqsma.Current.Value - 2.00):
self.Liquidate()