Overall Statistics
import numpy as np

### <summary>
### Basic template algorithm simply initializes the date range and cash. This is a skeleton
### framework you can use for designing an algorithm.
### </summary>
class BasicTemplateAlgorithm(QCAlgorithm):
    '''Basic template algorithm simply initializes the date range and cash'''

    def Initialize(self):
        self.SetStartDate(2019, 12, 1) #Set Start Date
        self.SetEndDate(datetime.now().date() - timedelta(1)) # Set End Date
        self.SetCash(10000) #Set Strategy Cash
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage) # Set Brokerage Model
        self.SetTimeZone("America/New_York") # Set Time Zone
        
        # Find more symbols here: http://quantconnect.com/data
        self.svxy = self.AddEquity("SVXY", Resolution.Hour)
        self.svxy.SetDataNormalizationMode(DataNormalizationMode.Raw) # Select Normalization Mode
        self.vxz = self.AddEquity("VXZ", Resolution.Hour)
        self.vxz.SetDataNormalizationMode(DataNormalizationMode.Raw) # Select Normalization Mode
        
        self.fast = self.RSI("SVXY", 6,  MovingAverageType.Simple, Resolution.Hour) # define a period RSI indicator
        #self.slow = self.RSI("SVXY", 15,  MovingAverageType.Simple, Resolution.Hour) # define a period RSI indicator
        
        self.previous = None
        
        self.SetBenchmark("SVXY") # Set Benchmark  
        self.SetWarmUp(20, Resolution.Hour) # Set Warm Up
        
    def OnData(self, data):
        
        if self.IsWarmingUp: # Don't place trades until our indicators are warmed up
            return
        
        holdingsSVXY = self.Portfolio["SVXY"].Quantity
        holdingsVXZ = self.Portfolio["VXZ"].Quantity
    
        # when fastRSI above 70, buy SVXY   
        if holdingsSVXY <= 0:
            if self.fast.Current.Value > 70: # when RSI2 above 70, sell VXZ & buy SVXY
                self.Liquidate("VXZ")
                self.Debug(str(self.Portfolio["VXZ"].AveragePrice)) # Debug average price
                self.SetHoldings("SVXY", 1.0, True)
                self.Debug(str(self.Portfolio["SVXY"].AveragePrice)) # Debug average price
                closeSVXY = self.Portfolio["SVXY"].AveragePrice
                stopMarketTicketSVXY = self.StopMarketOrder("SVXY",-self.Portfolio['SVXY'].Quantity, closeSVXY * 0.90)
                
        # when fastRSI below 85, sell SVXY 
        if holdingsSVXY > 0:
            if self.fast.Current.Value < 85:
                self.Liquidate("SVXY")
                self.Debug(str(self.Portfolio["SVXY"].AveragePrice)) # Debug average price
                
        self.previous = self.Time
        
    def OnEndOfDay(self):
        self.Plot("Indicators","fastRSI", self.fast.Current.Value)