Hi I'm going through the bootcamp 10. Below is the script
spread = self.pair[1].Price - self.pair[0].Price
self.spreadMean.Update(algorithm.Time, spread)
self.spreadStd.Update(algorithm.Time, spread)
upperthreshold = self.spreadMean.Current.Value + self.spreadStd.Current.Value
lowerthreshold = self.spreadMean.Current.Value - self.spreadStd.Current.Value
if spread > upperthreshold:
return Insight.Group(
[
Insight.Price(self.pair[0].Symbol, self.period, InsightDirection.Up), Insight.Price(self.pair[1].Symbol, self.period, InsightDirection.Down)
])
if spread < lowerthreshold:
return Insight.Group(
[
Insight.Price(self.pair[0].Symbol, self.period, InsightDirection.Down), Insight.Price(self.pair[1].Symbol, self.period, InsightDirection.Up)
])
return []
def OnSecuritiesChanged(self, algorithm, changes):
self.pair = [x for x in changes.AddedSecurities]
#1. Call for 500 hours of history data for each symbol in the pair and save to the variable history
history=algorithm.History([x.Symbol for x in self.pair], 500)
#2. Unstack the Pandas data frame to reduce it to the history close price
history=history.close.unstack(level=0)
#3. Iterate through the history tuple and update the mean and standard deviation with historical data
for tuple in history.itertuples():
self.spreadMean.Update(tuple[0], tuple[2]-tuple[1])
self.spreadStd.Update(tuple[0], tuple[2]-tuple[1])
- In the PairTradingAlphaModel constructor, SimpleMovingAverage doesn't require any asset symbol. I don't know what quantities are moving-averaged.
- In the Update method of the PairTradingAlphaModel class, it returns empty list. Is this OK?
Mia Alissi
In the PairTradingAlphaModel constructor, the SimpleMovingAverage is being used to calculate the moving average of the spread between the prices of two securities, not the price of a single security. The spread is calculated as
spread = Pair[1].Price - Pair[0].Price
and then this spread value is used to update the SimpleMovingAverage (spreadMean.Update(algorithm.Time, spread)
). So, the quantities being moving-averaged are these spread values, not the prices of individual assets. This is a common technique in pairs trading strategies, where the spread between the prices of two correlated securities is often of interest (source).In the Update method of the PairTradingAlphaModel class, returning an empty list is perfectly fine. This method is expected to return a list of insights, which are predictions about future price movements. If the conditions for generating an insight are not met (i.e., the spread is not greater than the upper threshold and not less than the lower threshold), then no insights are generated, and an empty list is returned. This simply means that at the current time step, the model does not have any predictions about future price movements (source).
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.
Uhahahaha
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!