Last N Candle Reverse Logic Strategy
Overview
The main idea of this strategy is to determine long or short based on the color of the last N candles. If the last N candles are green, go long; if the last N candles are red, go short. The unique part is the addition of an "inverse logic" parameter that can reverse the original logic. When the "inverse logic" parameter is true, the last N green candles will go short, and the last N red candles will go long.
Strategy Principle
This strategy mainly relies on the following important parameters:
- numCandlesToCheck: Used to specify the number of candles to check
- numCandlesToExit: Specifies the number of candles after opening position that needs to exit
- inverseLogic: The inverse logic parameter. When true, the original long and short logic is reversed
The key logic is to traverse the last numCandlesToCheck candles through a for loop, and count the consecutive green and red candles in real time. If consecutive red candles ≥numCandlesToCheck, mark lastNCandlesRed as true. If consecutive green candles ≥numCandlesToCheck, mark lastNCandlesGreen as true.
When lastNCandlesGreen is true, go long if inverseLogic is false, otherwise go short. On the contrary, when lastNCandlesRed is true, go short if inverseLogic is false, otherwise go long.
No matter long or short, the barsSinceEntry counter will be reset to 0 after opening position. When barsSinceEntry is greater than or equal to numCandlesToExit, the current position will be closed.
Advantage Analysis
This is an interesting strategy that uses candle color to make decisions, with an “inverse logic” parameter that can flexibly adjust the long and short logic. The main advantages are:
- The idea is novel and can form reverse investment against market common logic
- The code is clear and concise, easy to understand and modify
- Can find the optimal parameter combination by adjusting parameters
- No matter the market condition, this strategy can continue to run and generate signals
Risk Analysis
There are also some risks to note for this strategy:
- Candle color cannot fully represent market condition, risk of tracking incorrect signal exists
- Unable to determine optimal value for numCandlesToCheck
- Unable to determine optimal value for numCandlesToExit
- Improper inverse logic parameter may amplify losses
- Unable to effectively control single stop loss
To address these risks, the following measures can be adopted for control and optimization:
- Increase other filters to avoid incorrect signals, e.g. determine trend on higher timeframe
- Traverse different parameter values to find optimal parameter combination
- Add stop loss mechanism to control single loss
- Verify effectiveness of inverse logic parameter
Optimization Directions
The main optimization directions for this strategy are:
- Increase orderbook condition to avoid being trapped
- Optimize the values of numCandlesToCheck and numCandlesToExit
- Add trend indicator on higher timeframe to filter false signal
- Add stop loss and take profit
- Backtest on different products to verify effectiveness
- Compare return between original and inverted logic
Conclusion
The overall idea of this strategy is clear and easy to understand, generating trading signals simply based on candle color determination. Adjusting parameters can form rich combination variations for optimization targeting different market environments and products. Also need to pay attention to some potential risks and take necessary measures to control them. By continuously enriching strategy content, this strategy can become a valuable one to keep optimizing for long term trading.
- 1

