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:

  1. 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.
  2. 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.
  3. 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.