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

如何在Pine Script 3分钟策略中实现基于入场价的百分比止损?

基于入场价的固定百分比止损实现方案

以下是修改后的完整Pine Script v5代码,已添加基于入场价的固定百分比止损功能:

//@version=5
strategy("3 Minute", overlay=true)

// 添加固定止损百分比参数
stop_pct = input.float(1.0, title="固定止损百分比(%)", minval=0.1, maxval=10.0, tooltip="多头止损=入场价*(1-百分比/100),空头止损=入场价*(1+百分比/100)")

// 动量指标
source = close
length = input.int(20, minval=1)
mom = ta.mom(source, length)

// 布林带指标
mult = input.float(2.0, minval=0.001, maxval=50)
basis = ta.sma(source, length)
dev = mult * ta.stdev(source, length)
upper = basis + dev
lower = basis - dev
buyEntry = ta.crossover(source, lower)
sellEntry = ta.crossunder(source, upper)

// 多头入场与止损设置
if (ta.crossover(source, lower))
    strategy.entry("Buy Calls", strategy.long, oca_name="BollingerBands", oca_type=strategy.oca.cancel, comment="Buy Calls")
    // 计算基于入场价的多头止损价
    long_stop = strategy.entry_price("Buy Calls") * (1 - stop_pct / 100)
    strategy.exit("Exit Long", "Buy Calls", stop=long_stop)
else
    strategy.cancel(id="BBandLE")

// 空头入场与止损设置
if (ta.crossunder(source, upper))
    strategy.entry("Buy Puts", strategy.short, oca_name="BollingerBands", oca_type=strategy.oca.cancel, comment="Buy Puts")
    // 计算基于入场价的空头止损价
    short_stop = strategy.entry_price("Buy Puts") * (1 + stop_pct / 100)
    strategy.exit("Exit Short", "Buy Puts", stop=short_stop)
else
    strategy.cancel(id="BBandSE")

// 绘制动量指标
plot(mom, title="Momentum", color=color.blue)

关键修改说明

  • 添加了固定止损百分比输入参数,可在策略设置中调整止损比例(范围0.1%-10%)
  • 在多头/空头入场订单执行后,通过strategy.entry_price()获取对应订单的实际入场价格,计算固定止损位
  • 使用strategy.exit()绑定入场订单ID,确保止损指令与入场订单关联,一旦价格触达止损位自动平仓
  • 止损位基于入场价固定计算,不会随后续价格变动调整(非追踪止损)

内容的提问来源于stack exchange,提问作者Dalton Trevino

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 06:20:30