Overall Statistics
Total Trades
51
Average Win
0.37%
Average Loss
-0.19%
Compounding Annual Return
7.827%
Drawdown
1.100%
Expectancy
1.001
Net Profit
3.379%
Sharpe Ratio
1.682
Loss Rate
33%
Win Rate
67%
Profit-Loss Ratio
2.00
Alpha
0.195
Beta
-6.004
Annual Standard Deviation
0.045
Annual Variance
0.002
Information Ratio
1.239
Tracking Error
0.045
Treynor Ratio
-0.013
Total Fees
$92.32
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.Minute, Market.USA)
        self.spxl = self.AddEquity("SPXL", Resolution.Minute, Market.USA)
        self.spxs = self.AddEquity("SPXS", Resolution.Minute, Market.USA)
        
        self.bearSMA = self.SMA("SPXS", 20, Resolution.Minute)
        self.bullSMA = self.SMA("SPXL", 20, Resolution.Minute)
        self.marketSMA = IndicatorExtensions.Minus(self.bullSMA, self.bearSMA)
        
        self.symbols = ["SPXS", "SPXL", "TMF"]
        for symbol in self.symbols:
            self.Consolidate(symbol, timedelta(minutes=60), self.twoforty)
            
        #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
        pass   
    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
        
        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 OnEndOfDay(self):
        #self.Log("Settled Cash is: " + str(self.Portfolio.Cash))
        #self.Log("Total Margin Used is: " + str(self.Portfolio.TotalAbsoluteHoldingsCost))
        #self.Log("Total Units held is: " + str(self.Portfolio.TotalHoldingsValue))
        #self.Log("Invested in SPXL is: " + str(self.Portfolio["SPXL"].Invested))
        #self.Log("Invested in SPXS is: " + str(self.Portfolio["SPXS"].Invested))
        #self.Log("Invested in TMF is: " + str(self.Portfolio["TMF"].Invested))
        pass