Strategy vault / Machine learning
Dynamic Candle Direction Strategy
This strategy determines future candle direction by analyzing the closing price relative to opening price of past N candles. It takes long or short positions based on candle direction signals.

Overview
This strategy determines future candle direction by analyzing the closing price relative to opening price of past N candles. It takes long or short positions based on candle direction signals.
Strategy Logic
The core logic of this strategy is:
Set parameter NUM_CANDLES to determine the number of candles to analyze.
Define function candle_dir to determine direction of a single candle. close>open is bullish, close<open is bearish, close=open is neutral.
Define function count_candles to count number of candles with certain direction in past NUM_CANDLES candles.
Count number of bullish, bearish and neutral candles in past NUM_CANDLES candles, store in ups, dns, neu.
Define indic indicator, its value equals ups-dns plus/minus neu.
Determine long/short entry based on indic indicator.
By analyzing candle direction of a certain number of candles, this strategy estimates probability of future candle direction for trading decisions. NUM_CANDLES controls sample size to adjust strategy sensitivity.
Advantage Analysis
Strategy logic is clear and easy to understand, interpret and verify.
Only candle data is needed, reducing computing cost.
Easy to adjust sensitivity by tuning NUM_CANDLES parameter.
Applicable to all products and timeframes, high adaptability.
Easy to optimize parameters to find best combination.
Risk Analysis
Unable to handle range-bound market, may cause over-trading.
Inappropriate sample period may cause signal lag, NUM_CANDLES needs careful tuning.
Unable to adapt to trend reversal, risk of loss in reversing trend.
Trading cost impact needs consideration to avoid over-trading.
Beware of overfitting in parameter optimization, require multi-market verification.
Optimization Directions
Consider adding stop loss to limit loss.
Combine with trend indicator to avoid counter-trend trading.
Increase sample size or use lower timeframe to improve stability.
Consider multi-market compounding to improve win rate.
Utilize machine learning for automatic parameter optimization.
Conclusion
This strategy determines trade direction by analyzing candle direction, with clear and simple logic. Sensitivity is controllable through parameter tuning. The pros are simplicity, low requirement, and wide adaptability, but some risks exist and further optimization is needed to improve stability. Overall, this strategy provides a simple and practical approach for quantitative trading.
Source Pine Script
/*backtest
start: 2023-09-24 00:00:00
end: 2023-10-24 00:00:00
period: 3h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=3
strategy("Refined CandleCounter Strategy by origo", overlay=true)
// how many candles to count
NUM_CANDLES = 7
// determine candle direction
candle_dir = close > open ? 1 : (round(close-open) == 0 ? 0 : -1)
// return # of candles with a given direction
count_candles(dir, max) =>
count = 0
for i = 0 to max
if candle_dir[i] == dir
count := count + 1
count
ups = count_candles(1, NUM_CANDLES)
dns = count_candles(-1, NUM_CANDLES)
neu = count_candles(0, NUM_CANDLES)
indic = ups-dns
if indic > 0
indic := indic+neu
else
indic := indic-neu
plotarrow(neu, title="UP vs DN")
longCondition = (indic) > 0
shortCondition = (indic) <= 0
strategy.entry("buy", strategy.long, 1, when = longCondition and not shortCondition)
strategy.entry("sell", strategy.short, 1, when = shortCondition and not longCondition)
Published by ChaoZhang on the FMZ strategy square · last modified 2023-10-25. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Dynamic-Candle-Direction-Strategy.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- AI Driven Dynamic Volatility Adaptive Trend Breakout Trading StrategyPine Script · Machine learning · 2d
- AI Powered Volatility Price System Divergence Trading StrategyPine Script · Machine learning · 1h
- Configurable BBRSIAroon Strategy BacktestPine Script · Machine learning · 5m
- Double Decker RSI Trading StrategyPine Script · Machine learning · 1h
- Dual Moving Average Matching Strategy Based on Bollinger BandsPine Script · Machine learning · 1h
- Dynamic Weighted Moving Average Trading StrategyPine Script · Machine learning · 1d