//@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”) // 达到条件平仓