Hello everyone,
I was wondering if I can get your thoughts on something most likely just a simple issue.
The given algorithm is a momentum-based strategy that uses Bollinger Bands, Rate of Change (ROC) indicators, and a few other mechanisms for trading decisions. Here is a summarized description of its trading logic:
Initialization:
- The algorithm starts on January 1, 2023, with $1000 in cash.
- It tracks several equities: SPY, AAPL, MSFT, AMZN, NVDA, TSLA, GOOGL, META, GOOG, AVGO, and ORCL.
- It also tracks three other assets: UUP (US Dollar Index Bullish Fund), TLT (20+ Year Treasury Bond ETF), and GLD (Gold Trust ETF).
- Bollinger Bands with a period of 55 days and a standard deviation of 2 are defined for each equity.
- A ROC indicator with a period of 55 days is defined for each equity and the other three assets.
- A trailing stop risk management model is set with a 5% stop.
- The algorithm has a warm-up period of 55 days to ensure that all indicators are ready.
- Previous close prices of each equity are stored in a rolling window of size 2.
Order Event Handling:
- When an order is filled, the algorithm checks if it was a sell or stop market order. If so, a flag (stop_loss_triggered) is set to true, and the time of the sell (lastSellTime) is recorded.
- If an asset is bought, its buy price is stored for potential profit-taking later.
Trading Logic on New Data:
- The algorithm skips processing if it's still warming up or if it's within a day since the last sell.
- It checks the readiness of data and indicators before proceeding.
- For each equity:
- The conditions of the Bollinger Bands and ROC are checked.
- If the previous close price was less than or equal to the lower Bollinger Band and the current price is greater than the middle Bollinger Band, and the ROC is positive, a buy order is placed for the equity (investing 95% of the portfolio value).
- If the asset's current price is 5% or more above its buy price, it's sold for profit.
- The algorithm identifies the equity with the highest ROC:
- If the portfolio is not invested in any asset and the highest ROC equity is not invested, the portfolio is liquidated, and the equity with the highest ROC is bought (investing 95% of the portfolio value).
- If all equities have a negative ROC:
- The entire portfolio is liquidated.
- Based on the ROC values of UUP, TLT, and GLD, one of them is selected and bought (investing 95% of the portfolio value).
- Specific conditions determine which of UUP, TLT, or GLD to buy.
- If any equity has a positive ROC, then UUP, TLT, and GLD are liquidated (if they are in the portfolio).
Tag Message Generation:
The algorithm generates and attaches informative tag messages to orders. These messages contain details like the previous close, current price, values of the Bollinger Bands, and ROC.The problem:
After examining the trade occurred for the symbol META on 2023-01-24:
The Buy condition for equities, as per the algorithm, is:
- Previous Close (prev_close) ≤ Lower Band (lower_band)
- Current Price (current_price) > Middle Band (middle_band)
- ROC (current_roc) > 0This is the code for the given conditions
Checking Previous Close vs Lower Band:
def Initialize(self):
[...]
self.previous_closes = {symbol: RollingWindow[float](2) for symbol in self.equities}
(other lines of code)
def OnData (self, data):
[...]
# Store the current price for the symbol
self.previous_closes[symbol].Add(current_price)
if self.previous_closes[symbol].Count < 2:
continue
prev_close = self.previous_closes[symbol][1]
# Tagging the values for better visibility
tag_message = f"Prev Close: {prev_close}, Current Price: {current_price}, Lower Band: {lower_band}, Middle Band: {middle_band}, Upper Band: {upper_band}, ROC: {current_roc}"
# Buy conditions
if (prev_close <= lower_band and current_price > middle_band) and current_roc > 0:
if not self.Portfolio[symbol].Invested:
self.SetHoldings(symbol, 0.95, tag_message)
As per the data provided for META on 2023-01-24: (see screenshot below)
- Prev Close is 136.150
- Lower Band is 94.006 The actual data doesn't satisfy this condition since 136.150 > 94.006. Hence, this specific condition isn't met.
Checking Current Price vs Middle Band: Based on the same condition mentioned above:
- Current Price is 143.270
- Middle Band is 117.848 This condition is met because 143.270 > 117.848.
Checking if ROC is Positive:
- ROC is 0.505, which is positive. So, this condition is also met.
Given the conditions and the data, the buy decision for META should not have been made because the previous close was not below or equal to the lower band.
Louis Szeto
Hi Kristofferson
I've checked the issue you've reported using the debugger tool. The first “buy” logic of the above 3 conditions of META in 24/01 is not triggered, but the second logic at line 120:
is being triggered, giving the order (ordered at line 132) which you questioned about. I'm unsure about the trading logic nest of your algorithm, but maybe you'd want to check the ident level of the code below line 116 to avoid some unexpected orders.
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.
Kristofferson V Tandoc
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!