| Overall Statistics |
|
Total Trades 17 Average Win 21.81% Average Loss -8.38% Compounding Annual Return 6.687% Drawdown 33.400% Expectancy 1.702 Net Profit 175.509% Sharpe Ratio 0.507 Probabilistic Sharpe Ratio 1.469% Loss Rate 25% Win Rate 75% Profit-Loss Ratio 2.60 Alpha 0.069 Beta -0.065 Annual Standard Deviation 0.124 Annual Variance 0.015 Information Ratio -0.121 Tracking Error 0.225 Treynor Ratio -0.975 Total Fees $17.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
# create a consolidator for weekly data
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
# # determine where the price is relative to the sma
upSMA = sma_short > sma_long
downSMA = sma_short < sma_long
if (upSMA):
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 (downSMA):
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)
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}")