Hello everyone,
Posting my simple work on a basic strategy for swing trading. Feel free to modify or provide feedback in any way, shape or form.
The strategy focuses on momentum trading, primarily driven by the Rate of Change (ROC) indicator while using Bollinger Bands (BB) as additional conditions for entry points. The ROC is used to measure the strength of momentum for each stock, with the strategy preferring to invest in the stock with the highest positive ROC. Bollinger Bands help determine entry points within a certain range above the middle band, but below the upper band.
There is also a diversification into different asset classes (UUP for US dollar strength, TLT for long-term treasuries, and GLD for gold) based on momentum, suggesting a multi-asset adaptive strategy that could be beneficial in different market conditions. The 5% trailing stop and explicit profit-taking rules are risk management measures to protect gains and limit losses.
Here's a breakdown of the strategy:
- Initialization (def Initialize(self)):
- The strategy starts on January 1, 2014, with a starting capital of $100,000. Rationale: I figured the market conditions change so often that going back past a decade would be introduce too many outliers (than it already does) that may not be representative of the current or future market conditions.
- A trailing stop risk management model is set with a 5% stop loss.
- A list of 11 equities (top 10 equities in SP500) and 3 other securities (UUP, TLT, GLD) are added with daily resolution.
- Bollinger Bands (BB) and Rate of Change (ROC) indicators are defined for the equities with parameters presumably set from external input (self.GetParameter()). The periods for BB and ROC indicators are 13 and 37 respectively. The values were obtained using the optimization tool based on the highest profit %.
- A warm-up period of 60 days is set to prime the indicators before live trading begins.
- The SPY ETF is set as the benchmark for the strategy.
- The algorithm also tracks the last sell time and whether a stop loss has been triggered.
- Order Event Handling (def OnOrderEvent(self, orderEvent)):
- Records the fill price and purchase date when a buy order is filled.
- Removes the asset from the purchase date tracking and sets a stop loss flag if a sell or stop-market order is filled.
- Data Handling (def OnData(self, data)):
- The strategy will not trade during the warm-up period or for 1 day after the last sell event.
- The ROC and Bollinger Band indicators are checked to be ready before proceeding with any trading logic.
- If an equity's price is above its middle Bollinger Band but below the upper band and has the highest positive ROC value among all equities, the algorithm may decide to buy this equity.
- If the equity with the highest positive ROC is already bought, it will not be repurchased unless a stop loss was triggered previously.
- If an equity has been held for 10 days or more, it is sold.
- There is a profit-taking condition where if the current price is 5% higher than the buy price, the equity is sold.
- If all equities have a negative ROC, the algorithm liquidates all equity positions and buys UUP, TLT, or GLD based on their positive ROC values (with a preference hierarchy).
Your time and input are greatly appreciated.
Please see backtest attached.
Mia Alissi
Hello Kristofferson,
Your strategy seems well thought out and it's great to see you're using a combination of indicators and risk management techniques.
From the information you've provided, it seems you've already implemented most of the strategy. However, I can provide some guidance on how to implement some of the features you've mentioned based on the information extracts I have.
You can add equities with daily resolution like this:
For the Bollinger Bands (BB) and Rate of Change (ROC) indicators, you can use the helper methods provided by the
QCAlgorithm
class. Here's an example of how to create these indicators:OnData
method. Here's an example of how to check if the indicators are ready and access their values:Please note that these are just examples and you'll need to adapt them to fit your strategy. Also, I don't have information on how to implement some of the features you've mentioned, such as the trailing stop risk management model, the optimization tool, and the stop loss flag.
For more information, you can refer to the QuantConnect documentation and the QuantConnect tutorials.
I hope this helps and I wish you the best of luck with your strategy!
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
Implemented SPY ETF constituents filtering top 20 by weight with dynamic universe selection.
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!