| Overall Statistics |
|
Total Trades 9 Average Win 18.91% Average Loss -2.05% Compounding Annual Return 29.387% Drawdown 14.300% Expectancy 6.676 Net Profit 75.641% Sharpe Ratio 1.405 Probabilistic Sharpe Ratio 66.487% Loss Rate 25% Win Rate 75% Profit-Loss Ratio 9.23 Alpha 0.255 Beta -0.003 Annual Standard Deviation 0.181 Annual Variance 0.033 Information Ratio 0.405 Tracking Error 0.291 Treynor Ratio -72.824 Total Fees $9.00 |
from System.Drawing import Color
class Crossover_Algo(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2018, 7, 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 = "SBUX" # 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 = 50
# 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
# how do I get statistics on the history of this function????
# 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}")