TradingView固定止损+动态止盈回调策略技术求助
周线级回调交易策略固定止损实现问题
需求概述
开发周线级回调交易策略并在测试器中验证,核心诉求是固定初始止损不随行情变动,当前ChatGPT生成的代码未满足该要求。
原始策略逻辑
- 入场:价格回调至周线10 EMA/30 EMA后,日线收盘价高于4 EMA(high)确认反弹
- 初始止损:本次反弹最低点(摆动低点)下方
- 止盈:周线最近高点下方2倍ATR处
简化日线版规则
- 最近x根K线触碰50 EMA
- 收盘价高于4 EMA(high)
- 止损:最近低点(或50 EMA下方1倍ATR,即Keltner通道下沿)
- 止盈:最近高点下方4倍ATR(近似周线2倍ATR)
核心问题
初始止损需固定,不随行情波动移动;当价格触及止盈或止损时平仓。
原ChatGPT生成代码
//@version=5 strategy("EMA and Chandelier Strategy (Fixed Stop-Loss)", overlay=true) // Input Parameters ema_length50 = input.int(50, title="50 EMA Length") ema_length4 = input.int(4, title="4 EMA Length (Highs)") atr_length = input.int(14, minval=1, title="ATR Length") chandelier_length = input.int(22, minval=1, title="Chandelier Highest High Length") atr_coefficient = input.float(4.0, title="Chandelier ATR Coefficient") lookback_candles = input.int(5, minval=1, title="Lookback Candles for Cross and Stop Loss") // Weekly EMA Parameters ema_length8 = 8 ema_length21 = 21 ema_length34 = 34 ema_length55 = 55 // EMA Calculations ema50 = ta.ema(close, ema_length50) ema4_high = ta.ema(high, ema_length4) ema8 = request.security(syminfo.tickerid, "W", ta.ema(close, ema_length8)) ema21 = request.security(syminfo.tickerid, "W", ta.ema(close, ema_length21)) ema34 = request.security(syminfo.tickerid, "W", ta.ema(close, ema_length34)) ema55 = request.security(syminfo.tickerid, "W", ta.ema(close, ema_length55)) // Chandelier Exit Calculation atr = ta.rma(ta.tr, atr_length) highest_high = ta.highest(high, chandelier_length) chandelier_exit = highest_high - (atr * atr_coefficient) // Entry Conditions crossed_ema50 = ta.lowest(low, lookback_candles) < ema50 and close > ema50 stacked_weekly_emas = ema8 > ema21 and ema21 > ema34 and ema34 > ema55 price_above_ema4 = close > ema4_high price_above_previous_high = close > high[1] long_condition = crossed_ema50 and stacked_weekly_emas and price_above_previous_high // Variables for Stop-Loss var float stop_loss_level = na var int entry_bar_index = na // Entry Logic if (long_condition and strategy.position_size == 0) // New trade strategy.entry("Long", strategy.long, comment="Long Entry") stop_loss_level := ema50 - (1 * atr) // Set stop-loss to 1 ATR below EMA50 entry_bar_index := bar_index // Track the bar index of the entry // Exit Conditions stop_condition = strategy.position_size > 0 and low < stop_loss_level exit_condition = strategy.position_size > 0 and close < chandelier_exit // Manage Position if stop_condition strategy.close("Long", comment="Stop Loss Hit") if exit_condition strategy.close("Long", comment="Chandelier Exit") // Reset Stop-Loss Level After Exit if (strategy.position_size == 0) stop_loss_level := na entry_bar_index := na
问题分析与修正代码
原代码存在两处核心问题:一是添加了与需求无关的周线EMA堆叠条件,二是止盈逻辑采用随行情变动的Chandelier Exit,未实现固定止盈;虽声明了止损变量,但未完全贴合“反弹最低点下方”的止损规则。
以下是符合需求的修正代码:
修正后的Pine Script代码
//@version=5 strategy("固定止损止盈回调策略", overlay=true, default_qty_type=strategy.cash, default_qty_value=10000) // 输入参数 lookback_candles = input.int(5, title="触碰50 EMA的回溯K线数") ema_50_length = input.int(50, title="50 EMA周期") ema_4_high_length = input.int(4, title="4 EMA(High)周期") atr_length = input.int(14, title="ATR周期") stop_atr_multiplier = input.float(1.0, title="止损:50 EMA下方ATR倍数") take_profit_atr_multiplier = input.float(4.0, title="止盈:最近高点下方ATR倍数") // 指标计算 ema_50 = ta.ema(close, ema_50_length) ema_4_high = ta.ema(high, ema_4_high_length) atr = ta.rma(ta.tr, atr_length) // 入场条件:最近x根K线触碰50 EMA,且收盘价高于4 EMA(High) touched_ema50 = ta.lowest(low, lookback_candles) <= ema_50 price_above_ema4 = close > ema_4_high long_condition = touched_ema50 and price_above_ema4 and strategy.position_size == 0 // 固定止损和止盈变量(仅在入场时赋值) var float fixed_stop_loss = na var float fixed_take_profit = na // 入场逻辑 if long_condition // 获取入场前的最近高点、低点 recent_high = ta.highest(high, lookback_candles) recent_low = ta.lowest(low, lookback_candles) // 固定止损:取50 EMA下方1倍ATR 或 最近低点下方(更保守) stop_option1 = ema_50 - stop_atr_multiplier * atr fixed_stop_loss := math.min(stop_option1, recent_low * 0.99) // 固定止盈:最近高点下方4倍ATR fixed_take_profit := recent_high - take_profit_atr_multiplier * atr // 开仓 strategy.entry("多单", strategy.long) label.new(bar_index, low, "入场", color=color.green, textcolor=color.white) // 平仓条件:触碰固定止损或固定止盈 if strategy.position_size > 0 // 止损平仓 if low <= fixed_stop_loss strategy.close("多单", comment="止损离场") label.new(bar_index, low, "止损", color=color.red, textcolor=color.white) // 止盈平仓 if high >= fixed_take_profit strategy.close("多单", comment="止盈离场") label.new(bar_index, high, "止盈", color=color.blue, textcolor=color.white) // 持仓结束后重置变量 if strategy.position_size == 0 fixed_stop_loss := na fixed_take_profit := na // 绘制指标线 plot(ema_50, color=color.orange, title="50 EMA") plot(ema_4_high, color=color.blue, title="4 EMA(High)") // 绘制持仓期间的止损止盈虚线 var line stop_line = na var line tp_line = na if strategy.position_size > 0 stop_line := line.new(bar_index, fixed_stop_loss, bar_index+1, fixed_stop_loss, color=color.red, width=2, style=line.style_dashed) tp_line := line.new(bar_index, fixed_take_profit, bar_index+1, fixed_take_profit, color=color.blue, width=2, style=line.style_dashed) else line.delete(stop_line) line.delete(tp_line)
关键修改说明
- 固定值锁定:在入场时一次性计算
fixed_stop_loss和fixed_take_profit,使用入场Bar的ATR、最近高低点数值,持仓期间不再更新 - 止损规则贴合:提供两种止损选项,取更保守的数值确保止损在反弹低点下方
- 移除冗余条件:删除原代码中与需求无关的周线EMA堆叠规则,聚焦指定入场逻辑
- 可视化优化:添加入场、止损、止盈标签,以及持仓期间的止损止盈虚线,便于测试观察
内容的提问来源于stack exchange,提问作者Jochen Fostie
相关产品推荐
相关产品推荐

