Overall Statistics
Total Trades
13
Average Win
10.45%
Average Loss
-2.14%
Compounding Annual Return
40.539%
Drawdown
21.100%
Expectancy
1.945
Net Profit
28.179%
Sharpe Ratio
1.146
Loss Rate
50%
Win Rate
50%
Profit-Loss Ratio
4.89
Alpha
-0.122
Beta
25.916
Annual Standard Deviation
0.344
Annual Variance
0.118
Information Ratio
1.089
Tracking Error
0.344
Treynor Ratio
0.015
Total Fees
$41.80
import clr
clr.AddReference("System")
clr.AddReference("QuantConnect.Algorithm")
clr.AddReference("QuantConnect.Indicators")
clr.AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
import decimal as d
import math
import numpy as np
import pandas as pd
import statistics
from datetime import datetime, timedelta

class MovingAverageCrossAlgorithm(QCAlgorithm):

    def Initialize(self):
        '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

        self.SetStartDate(2017,9,1) #Set Start Date
        self.SetEndDate(2018,3,12)   #Set End Date
        self.SetCash(60000)             #Set Strategy Cash
        
        # Find more symbols here: http://quantconnect.com/data
        self.spy = self.AddEquity("SPY", Resolution.Minute).Symbol
        self.vxx = self.AddEquity("VXX", Resolution.Minute).Symbol
        #self.gld = self.AddEquity("GLD", Resolution.Minute).Symbol
        
        #SetWarmup
        self.SetWarmup(TimeSpan.FromDays(75))
        
        #Run 1x/Hr
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(10, 0),        
            Action(self.EveryDay))
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(11, 0),        
            Action(self.EveryDay))
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(12, 0),        
            Action(self.EveryDay))
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(13, 0),        
            Action(self.EveryDay))
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(14, 0),        
            Action(self.EveryDay))
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(15, 0),        
            Action(self.EveryDay))
            
        # create a 21 day exponential moving average
        self.SPY = self.EMA("SPY", 18, Resolution.Daily);
        
        self.previous = None
    
    def EveryDay(self):

        #Check to see if warmed SetWarmup
        if self.IsWarmingUp: return
    
        #Define a small tolerance on our checks to avoid bouncing
        tolerance = 0.004;
        
        #Identify if we hold the security or not
        SPYholdings = self.Portfolio["SPY"].Quantity
        VXXholdings = self.Portfolio["VXX"].Quantity
        
        #SPY
        if SPYholdings <= 60 and VXXholdings >= 0 and self.Securities["SPY"].Price > self.SPY.Current.Value * d.Decimal(1 + tolerance):
            self.Liquidate("VXX")
            #self.Liquidate("GLD")
            self.SetHoldings("SPY", 1.0)
            
        if SPYholdings >= 0 and VXXholdings <= 0 and self.Securities["SPY"].Price < self.SPY.Current.Value * d.Decimal(1 - tolerance):
            self.Liquidate("SPY")
            self.SetHoldings("VXX", 0.6)
            #self.SetHoldings("GLD", 0.4)
            
        self.previous = self.Time