资源加载中... loading...

学习PINE代码,请问止损设置有什么问题?回测的时候止损不执行,实盘的时候执行止损了,但后续就不按照条件开单直接不停的开仓,平仓。请问高手是什么问题?

Author: Fland, Created: 2024-10-22 05:50:32, Updated: 2024-10-22 05:51:23

//@version=5 strategy(“RSI(6) Buy at 30, EMA(34) Sell with Stop Loss”, overlay=true)

// 参数设置 rsiPeriod = 6 emaPeriod = 54 buyLevel = 30 positionSize = 0.02

// 计算 RSI 和 EMA rsiValue = ta.rsi(close, rsiPeriod) emaValue = ta.ema(close, emaPeriod)

// 买入条件:RSI 低于 30 buySignal = ta.crossunder(rsiValue, buyLevel)

// 卖出条件:价格高于 EMA 54 sellSignal = close > emaValue

// 记录开仓价格 var float entryPrice = na

// 买入逻辑:只做多 if (buySignal and strategy.position_size == 0) strategy.entry(“Buy”, strategy.long, qty=positionSize) entryPrice := close // 记录买入时的开仓价格

// 止损逻辑:设定止损为 0.5% if (strategy.position_size > 0) stopLossPrice = entryPrice * 0.995 // 0.5% 止损 if (close <= stopLossPrice) strategy.close(“Buy”, comment=“Stop Loss”) // 止损平仓

// 平仓逻辑:价格高于 EMA 54 时平仓 if (strategy.position_size > 0 and sellSignal) strategy.close(“Buy”, comment=“Take Profit”) // 达到条件平仓


More