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 datetime import timedelta
import decimal
class BasicTemplateFuturesAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2019, 1, 1)
        self.SetEndDate(2019, 5, 20)
        self.SetCash(8000)
        slowperiod = 1720
        self.SetWarmUp(slowperiod)
        # Subscribe and set our expiry filter for the futures chain
        futureCL = self.AddFuture(Futures.Energies.CrudeOilWTI)
        futureCL.SetFilter(timedelta(30), timedelta(90))
        futureRB = self.AddFuture(Futures.Energies.Gasoline)
        futureRB.SetFilter(timedelta(30), timedelta(90))
        self.frontCL = None
        self.frontRB = None
        
        
    def OnData(self,slice):
        for chain in slice.FutureChains:
            contracts = list(filter(lambda x: x.Expiry > self.Time + timedelta(45), chain.Value))
            for contract in contracts:
                if ('RB ' in str(contract.Symbol)) and (self.frontRB is None):
                    self.frontRB = sorted(contracts, key = lambda x: x.Expiry, reverse=True)[0]
                    self.Consolidate(contract.Symbol, timedelta(minutes=120), self.twoforty)
                    self.rbsma = self.SMA(self.frontRB.Symbol, 60, Resolution.Minute)
                    
                    
                if ('CL ' in str(contract.Symbol)) and (self.frontCL is None):
                    self.frontCL = sorted(contracts, key = lambda x: x.Expiry, reverse=True)[0]
                    #self.clsma = self.SMA(self.frontCL.Symbol, 20, Resolution.Minute)
                    #self.clrbsma = IndicatorExtensions.Minus(self.clsma, self.rbsma)  
                self.Log("contract symbol: " + str(contract.Symbol))
    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}")
        #if self.IsWarmingUp: return
        #if (not self.clrbsma.IsReady): return 
        #currentspread = (self.Securities[self.frontNQ.Symbol].Price - self.Securities[self.frontES.Symbol].Price)
        currentspread2 = (self.Securities[self.frontRB.Symbol].Price - self.Securities[self.frontCL.Symbol].Price)
        tolerance2 = decimal.Decimal(1.50)
        
        self.Debug("frontRB SMA: " + str(self.rbsma.Current.Value))
        '''
        if (self.frontCL is not None) and (self.frontRB is not None): 
            self.Log("frontcl is not none and frontrb is not none")
            if not self.Portfolio.Invested and currentspread2 <= (self.clrbsma.Current.Value - tolerance2):
                self.MarketOrder(self.frontRB.Symbol , -1)
                self.MarketOrder(self.frontCL.Symbol , 1)
                #self.Plot("Spread Plot", "Long Spread Trade 2", currentspread2)
                self.Log("We are Long, Total Margin Used is: " + str(self.Portfolio.TotalAbsoluteHoldingsCost))
                #self.Log("currentspread is less than esnq tolerance: " + str(currentspread2) + " < " + str(self.clrbsma.Current.Value - tolerance2))
                #self.Log("Did we purchase any contracts??" + str(self.frontES.Symbol) + " and " + str(self.frontNQ.Symbol))
            if not self.Portfolio.Invested and currentspread2 >= (self.clrbsma.Current.Value + tolerance2):
                self.MarketOrder(self.frontRB.Symbol , 1)
                self.MarketOrder(self.frontCL.Symbol , -1)
                #self.Plot("Spread Plot", "Short Spread Trade 2", currentspread2)
                self.Log("We are Short, Total Margin Used is: " + str(self.Portfolio.TotalAbsoluteHoldingsCost))
                #self.Log("currentspread2 is greater than esnq tolerance2: " + str(currentspread2) + " > " + str(self.clrbsma.Current.Value + tolerance2))
                #self.Log("Did we purchase any contracts??" + str(self.frontRB.Symbol) + " and " + str(self.frontCL.Symbol))
            if self.Portfolio.Invested:
                if (self.clrbsma.Current.Value + 1.00) >= currentspread2 >= (self.clrbsma.Current.Value - 1.00):
                    self.Liquidate()   
        '''