Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Data import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
import numpy as np

class SPYMeanReversionAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2009,9, 1)  #Set Start Date
        self.SetEndDate(2018,10,30)    #Set End Date
        self.SetCash(10000)           #Set Strategy Cash
        self.spy = self.AddEquity("SPY", Resolution.Daily)
        self.qqq = self.AddEquity("QQQ", Resolution.Daily)
        self.AddEquity("UPRO", Resolution.Daily)
        self.vix = self.AddEquity("VIX", 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 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") == False: return
        if data.ContainsKey("QQQ") == False: return
        if data.ContainsKey("UPRO") == False: return
        if data.ContainsKey("VIX") == False: return
        
        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.Current.Value>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.Debug("SPY buy")
            #self.MarketOrder("SPY", 5000)
            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.Debug("SPY sell")
            self.Liquidate()