Hi everyone, so the basic premise of what this strategy is supposed to be doing is this:
On days SPY is down, buy stocks that are up at 9:45am, and sell everything at 3:45pm. But for some reason QuantConnect buys and sells stock at exact same price? The quote is the same. My code is attached below, I'm hoping someone can provide insight on what I'm doing wrong. Thank you.
"""
Buy 20 stocks showing strength vs SPY. Buy them. Liquidate at EOD
"""
class JumpingLightBrownScorpion(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2018, 1, 1) # Set Start Date
self.SetCash(100000) # Set Cash
self.stocks = ["AAPL", "MSFT"] # stocks I'm interested in
# Dictionary to hold Symbol Data
self.symbolData = {}
for stock in self.stocks:
# Add equity data
symbol = self.AddEquity(stock, Resolution.Daily).Symbol
# Create symbol data for respective symbol
self.symbolData[symbol] = SymbolData(self, symbol)
self.spy = self.AddEquity("SPY", Resolution.Minute).Symbol # intialize SPY
self.SetWarmUp(200, Resolution.Daily) # warm up indicators
self.yest_close = self.SMA(self.spy, 1, Resolution.Daily, Field.Close) # set SMA with close price as field
def OnData(self, data):
if self.IsWarmingUp or not self.yest_close.IsReady or not len(data.Bars) > 0: # if indicators warming up, not ready, or no data bars then stop
return
# Make sure indicators and rolling windows are ready
for symbol in self.symbolData.values():
if not symbol.IsReady:
return
if self.Time.hour == 9 and self.Time.minute == 45: # if it's 9:45am eastern
price = self.Securities[self.spy].Price # set price to SPYs price
yest_close = self.yest_close.Current.Value # set yesterday's closing price to variable
if price < yest_close:# If SPY fell from yesterday,
selected = []
for symbol, value in self.symbolData.items():
# If the SMA today is higher than SMA yesterday
sma_value = str(value.smaWindow[1])
sma_value = float((sma_value.split("-")[3]).strip())
if self.Securities[symbol].Price > sma_value:
selected.append(symbol)
for stock in selected:
self.SetHoldings(stock,1/len(selected))
if self.Time.hour == 15 and self.Time.minute == 45: # if it's 3:45pm eastern, liquidate portfolio
self.Liquidate()
class SymbolData:
def __init__(self, algorithm, symbol):
self.algorithm = algorithm
self.symbol = symbol
# Define our indicator
self.sma = algorithm.SMA(symbol, 1, Resolution.Daily, Field.Close)
# Define our rolling window to hold indicator points
self.smaWindow = RollingWindow[IndicatorDataPoint](2)
# Set our event handler
self.sma.Updated += self.OnSMAUpdated
def OnSMAUpdated(self, sender, updated):
# Add updated indicator data to rolling window
self.smaWindow.Add(updated)
@property
def IsReady(self):
return self.sma.IsReady and self.smaWindow.IsReady
Louis Szeto
Hi Arsal
The trading equities (AAPL, MSFT) are subscribed in Daily Resolution. So it can only be traded when the daily bar is consolidated, which makes intra-day trading in the same stale price. We recommend using Resolution.Minute since the algorithm is minute-sensitive.
Best
Louis
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Arsal Khan
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!