Overall Statistics
import numpy as np
from datetime import timedelta

class SPYMeanReversionAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2018,1, 1)  #Set Start Date
        self.SetEndDate(2018,10,30)    #Set End Date
        self.SetCash(10000)           #Set Strategy Cash
        self.SetWarmUp(timedelta(400))
        self.spy = self.AddEquity("SPY", Resolution.Daily)
        self.qqq = self.AddEquity("QQQ", Resolution.Daily)
        self.AddEquity("UPRO", Resolution.Daily)
        self.vix = self.AddEquity("VXX", Resolution.Daily)
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage) 
        self.rsi = self.RSI("SPY", 2)
        self.rsiQQQ = self.RSI("QQQ", 2)
        self.sma200 = self.SMA("SPY", 200)
        self.sma20 = self.SMA("SPY", 20)
        self.adx10 = self.ADX("SPY",10)
        self.min = self.MIN("SPY", 10, Resolution.Daily, Field.Low)
        
    def OnData(self, data):
        if self.IsWarmingUp: return
        if not self.sma200.IsReady: return
        # if not self.adx10.IsReady: return
        # if not self.rsi.IsReady: return
        # if not self.rsiQQQ.IsReady: return
        if self.min.IsReady:
            lowest_low = self.min.Current.Value
        if data.ContainsKey("SPY") and data.ContainsKey("QQQ") and data.ContainsKey("UPRO") and data.ContainsKey("VXX"):
                
            self.VixClosingRng = (data[self.vix.Symbol].Close - data[self.vix.Symbol].Low)/(data[self.vix.Symbol].High - data[self.vix.Symbol].Low)
    
            if self.VixClosingRng > 0.5 and data[self.spy.Symbol].High>self.sma200.Current.Value and self.min.Current.Value>5 and self.rsi.Current.Value<25 and self.rsiQQQ.Current.Value>25 and self.adx10.Current.Value<40 and not self.Portfolio.Invested:
                self.SetHoldings("UPRO", 1)
    
            if data[self.spy.Symbol].Price > self.sma20.Current.Value and (data[self.spy.Symbol].High>self.sma200.Current.Value and self.rsi.Current.Value > 85 or self.rsi.Current.Value > 80):
                self.SetHoldings("UPRO", 0)
                self.Liquidate()