Overall Statistics
from datetime import timedelta
import decimal
class BasicTemplateFuturesAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2019, 1, 7)
        self.SetEndDate(2019, 6, 15)
        self.SetCash(25000)
        slowperiod = 1720
        self.SetWarmUp(slowperiod)
        
        self.tmf = self.AddEquity("TMF", Resolution.Hour, Market.USA)
        self.spxl = self.AddEquity("SPXL", Resolution.Hour, Market.USA)
        self.spxs = self.AddEquity("SPXS", Resolution.Hour, Market.USA)
        
        self.bearSMA = self.SMA("SPXS", 20, Resolution.Hour)
        self.bullSMA = self.SMA("SPXL", 20, Resolution.Hour)
        self.marketSMA = IndicatorExtensions.Minus(self.bullSMA, self.bearSMA)
        
        
        #Add the spread plot and mark the long/short spread point
        spreadPlot = Chart("Spread Plot")
        spreadPlot.AddSeries(Series("CurrentSMA", SeriesType.Line, 0))
        spreadPlot.AddSeries(Series("Long Spread Trade", SeriesType.Scatter, 0))
        spreadPlot.AddSeries(Series("Short Spread Trade", SeriesType.Scatter, 0))
        spreadPlot.AddSeries(Series("marketSMA", SeriesType.Line, 0))
        self.AddChart(spreadPlot)
        
    def OnData(self,slice):
        #if (not data.ContainsKey(self.spxl) or ( not data.ContainsKey(self.spxs)): return
        if self.IsWarmingUp: return
        if (not self.bullSMA.IsReady) or ( not self.bearSMA.IsReady): return
        CurrentSMA = (self.Securities["SPXL"].Price - self.Securities["SPXS"].Price)
        tolerance = decimal.Decimal(0.50)
        if not self.Portfolio.Invested and CurrentSMA <= (self.marketSMA.Current.Value - tolerance):
            self.SetHoldings("SPXL", -0.2) 
            self.SetHoldings("SPXS", 0.2) 
            self.SetHoldings("TMF", 0.4)
            self.Plot("Spread Plot", "Long Spread Trade", CurrentSMA)
            #self.Log("We are Long, Total Margin Used is: " + str(self.Portfolio.TotalAbsoluteHoldingsCost))
            #self.Log("currentspread is less than esnq tolerance: " + str(CurrentSMA) + " < " + str(self.esnqsma.Current.Value - tolerance))
            #self.Log("What contracts are available??" + str(self.frontES) + " and " + str(self.frontNQ))    
            self.Notify.Sms("+17574091415", "LONG, Paper Forex")
        if not self.Portfolio.Invested and CurrentSMA >= (self.marketSMA.Current.Value + tolerance):
            self.SetHoldings("SPXL", 0.2) 
            self.SetHoldings("SPXS", -0.2) 
            self.SetHoldings("TMF", -0.4) 
            self.Notify.Sms("+17574091415", "Liquidate, Paper Forex")
            self.Plot("Spread Plot", "Short Spread Trade", CurrentSMA)
            #self.Log("We are Short, Total Margin Used is: " + str(self.Portfolio.TotalAbsoluteHoldingsCost))
            #self.Log("currentspread is greater than esnq tolerance: " + str(CurrentSMA) + " > " + 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.marketSMA.Current.Value + 0.10) >= CurrentSMA >= (self.marketSMA.Current.Value - 0.10):
                self.Liquidate()
        self.Plot("Spread Plot", "marketSMA", self.marketSMA.Current.Value)
        self.Plot("Spread Plot", "currentspread", CurrentSMA) 
    def OnOrderEvent(self, orderEvent):
        self.Log(str(orderEvent))