| Overall Statistics |
|
Total Trades 23 Average Win 20.24% Average Loss -4.63% Compounding Annual Return 6.993% Drawdown 31.800% Expectancy 2.418 Net Profit 188.176% Sharpe Ratio 0.55 Probabilistic Sharpe Ratio 2.431% Loss Rate 36% Win Rate 64% Profit-Loss Ratio 4.37 Alpha 0.07 Beta -0.058 Annual Standard Deviation 0.117 Annual Variance 0.014 Information Ratio -0.117 Tracking Error 0.221 Treynor Ratio -1.115 Total Fees $23.00 |
from System.Drawing import Color
class Crossover_Algo(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2005, 1, 1) # the starting date for our backtest
#self.SetEndDate(2020, 2, 1) # the ending date for our backtest (if no date, then it will test up until today)
self.start_value = 5000 # define the amount of starting cash
self.SetCash(self.start_value) # initialize our wallet
self.EQY = "SPY" # define the stock ticker
self.AddEquity(self.EQY, Resolution.Daily) # add a stock to the list of those we want to trade, and how often to pull data
self.starting_price = None
self.spy_portfolio = None
self.period1 = 20
self.period2 = 200
# set up our sma indicators
self.sma_short = self.SMA(self.EQY, self.period1, Resolution.Daily)
self.sma_long = self.SMA(self.EQY, self.period2, Resolution.Daily)
# need to give the indicators data before running the algorithm
self.SetWarmUp(self.period2)
# set up a chart to display our buy and sell dates
self.stockPlot = Chart('Equity')
# self.SPY_candles = Series(self.EQY, SeriesType.Candle)
# self.stockPlot.AddSeries(self.SPY_candles)
self.stockPlot.AddSeries(Series('Buy', SeriesType.Scatter, '$', Color.Green, ScatterMarkerSymbol.Triangle))
self.stockPlot.AddSeries(Series('Sell', SeriesType.Scatter, '$', Color.Red, ScatterMarkerSymbol.TriangleDown))
self.AddChart(self.stockPlot)
# this is for plotting the relative change of each stock and our portfolio value
def OnEndOfDay(self):
current_portfolio = self.Portfolio.TotalPortfolioValue
change_portfolio = (current_portfolio - self.start_value )/ self.start_value
self.Plot("Percent Change","Portfolio Change", change_portfolio)
# # if we haven't gotten the starting price, then get it
if self.starting_price == None:
self.starting_price = self.Securities[self.EQY].Price
start_price = self.starting_price
price = self.Securities[self.EQY].Price
self.change_in_price = (price - start_price)/ start_price
self.Plot("Percent Change", self.EQY + " Change", self.change_in_price)
def OnData(self, data):
# # if the indicators aren't ready, don't do anything
if self.IsWarmingUp: return
daily_low_price = self.Securities[self.EQY].Low
daily_high_price = self.Securities[self.EQY].High
# # extract the current value of each indicator
sma_short = self.sma_short.Current.Value
sma_long = self.sma_long.Current.Value
sma_pct_delta = (sma_short-sma_long)/sma_long
# determine the time to open and close the position
open_signal = sma_short > sma_long
#buy_signal = sma_signal < -0.01
close_signal = sma_short < sma_long
#close_signal = sma_signal > 0.02
if (open_signal):
if not self.Portfolio[self.EQY].Invested:
self.SetHoldings(self.EQY, 1) # if the price is above the 20-day moving average, buy the stock
self.Plot("Equity", 'Buy', self.Securities[self.EQY].Price)
elif (close_signal):
if self.Portfolio[self.EQY].Invested:
self.SetHoldings(self.EQY, 0) # otherwise exit the position
self.Plot("Equity", 'Sell', self.Securities[self.EQY].Price)
# plotting stuff
# self.SPY_candles.AddPoint(self.Time + timedelta(minutes=1), self.Securities[self.EQY].Open)
# self.SPY_candles.AddPoint(self.Time + timedelta(minutes=2), self.Securities[self.EQY].High)
# self.SPY_candles.AddPoint(self.Time + timedelta(minutes=3), self.Securities[self.EQY].Low)
# self.SPY_candles.AddPoint(self.Time + timedelta(minutes=4), self.Securities[self.EQY].Close)
self.Plot("Equity",self.EQY,self.Securities[self.EQY].Price)
self.Plot("Equity","SMA_short", sma_short)
self.Plot("Equity","SMA_long", sma_long)
self.Plot("sma pct delta","sma_pct_delta", sma_pct_delta)
def OnOrderEvent(self, orderEvent):
if orderEvent.Status != OrderStatus.Filled:
return
self.Log(f"Order Executed! Time: {self.Time}, Price: {self.Securities[self.EQY].Price}, SMA: {self.sma_short.Current.Value}")