Overall Statistics
Total Trades
162
Average Win
1.19%
Average Loss
-0.37%
Compounding Annual Return
22.550%
Drawdown
8.300%
Expectancy
0.315
Net Profit
9.382%
Sharpe Ratio
1.177
Loss Rate
69%
Win Rate
31%
Profit-Loss Ratio
3.26
Alpha
0.733
Beta
-25.892
Annual Standard Deviation
0.186
Annual Variance
0.035
Information Ratio
1.07
Tracking Error
0.186
Treynor Ratio
-0.008
Total Fees
$265.97
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))