请求修改TradingView RSI策略指标 新增百分比止盈止损功能
带百分比止盈止损的RSI策略脚本修改版
以下是满足需求的修改后脚本,核心改动包括:
- 添加可自定义的止盈/止损百分比参数,方便调整风险收益比
- 针对多单/空单分别设置对应逻辑:多单止损为入场价下跌指定百分比,止盈为入场价上涨指定百分比;空单则反向设置
//@version=5 strategy("RSI Strategy with Percent TP/SL", overlay=true) // RSI核心参数 length = input.int(14, title="RSI周期") overSold = input.int(30, title="超卖阈值") overBought = input.int(70, title="超买阈值") // 止盈止损百分比参数 long_stop_pct = input.float(1.0, title="多单止损百分比(%)", minval=0.1) long_tp_pct = input.float(5.0, title="多单止盈百分比(%)", minval=0.1) short_stop_pct = input.float(1.0, title="空单止损百分比(%)", minval=0.1) short_tp_pct = input.float(5.0, title="空单止盈百分比(%)", minval=0.1) price = close vrsi = ta.rsi(price, length) co = ta.crossover(vrsi, overSold) cu = ta.crossunder(vrsi, overBought) if (not na(vrsi)) if (co) strategy.entry("RsiLE", strategy.long, comment="RsiLE") // 设置多单止盈止损 strategy.exit("Exit Long", "RsiLE", stop=strategy.entry_price * (1 - long_stop_pct/100), limit=strategy.entry_price * (1 + long_tp_pct/100)) if (cu) strategy.entry("RsiSE", strategy.short, comment="RsiSE") // 设置空单止盈止损 strategy.exit("Exit Short", "RsiSE", stop=strategy.entry_price * (1 + short_stop_pct/100), limit=strategy.entry_price * (1 - short_tp_pct/100))
关键逻辑说明
- 参数配置:所有参数都通过
input函数暴露在编辑器设置面板,可随时调整 - 入场逻辑:保留原脚本的RSI交叉信号(上穿超卖做多,下穿超买做空)
- 止盈止损计算:
- 多单:止损价 = 入场价 × (1 - 止损百分比/100),止盈价 = 入场价 × (1 + 止盈百分比/100)
- 空单:止损价 = 入场价 × (1 + 止损百分比/100),止盈价 = 入场价 × (1 - 止盈百分比/100)
- 订单关联:通过
strategy.exit的第二个参数关联对应的入场订单ID,确保每个入场单对应独立的止盈止损规则
内容的提问来源于stack exchange,提问作者Amer
相关产品推荐
相关产品推荐

