Strategy vault / Machine learning
Advanced Dynamic Trailing Stop with Risk Reward Targeting Strategy
This strategy is an advanced trading system that combines dynamic trailing stops, risk-reward ratios, and RSI extreme exits. It identifies specific patterns (parallel bar patterns and pin bar patterns) for trade…

Overview
This strategy is an advanced trading system that combines dynamic trailing stops, risk-reward ratios, and RSI extreme exits. It identifies specific patterns (parallel bar patterns and pin bar patterns) for trade entry, while utilizing ATR and recent lows for dynamic stop loss placement, and determines profit targets based on preset risk-reward ratios. The system also incorporates an RSI-based market overbought/oversold exit mechanism.
Strategy Principles
The core logic includes several key components:
- Entry signals based on two patterns: parallel bar pattern (big bullish bar following a big bearish bar) and double pin bar pattern.
- Dynamic trailing stops using ATR multiplier adjusted to recent N-bar lows, ensuring stop loss levels adapt to market volatility.
- Profit targets set based on fixed risk-reward ratios, calculated using the risk value (R) for each trade.
- Position sizing dynamically calculated based on fixed risk amount and per-trade risk value.
- RSI extreme exit mechanism triggers position closure at market extremes.
Strategy Advantages
- Dynamic Risk Management: Stop loss levels dynamically adjust to market volatility through ATR and recent lows combination.
- Precise Position Control: Position sizing based on fixed risk amount ensures consistent risk per trade.
- Multi-dimensional Exit Mechanism: Combines trailing stops, fixed profit targets, and RSI extremes.
- Flexible Trading Direction: Options for long-only, short-only, or bi-directional trading.
- Clear Risk-Reward Setup: Predetermined risk-reward ratios define clear profit targets for each trade.
Strategy Risks
- Pattern Recognition Accuracy Risk: Potential false identification of parallel bars and pin bars.
- Slippage Risk in Stop Loss: May face significant slippage in volatile markets.
- Premature RSI Exit: Could lead to early exits in strong trend markets.
- Fixed Risk-Reward Ratio Limitations: Optimal risk-reward ratios may vary across market conditions.
- Parameter Optimization Overfitting Risk: Multiple parameter combinations may lead to over-optimization.
Strategy Optimization Directions
- Entry Signal Enhancement: Add more pattern confirmation indicators like volume and trend indicators.
- Dynamic Risk-Reward Ratio: Adjust risk-reward ratios based on market volatility.
- Intelligent Parameter Adaptation: Introduce machine learning algorithms for dynamic parameter optimization.
- Multi-timeframe Confirmation: Add signal confirmation mechanisms across multiple timeframes.
- Market Environment Classification: Apply different parameter sets for different market conditions.
Summary
This is a well-designed trading strategy that combines multiple mature technical analysis concepts to build a complete trading system. The strategy's strengths lie in its comprehensive risk management system and flexible trading rules, while attention needs to be paid to parameter optimization and market adaptability. Through the suggested optimization directions, there is room for further improvement of the strategy.
Source Pine Script
/*backtest
start: 2024-11-10 00:00:00
end: 2024-12-09 08:00:00
period: 2h
basePeriod: 2h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ZenAndTheArtOfTrading | www.TheArtOfTrading.com
// @version=5
strategy("Trailing stop 1", overlay=true)
// Get user input
int BAR_LOOKBACK = input.int(10, "Bar Lookback")
int ATR_LENGTH = input.int(14, "ATR Length")
float ATR_MULTIPLIER = input.float(1.0, "ATR Multiplier")
rr = input.float(title="Risk:Reward", defval=3)
// Basic definition
var float shares=na
risk = 1000
var float R=na
E = strategy.position_avg_price
// Input option to choose long, short, or both
side = input.string("Long", title="Side", options=["Long", "Short", "Both"])
// RSI exit option
RSIexit = input.string("Yes", title="Exit at RSI extreme?", options=["Yes", "No"])
RSIup = input(75)
RSIdown = input(25)
// Get indicator values
float atrValue = ta.atr(ATR_LENGTH)
// Calculate stop loss values
var float trailingStopLoss = na
float longStop = ta.lowest(low, BAR_LOOKBACK) - (atrValue * ATR_MULTIPLIER)
float shortStop = ta.highest(high, BAR_LOOKBACK) + (atrValue * ATR_MULTIPLIER)
// Check if we can take trades
bool canTakeTrades = not na(atrValue)
bgcolor(canTakeTrades ? na : color.red)
//Long pattern
//Two pin bar
onepinbar = (math.min(close,open)-low)/(high-low)>0.6 and math.min(close,open)-low>ta.sma(high-low,14)
twopinbar = onepinbar and onepinbar[1]
notatbottom = low>ta.lowest(low[1],10)
// Parallel
bigred = (open-close)/(high-low)>0.8 and high-low>ta.sma(high-low,14)
biggreen = (close-open)/(high-low)>0.8 and high-low>ta.sma(high-low,14)
parallel = bigred[1] and biggreen
atbottom = low==ta.lowest(low,10)
// Enter long trades (replace this entry condition)
longCondition = parallel
if (longCondition and canTakeTrades and strategy.position_size == 0 and (side == "Long" or side == "Both"))
R:= close-longStop
shares:= risk/R
strategy.entry("Long", strategy.long,qty=shares)
// Enter short trades (replace this entry condition)
shortCondition = parallel
if (shortCondition and canTakeTrades and strategy.position_size == 0 and (side == "Short" or side == "Both"))
R:= shortStop - close
shares:= risk/R
strategy.entry("Short", strategy.short,qty=shares)
// Update trailing stop
if (strategy.position_size > 0)
if (na(trailingStopLoss) or longStop > trailingStopLoss)
trailingStopLoss := longStop
else if (strategy.position_size < 0)
if (na(trailingStopLoss) or shortStop < trailingStopLoss)
trailingStopLoss := shortStop
else
trailingStopLoss := na
// Exit trades with trailing stop
strategy.exit("Long Exit", "Long", stop=trailingStopLoss, limit = E + rr*R )
strategy.exit("Short Exit", "Short", stop=trailingStopLoss, limit = E - rr*R)
//Close trades at RSI extreme
if ta.rsi(high,14)>RSIup and RSIexit == "Yes"
strategy.close("Long")
if ta.rsi(low,14)<RSIdown and RSIexit == "Yes"
strategy.close("Short")
// Draw stop loss
plot(trailingStopLoss, "Stop Loss", color.red, 1, plot.style_linebr)Published by ChaoZhang on the FMZ strategy square · last modified 2024-12-11. 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/Advanced-Dynamic-Trailing-Stop-with-Risk-Reward-Targeting-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
- High Frequency Hybrid Technical Analysis Quantitative StrategyPine Script · Machine learning · 15m
- Multi Indicator Dynamic Adaptive Quantitative Trading StrategyPine Script · Machine learning · 1h
- kNN based Trend Following StrategyPine Script · Machine learning · 1h
- Dual Indicator Stochastic RSI and EMA Trading StrategyPine Script · Machine learning · 1d