Overall Statistics
import numpy as np
from clr import AddReference
AddReference("System")
AddReference("NodaTime")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")

from System import *
from NodaTime import DateTimeZone
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Brokerages import *
from QuantConnect.Data.Market import *

class BasicTemplateAlgorithm(QCAlgorithm):
    

    def Initialize(self):
        # Set the cash we'd like to use for our backtest
        # This is ignored in live trading 
        self.SetCash(37000)
        
        # Start and end dates for the backtest.
        # These are ignored in live trading.
        self.SetStartDate(2011,06,8)
        self.SetEndDate(2018,2,6)
        
        #Assets predetermined
        #self.uvxy = self.AddEquity("UVXY", Resolution.Hour).Symbol
        self.svxy = self.AddEquity("SVXY", Resolution.Hour, None, True, 1, True).Symbol
        self.spy = self.AddEquity("SPY", Resolution.Minute, None, True, 1, True).Symbol
        
        # Indicators
        self.emaBig = self.EMA("SVXY", 5, Resolution.Hour)        
        self.emaSmall = self.EMA("SVXY", 2, Resolution.Hour)
        
        # Schedules
        self.Schedule.On(self.DateRules.EveryDay("SVXY"), self.TimeRules.At(16, 1), Action(self.EveryDayBeforeMarketClose))
        self.Schedule.On(self.DateRules.EveryDay("SVXY"), self.TimeRules.At(9, 59), Action(self.EveryDayAfterMarketOpen))
        self.Schedule.On(self.DateRules.On(2018, 1, 22), self.TimeRules.At(13, 0), Action(self.SpecificTime))
        
        # Timers
        self.w = 1
        
        # Set benchmark
        self.SetBenchmark("SVXY")
        
        # Get warmup data so that the Algorithm can start trading
        self.SetWarmUp(5)
        
        # Counters
        self.case1 = 0
        self.case2 = 0
        
    def OnData(self, data):
        
        if self.w == 0:
            
            if not self.Portfolio.Invested:
                if self.emaBig < self.emaSmall:
                    self.case1 += 1
                    self.SetHoldings(self.svxy, 0.85)
            
            else:
                if self.emaBig > self.emaSmall:
                    self.case2 += 1
                    self.SetHoldings(self.svxy, 0)
                    
    def EveryDayBeforeMarketClose(self):
        if self.w == 0:
            self.w = 1
            #self.Log('Market Close')
        
    def EveryDayAfterMarketOpen(self):
        self.w = 0
        #self.Log('Market Open')
    
    def SpecificTime(self):
        self.Log('Case 1: {}'.format(self.case1))
        self.Log('Case 2: {}'.format(self.case2))