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

PineScript策略求助:入场后无法固定止损与止盈值

问题说明

我开发了一套融合MACD、HMA和布林带(BB)指标的交易策略,规则如下:

  • 多头入场时,止损设为入场前最近7根K线的最低低点,止盈计算公式为入场价格 + 1.5*(入场价格 - 最低低点)
  • 空头入场时,止损设为入场前最近7根K线的最高高点,止盈计算公式为入场价格 - 1.5*(最高高点 - 入场价格)

当前核心问题:持仓过程中,止损和止盈的数值会随K线更新而变动,无法固定在入场时的计算值。我尝试用strategy.position标记持仓状态来解决,但未成功。由于刚接触PineScript,可能代码存在逻辑疏漏,希望能帮忙排查修复。

策略代码
strategy("自定义策略", overlay=true, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=1.5)
// 输入参数
lengthBB = 200
multBB1 = 1.2
multBB2 = 2.0
lengthHMA = 22
lengthMACD = 12
signalMACD = 26
lengthATR = 14
riskToRewardRatio = 1.5
maxBars = 10

// 布林带计算
basisBB1 = ta.sma(close, lengthBB)
upperBB1 = basisBB1 + multBB1 * ta.stdev(close, lengthBB)
lowerBB1 = basisBB1 - multBB1 * ta.stdev(close, lengthBB)
basisBB2 = ta.sma(close, lengthBB)
upperBB2 = basisBB2 + multBB2 * ta.stdev(close, lengthBB)
lowerBB2 = basisBB2 - multBB2 * ta.stdev(close, lengthBB)

// 赫尔移动平均线(HMA)
hmaClose = ta.hma(close, 22)

// MACD指标计算
[macdLine, signalLine, _] = ta.macd(close, lengthMACD, signalMACD, 9)

// ATR指标
atrValue = ta.atr(lengthATR)

// 入场条件
longCondition = (close <= lowerBB1 or close >= upperBB2) and ta.crossover(macdLine, signalLine) and close >= hmaClose
shortCondition = (close > upperBB1 or close < lowerBB2) and ta.crossunder(macdLine, signalLine) and close <= hmaClose

// 按每笔1.5%风险计算仓位
riskPerTrade = strategy.equity * 0.015
positionSize = riskPerTrade / atrValue  

// 计算当前周期的止损止盈(未固定入场值)
stoplossshort = ta.highest(high, 7)
stoplosslong  = ta.lowest(low, 7)
takeProfitLevelLong = close + riskToRewardRatio * (close - ta.lowest(low, 7))
takeProfitLevelShort = close - riskToRewardRatio * (ta.highest(high, 7) - close)

// 用于存储入场时的止损止盈值(var声明实现持久化)
var float stoplossshort1 = na
var float stoplosslong1 = na
var float takeProfitLevelLong1 = na
var float takeProfitLevelShort1= na

// 原逻辑:无入场信号时更新止损止盈值(错误逻辑)
if (longCondition == false and shortCondition == false)
    stoplossshort1 := stoplossshort
    stoplosslong1 := stoplosslong
    takeProfitLevelLong1 := takeProfitLevelLong
    takeProfitLevelShort1 := takeProfitLevelShort

// 多单入场
strategy.entry("多单", strategy.long, qty = positionSize, when = longCondition)
// 空单入场
strategy.entry("空单", strategy.short, qty = positionSize, when = shortCondition)

// 多单平仓条件
if (close <= stoplosslong1 or close >= takeProfitLevelLong1)
    strategy.close("多单")

// 空单平仓条件
if (close >= stoplossshort1 or close <= takeProfitLevelShort1)
    strategy.close("空单")

// 绘制布林带
plot(upperBB1, color=color.red)
plot(lowerBB1, color=color.green)
plot(upperBB2, color=color.red)
plot(lowerBB2, color=color.green)

// 绘制HMA均线
plot(hmaClose, color=color.blue)

// 绘制止损止盈线
plot(stoplosslong1, color=color.red, linewidth=2, title="多单止损")
plot(stoplossshort1, color=color.green, linewidth=2, title="空单止损")
plot(takeProfitLevelLong1, color=color.rgb(175, 76, 76), linewidth=2, title="多单止盈")
plot(takeProfitLevelShort1, color=color.rgb(82, 255, 111), linewidth=2, title="空单止盈")

我曾尝试通过strategy.position设置持仓状态布尔值来固定入场时的止损止盈,但没成功。刚学PineScript,可能哪里写错了。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 16:06:03