Overall Statistics
Total Trades
315
Average Win
7.60%
Average Loss
-5.30%
Compounding Annual Return
14.414%
Drawdown
60.100%
Expectancy
0.240
Net Profit
304.942%
Sharpe Ratio
0.492
Probabilistic Sharpe Ratio
2.004%
Loss Rate
49%
Win Rate
51%
Profit-Loss Ratio
1.43
Alpha
0.052
Beta
1.003
Annual Standard Deviation
0.312
Annual Variance
0.097
Information Ratio
0.189
Tracking Error
0.277
Treynor Ratio
0.153
Total Fees
$5617.27
Estimated Strategy Capacity
$4700000.00
Lowest Capacity Asset
SVXY V0H08FY38ZFP
# region imports
from AlgorithmImports import *
import numpy as np
# endregion

class DancingRedHorse(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2012, 7, 15)  # Set Start Date
        self.SetEndDate(2022,11,30)
        self.SetCash(100000)  # Set Strategy Cash
        res = Resolution.Daily
        #add etfs and indexs
        self.svxy = self.AddEquity('SVXY',res).Symbol
        self.uvxy = self.AddEquity('UVXY',res).Symbol
        self.spy = self.AddEquity('SPY',res).Symbol
        self.upro = self.AddEquity('UPRO',res).Symbol
        self.vix_idx = self.AddData(CBOE,'VIX',res).Symbol
        self.vix9d_idx = self.AddData(CBOE,'VIX9D',res).Symbol

        #add hull moving averages
        self.vix_hma = self.HMA(self.vix_idx,5,res)
        self.vix9d_hma = self.HMA(self.vix9d_idx,5,res)
        #get histories 
        vix_close = self.History(self.vix_idx,10,res)['close']
        vix9d_close = self.History(self.vix9d_idx,10,res)['close']

        for time,price in vix_close.loc[self.vix_idx].items():
            self.vix_hma.Update(time,price)
        
        for time,price in vix9d_close.loc[self.vix9d_idx].items():
            self.vix9d_hma.Update(time,price)
        
        



        #set warmup
        self.SetWarmUp(5,res)

        self.hma_vix_list = []
    
   



    def OnData(self, data: Slice):
         #if indicators warming up exit
        if self.IsWarmingUp: return
        #if indicaotr not ready exit 
        if not self.vix_hma.IsReady and self.vix9d_hma.IsReady: return

        #vix_current = self.Securities[self.vix_idx].Price
        

        
        #if list is empty or less than 2 elements add hma 
        if len(self.hma_vix_list) <2:
             hma_diff_current = self.vix9d_hma.Current.Value - self.vix_hma.Current.Value 
             self.hma_vix_list.append(hma_diff_current)
             
        

        if len(self.hma_vix_list)>1:
            hma_diff_current = self.vix9d_hma.Current.Value - self.vix_hma.Current.Value 
            #- self.vix_hma.Current.Value
            hma_diff_prev = self.hma_vix_list[1]
        

            #self.Log(f"VIX: {vix_current} VIX_HMA: {vix_hma_current} VIX_HMA_Prev: {self.hma_vix_list[0]} ")
            self.Log(f"Current: {hma_diff_current} Prev: {hma_diff_prev}")



            #and self.vix_idx in data.Bars
            if self.svxy in data.Bars:
                if not self.Portfolio.Invested:
                    #if not invested and vix9d is less than vix currently and previously vix9d was > vix then go long svxy (short vol)
                    if (hma_diff_current < 0) and (hma_diff_prev > 0):
                        self.SetHoldings("SVXY", 1)
                        #self.SetHoldings("UPRO",.50)
                if self.Portfolio.Invested:
                    if (hma_diff_current > .05) and (hma_diff_prev < -.05):
                        self.Liquidate("SVXY")
                        #self.Liquidate("UPRO")
                       
                        
 
                
            
            #update             
            self.hma_vix_list.pop(1)
            self.hma_vix_list.insert(0,hma_diff_current)