| Overall Statistics |
|
Total Trades 60 Average Win 1.49% Average Loss -0.60% Compounding Annual Return 19.219% Drawdown 6.400% Expectancy 0.484 Net Profit 9.159% Sharpe Ratio 1.402 Probabilistic Sharpe Ratio 58.943% Loss Rate 57% Win Rate 43% Profit-Loss Ratio 2.46 Alpha 0.211 Beta -0.161 Annual Standard Deviation 0.118 Annual Variance 0.014 Information Ratio -0.629 Tracking Error 0.184 Treynor Ratio -1.03 Total Fees $60.00 Estimated Strategy Capacity $110000000.00 Lowest Capacity Asset ABT R735QTJ8XC9X |
class UnicornLove(QCAlgorithm):
# This strategy will look at 3 days worth of price data
# If today's open price is higher than the average of (yesterday's high price and the prior day's high price) and...
# Today's close price is greater than yesterday's close price and...
# The average volume for the last 3 days is greater than the average volume for the last 6 days and...
# The RSI is lower than 75
# We will setHoldings and take profit after 7% in our favor. Stop loss at 2%.
def Initialize(self):
self.SetStartDate(2021, 1, 1)
self.SetEndDate(2021, 7, 1)
self.SetCash(100000)
# Define custom set of stocks here:
self.a = 'PG' #10%
self.b = 'GOOG' #30%
self.c = 'MSFT' #20%
self.d = 'ABT' #15%
self.e = 'AMZN' #25%
self.StocksSetA = [self.a, self.b, self.c, self.d, self.e]
self.AddEquity(self.a, Resolution.Daily)
self.AddEquity(self.b, Resolution.Daily)
self.AddEquity(self.c, Resolution.Daily)
self.AddEquity(self.d, Resolution.Daily)
self.AddEquity(self.e, Resolution.Daily)
# Optional set of custom stocks here:
#self.never = 'FB'
#self.gonna = 'TWTR'
#self.let = 'AMZN'
#self.you = 'MSFT'
#self.down = 'GOOG'
#self.StocksSetB = [self.never, self.gonna, self.let, self.you, self.down]
#self.AddEquity(self.never, Resolution.Daily)
#self.AddEquity(self.gonna, Resolution.Daily)
#self.AddEquity(self.let, Resolution.Daily)
#self.AddEquity(self.you, Resolution.Daily)
#self.AddEquity(self.down, Resolution.Daily)
# Aim to benchmark against the S&P500 index
self.SetBenchmark('SPY')
# Adding an RSI indicator
self.a_rsi = self.RSI(self.a, 14)
self.b_rsi = self.RSI(self.b, 14)
self.c_rsi = self.RSI(self.c, 14)
self.d_rsi = self.RSI(self.d, 14)
self.e_rsi = self.RSI(self.e, 14)
# Adding MACD indicator
#self.a_macd = self.MACD(self.a, 12, 26, 9, MovingAverageType.Exponential, Resolution.Daily)
#self.b_macd = self.MACD(self.b, 12, 26, 9, MovingAverageType.Exponential, Resolution.Daily)
#self.c_macd = self.MACD(self.c, 12, 26, 9, MovingAverageType.Exponential, Resolution.Daily)
#self.d_macd = self.MACD(self.d, 12, 26, 9, MovingAverageType.Exponential, Resolution.Daily)
#self.e_macd = self.MACD(self.e, 12, 26, 9, MovingAverageType.Exponential, Resolution.Daily)
def OnData(self, data):
StocksA = self.StocksSetA
for element in StocksA:
running_data = self.History([element], 6, Resolution.Daily)
initial_close = running_data.close[3]
yesterday_close = running_data.close[4]
today_close = running_data.close[5]
initial_open = running_data.open[3]
yesterday_open = running_data.open[4]
today_open = running_data.open[5]
initial_high = running_data.high[3]
yesterday_high = running_data.high[4]
today_high = running_data.high[5]
initial_volume = running_data.volume[0:6].mean()
recent_volume = running_data.volume[3:6].mean()
if self.a_rsi.IsReady and self.b_rsi.IsReady and self.c_rsi.IsReady and self.d_rsi.IsReady and self.e_rsi.IsReady:
#self.a_macd.IsReady and self.b_macd.IsReady and self.c_macd.IsReady and self.d_macd.IsReady and self.e_macd.IsReady:
# Testing Code
if not self.Portfolio[StocksA[0]].Invested and \
today_open > (yesterday_high + initial_high) / 2 and \
today_close > yesterday_close and \
recent_volume > initial_volume and \
self.a_rsi.Current.Value <= 75:
#self.a_macd.Current.Value > self.a_macd.Signal.Current.Value:
self.SetHoldings([PortfolioTarget(StocksA[0], 0.20)])
self.a_entry_price = data[StocksA[0]].Close
#self.entry_prices.append(self.entry_price)
# We can only use the above if we're always buying and appending into array in order. Not likely.
if not self.Portfolio[StocksA[1]].Invested and \
today_open > (yesterday_high + initial_close) / 2 and \
today_close > yesterday_close and \
recent_volume > initial_volume and \
self.b_rsi.Current.Value <= 75:
#self.b_macd.Current.Value > self.b_macd.Signal.Current.Value:
self.SetHoldings([PortfolioTarget(StocksA[1], 0.20)])
self.b_entry_price = data[StocksA[1]].Close
if not self.Portfolio[StocksA[2]].Invested and \
today_open > (yesterday_high + initial_close) / 2 and \
today_close > yesterday_close and \
recent_volume > initial_volume and \
self.c_rsi.Current.Value <= 75:
#self.c_macd.Current.Value > self.c_macd.Signal.Current.Value:
self.SetHoldings([PortfolioTarget(StocksA[2], 0.20)])
self.c_entry_price = data[StocksA[2]].Close
if not self.Portfolio[StocksA[3]].Invested and \
today_open > (yesterday_high + initial_close) / 2 and \
today_close > yesterday_close and \
recent_volume > initial_volume and \
self.d_rsi.Current.Value <= 75:
#self.d_macd.Current.Value > self.d_macd.Signal.Current.Value:
self.SetHoldings([PortfolioTarget(StocksA[3], 0.20)])
self.d_entry_price = data[StocksA[3]].Close
if not self.Portfolio[StocksA[4]].Invested and \
today_open > (yesterday_high + initial_close) / 2 and \
today_close > yesterday_close and \
recent_volume > initial_volume and \
self.e_rsi.Current.Value <= 75:
#self.e_macd.Current.Value > self.e_macd.Signal.Current.Value:
self.SetHoldings([PortfolioTarget(StocksA[4], 0.20)])
self.e_entry_price = data[StocksA[4]].Close
# I will close the position if the market moves 2% in the wrong direction
# I will take profit after the market moves 7% in my favor
if self.Portfolio[StocksA[0]].Invested:
if data[StocksA[0]].Close >= 1.07 * self.a_entry_price:
self.Liquidate(StocksA[0])
elif data[StocksA[0]].Close <= 0.98 * self.a_entry_price:
self.Liquidate(StocksA[0])
if self.Portfolio[StocksA[1]].Invested:
if data[StocksA[1]].Close >= 1.07 * self.b_entry_price:
self.Liquidate(StocksA[1])
elif data[StocksA[1]].Close <= 0.98 * self.b_entry_price:
self.Liquidate(StocksA[1])
if self.Portfolio[StocksA[2]].Invested:
if data[StocksA[2]].Close >= 1.07 * self.c_entry_price:
self.Liquidate(StocksA[2])
elif data[StocksA[2]].Close <= 0.98 * self.c_entry_price:
self.Liquidate(StocksA[2])
if self.Portfolio[StocksA[3]].Invested:
if data[StocksA[3]].Close >= 1.07 * self.d_entry_price:
self.Liquidate(StocksA[3])
elif data[StocksA[3]].Close <= 0.98 * self.d_entry_price:
self.Liquidate(StocksA[3])
if self.Portfolio[StocksA[4]].Invested:
if data[StocksA[4]].Close >= 1.07 * self.e_entry_price:
self.Liquidate(StocksA[4])
elif data[StocksA[4]].Close <= 0.98 * self.e_entry_price:
self.Liquidate(StocksA[4])
def OnEndOfAlgorithm(self):
self.Debug('Total stock value is ' + str(self.Portfolio.TotalPortfolioValue))
self.Debug('Total unrealized profit is ' + str(self.Portfolio.TotalUnrealizedProfit))