Overall Statistics
Total Trades
12
Average Win
3.08%
Average Loss
-7.57%
Compounding Annual Return
9.011%
Drawdown
18.700%
Expectancy
0.173
Net Profit
7.475%
Sharpe Ratio
0.466
Loss Rate
17%
Win Rate
83%
Profit-Loss Ratio
0.41
Alpha
0.117
Beta
0.02
Annual Standard Deviation
0.253
Annual Variance
0.064
Information Ratio
0.388
Tracking Error
0.253
Treynor Ratio
5.776
Total Fees
$12.75
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()