Hello, I am new to QuantConnect, and I am developing my first algorithm. I am trying to use SMA in my execution model to block trading under certain cases. I researched our document for a long time and finally made this version of the execution model. I am so confused and don't think this implementation is ideal, and the prices are different from what I see from other data sources. The code and screenshots are attached. Looking forward to getting some guidance. Thanks!
class MyExecutionModel(ImmediateExecutionModel):
def __init__(self):
self.spy = None
self.spy_sma = None
super().__init__()
def Execute(self, algorithm, targets):
if len(targets) == 0:
return
if self.spy_sma is None and self.spy is None:
self.spy = algorithm.AddEquity("SPY", Resolution.Daily)
self.spy_sma = algorithm.SMA(self.spy.Symbol, 90)
algorithm.WarmUpIndicator(self.spy.Symbol, self.spy_sma)
# Only buy stocks if the 90-day SMA of SPY is upwards
if self.spy_sma.IsReady and self.spy_sma.Current.Value < self.spy.Price:
super().Execute(algorithm, targets)
else:
algorithm.Liquidate()
BoboStone
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!