| Overall Statistics |
|
Total Trades 10001 Average Win 0.01% Average Loss -0.01% Compounding Annual Return -99.998% Drawdown 20.500% Expectancy -0.724 Net Profit -20.485% Sharpe Ratio -25.288 Probabilistic Sharpe Ratio 0.200% Loss Rate 91% Win Rate 9% Profit-Loss Ratio 2.06 Alpha -9.459 Beta 0.387 Annual Standard Deviation 0.371 Annual Variance 0.138 Information Ratio -25.621 Tracking Error 0.374 Treynor Ratio -24.25 Total Fees $12338.08 |
import decimal as d
from datetime import timedelta
import numpy
from datetime import datetime
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(2020, 1, 1) #Set Start Date
self.SetEndDate(2020, 3, 26) #Set End Date
self.SetCash(60000) #Set Strategy Cash
self.AddEquity("SPY", Resolution.Minute).Symbol
self.AddEquity("SPXS", Resolution.Minute).Symbol
self.AddEquity("SDOW", Resolution.Minute).Symbol
self.AddEquity("EDZ", Resolution.Minute).Symbol
self.AddEquity("UDOW", Resolution.Minute).Symbol
self.AddEquity("TQQQ", Resolution.Minute).Symbol
self.AddEquity("SPXL", Resolution.Minute).Symbol
self.AddEquity("DIA", Resolution.Minute).Symbol
self.AddEquity("VOO", Resolution.Minute).Symbol
#Simple moving average
self.sma = {}
equity = self.AddEquity("SPY")
self.sma[equity.Symbol] = self.SMA(equity.Symbol, 60, Resolution.Minute)
self.smas = {}
equity = self.AddEquity("SPY")
self.smas[equity.Symbol] = self.SMA(equity.Symbol, 100, Resolution.Minute)
self.fastest = self.TEMA("DIA", 15, Resolution.Minute)
# create a 13 day exponential moving average
self.fast = self.EMA("VOO", 5, Resolution.Daily)
# create a 48 day exponential moving average
self.slow = self.EMA("VOO", 10, Resolution.Daily)
self.previous = None
self.SetWarmUp(timedelta(days=20))
self.now = datetime.now()
def OnData(self, data):
# wait for our slow ema to fully initialize
if self.IsWarmingUp: return
# only once per day
#if self.previous is not None and self.previous.date() == self.Time.date():
#return
self.TimeRules.Every(TimeSpan.FromMinutes(60))
# define a small tolerance on our checks to avoid bouncing
tolerance = 0.00015
#holdings = self.Portfolio["SPXL", "TQQQ", "UDOW", "SPXS", "SDOW", "EDZ"].Quantity
#if holdings <= 0:
# we only want to go long if we're currently short or flat
if self.fast.Current.Value > self.slow.Current.Value:
# if the fast is greater than the slow, we'll go long
self.SetHoldings("SPXL", .3, True)
self.SetHoldings("TQQQ", .5, True)
self.SetHoldings("UDOW", .2, True)
print("bull")
# we only want to liquidate if we're currently long
# if the fast is less than the slow we'll liquidate our long
if self.slow.Current.Value > self.fast.Current.Value:
self.SetHoldings("SPXS", .3, True)
self.SetHoldings("SDOW", .2, True)
self.SetHoldings("EDZ", .5, True)
print("bear")
self.previous = self.Time