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


class VolTrading(QCAlgorithm):

    def __init__(self):
        self.previous = None
        self.position = None

    def Initialize(self):

        self.SetStartDate(2018,1,1) #Set Start Date
        self.SetEndDate(2018,1,10) #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(440)



    def OnData(self, data):
        history_s = self.History([self.spy], 2, Resolution.Minute)
        history_v = self.History([self.vixy], 2, Resolution.Minute)
        if not history_v.empty and not history_s.empty:
            last_minute_close_s = history_s.loc[str(self.spy)]["close"][-1]
            last_minute_close_v = history_v.loc[str(self.vixy)]["close"][-1]
        else:
            return
        
        SPercent = np.log((float(self.Securities[self.spy].Price))/last_minute_close_s)
        VPercent = np.log((float(self.Securities[self.vixy].Price))/last_minute_close_v)


        if self.IsWarmingUp:
            return
        
        if SPercent <= -.01:
            if self.position == None:
              self.SetHoldings("VIXY", 1)
            elif self.position == "SPY":
                self.Liquidate("SPY")
                self.SetHoldings("VIXY", 1)
                self.position = "VIXY"
        
        
        elif VPercent <= -.01:
            if self.position == None:
                self.SetHoldings("SPY", 1)
            elif self.position == "VIXY":
                self.Liquidate("VIXY")
                self.SetHoldings("SPY", 1)
                self.position = "SPY"