Overall Statistics
Total Trades
19
Average Win
33.30%
Average Loss
-4.32%
Compounding Annual Return
51.032%
Drawdown
26.900%
Expectancy
3.833
Net Profit
234.165%
Sharpe Ratio
1.525
Probabilistic Sharpe Ratio
67.384%
Loss Rate
44%
Win Rate
56%
Profit-Loss Ratio
7.70
Alpha
0.48
Beta
-0.057
Annual Standard Deviation
0.31
Annual Variance
0.096
Information Ratio
0.891
Tracking Error
0.38
Treynor Ratio
-8.27
Total Fees
$329.33
import numpy as np
from QuantConnect.Python import PythonQuandl

class VIXPredictsStockIndexReturns(QCAlgorithm):

    def Initialize(self):

        self.SetStartDate(2018, 1, 1)  
        self.SetEndDate(2020, 12, 1)    
        self.SetCash(100000)          
        self.AddEquity("SPY", Resolution.Daily)
        self.AddEquity("SPXL", Resolution.Daily)
        self.AddEquity("TMF", Resolution.Daily)
        self.vix = 'CBOE/VIX'
        self.AddData(QuandlVix, self.vix, Resolution.Daily)
        self.window = RollingWindow[float](252*2)
        hist = self.History([self.vix], 1000, Resolution.Daily)
        for close in hist.loc[self.vix]['vix close']:
            self.window.Add(close)
        
        self.lsma20 = self.LSMA(self.vix, 20, Resolution.Daily)
        self.lsma200 = self.LSMA(self.vix, 200, Resolution.Daily)
        self.ema2 = self.EMA("SPY",2,Resolution.Daily)
        self.ema10 = self.EMA("SPY",10,Resolution.Daily)
        self.stocks = None
        self.bonds = None
        self.SetWarmUp(200) 
        
    def OnData(self, data):
        if not data.ContainsKey(self.vix): return
        self.window.Add(self.Securities[self.vix].Price)
        if not self.window.IsReady: return 
        history_close = [i for i in self.window]
        self.Log(f"{self.Time} lsma20 {self.lsma20.Current.Value} lsma200 {self.lsma200.Current.Value}")
        
        if self.lsma20.Current.Value < self.lsma200.Current.Value and self.ema2.Current.Value >  self.ema10.Current.Value and self.stocks == None:
            self.Liquidate("TMF")
            self.SetHoldings("SPXL", 1)
            self.stocks = 1
            self.bonds = None
        
        if self.lsma20.Current.Value > self.lsma200.Current.Value and self.ema2.Current.Value <  self.ema10.Current.Value and self.bonds == None:
            self.Liquidate("SPXL")
            self.SetHoldings("TMF", 1)
            self.bonds = 1
            self.stocks = None
            
class QuandlVix(PythonQuandl):
    
    def __init__(self):
        self.ValueColumnName = "VIX Close"