Overall Statistics
import numpy as np
from datetime import datetime
import pandas as pd



class VolTrading(QCAlgorithm):


    def Initialize(self):
      
        self.SetStartDate(2018,2,1)  #Set Start Date
        self.SetEndDate(2018,2,5)    #Set End Date
        self.SetCash(10000)           #Set Strategy Cash
        self.spy = self.AddEquity("SPY", Resolution.Minute).Symbol
        self.vixy = self.AddEquity("VIXY", Resolution.Minute).Symbol
        self.SetWarmUp(10)
        self.previous = None
        self.position = None

        

    def OnData(self, data):
        if self.IsWarmingUp: return         
        if data.Bars.ContainsKey("SPY") or data.Bars.ContainsKey("VIXY"):

            history_s = self.History([self.spy], 1, Resolution.Minute)
            history_v = self.History([self.vixy], 1, Resolution.Minute)
            if str(self.spy) in history_s.index and str(self.vixy) in history_v.index:
                last_minute_close_s = history_s.loc[str(self.spy)]["close"][-1]
                last_minute_close_v = history_v.loc[str(self.vixy)]["close"][-1]
         
    
            SPercent =  np.log(float(self.Securities[self.spy].Price/data["SPY"].Open))
            VPercent =  np.log(float(self.Securities[self.vixy].Price/data["VIXY"].Open))
             
             
            if SPercent <= -0.001:
                if self.position == None:
                    self.SetHoldings("VIXY", 1)
                elif self.position == "SPY":
                     self.Liquidate("SPY")  
                     self.SetHoldings("VIXY", 1)
                self.position = "VIXY"
                  
                  
            if SPercent >= .001:
                if self.position == None:
                    self.SetHoldings("SPY", 1)
                elif self.position == "VIXY":
                    self.Liquidate("VIXY")  
                    self.SetHoldings("SPY", 1)
                self.position = "SPY"