You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将止损价格设置为订单执行时的Supertrend指标值

固定入场时Supertrend价格为止损的修改方案

当前你代码里的止损会跟随实时Supertrend线变动,要改成固定订单执行那一刻的Supertrend价格,只需要把入场时的Supertrend值存储下来,再用这个固定值作为止损参数即可,修改后的代码如下:

risk_reward_ratio = 2

// 定义变量存储入场时的Supertrend价格,var保证变量不会随bar刷新重置
var float entry_supertrend = na

if (GoLong and close > supertrend and direction < 0)
    entry_price = close
    stop_loss_price = supertrend
    take_profit_price = entry_price + (entry_price - stop_loss_price) * risk_reward_ratio
    // 记录入场瞬间的Supertrend值
    entry_supertrend = supertrend
    strategy.entry("BUY", strategy.long, comment = "BUY")
    // 止损使用固定的入场时Supertrend值,而非实时值
    strategy.exit("TAKE_PROFIT", "BUY", limit=take_profit_price, stop=entry_supertrend, comment="Take Profit/SL")

关键修改说明

  • 用var float entry_supertrend = na声明变量,var关键字让变量仅在脚本初始化时创建,后续不会随每根K线刷新重置,确保只在入场时更新为当时的Supertrend价格。
  • 在入场条件分支内,将supertrend赋值给entry_supertrend,固定住订单执行时的指标价格。
  • strategy.exit的stop参数替换为entry_supertrend,这样止损价就不会再跟随后续的Supertrend线变动,只会在价格跌破入场时的指标线才触发止损。

内容的提问来源于stack exchange,提问作者Saad Hassan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 20:14:58