请求为Pine Script策略添加3%止盈1%止损平仓逻辑
为Pine Script v5策略添加止盈止损平仓逻辑
你需要为现有均线交叉策略添加上涨3%止盈、下跌1%止损的平仓规则,以下是修改后的完整代码,并附上关键逻辑说明:
//@version=5 startDate = input(title="开始日期", defval=15) startMonth = input(title="开始月份", defval=1) startYear = input(title="开始年份", defval=2023) afterStartDate = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) investment = input.int(defval=1000, title='投资金额 USDT') comm = 0.08 // 新增可调整的止盈止损参数 takeProfitPerc = input.float(defval=3.0, title='止盈百分比 (%)', tooltip='持仓后达到该比例平仓') stopLossPerc = input.float(defval=1.0, title='止损百分比 (%)', tooltip='持仓后达到该比例平仓') strategy('BS WAB with lines', shorttitle='WAB', overlay=true, default_qty_value = 1000, commission_type = strategy.commission.percent, commission_value = comm) // 定义价格与时间框架 src = input(close, title='数据源') resolution = timeframe.period price = request.security(syminfo.tickerid, resolution, src) // 定义均线周期与类型 ma1Period = input.int(defval=8, title='周期', inline='1', group='移动平均线: 区域1') ma1Type = input.string(defval='EMA', title='类型', options=['EMA', 'SMA', 'WMA', 'HMA', 'LIN'], tooltip='均线平滑方式', inline='1', group='移动平均线: 区域1') ma2Period = input.int(defval=21, title='周期', inline='2', group='移动平均线: 区域1') ma2Type = input.string(defval='EMA', title='类型', options=['EMA', 'SMA', 'WMA', 'HMA', 'LIN'], tooltip='均线平滑方式', inline='2', group='移动平均线: 区域1') // 计算均线 ma1 = ma1Type == 'EMA' ? ta.ema(price, ma1Period) : ma1Type == 'SMA' ? ta.sma(price, ma1Period) : ma1Type == 'WMA' ? ta.wma(price, ma1Period) : ma1Type == 'HMA' ? ta.hma(price, ma1Period) : ma1Type == 'LIN' ? ta.linreg(price, ma1Period, 0) : na ma2 = ma2Type == 'EMA' ? ta.ema(price, ma2Period) : ma2Type == 'SMA' ? ta.sma(price, ma2Period) : ma2Type == 'WMA' ? ta.wma(price, ma2Period) : ma2Type == 'HMA' ? ta.hma(price, ma2Period) : ma2Type == 'LIN' ? ta.linreg(price, ma2Period, 0) : na // 定义趋势状态 TrendUp1() => ma1 > ma2 TrendDown1() => ma1 < ma2 trendColor1 = TrendUp1() ? color.green : TrendDown1() ? color.red : color.blue // 绘制均线与填充 p_ma1 = plot(ma1) p_ma2 = plot(ma2) fill(p_ma1, p_ma2, color=trendColor1) // 绘制买卖信号 plotshape(ma1 > ma2 and ma1[1] < ma2[1], style=shape.triangleup, color= color.rgb(59, 255, 108), size=size.small, text="多", location=location.belowbar, title='买入信号', offset=1) plotshape(ma1 < ma2 and ma1[1] > ma2[1], style=shape.triangledown, color= color.rgb(255, 0, 0), size=size.small, text="空", location=location.abovebar, title='卖出信号', offset=1) // 计算下单数量与执行入场逻辑 mainComment=str.tostring(high[1]) strategyAmount=investment/close longCondition = ma1 > ma2 and ma1[1] < ma2[1] if (longCondition and afterStartDate) strategy.entry("多单", strategy.long, qty=strategyAmount, comment = mainComment) shortCondition = ma1 < ma2 and ma1[1] > ma2[1] if (shortCondition and afterStartDate) strategy.entry("空单", strategy.short, qty=strategyAmount, comment = mainComment) // --- 新增止盈止损平仓逻辑 --- // 获取持仓平均入场价 entryPrice = strategy.position_avg_price // 多单平仓条件:上涨3%止盈 / 下跌1%止损 longTakeProfit = close >= entryPrice * (1 + takeProfitPerc / 100) longStopLoss = close <= entryPrice * (1 - stopLossPerc / 100) // 空单平仓条件:下跌3%止盈 / 上涨1%止损 shortTakeProfit = close <= entryPrice * (1 - takeProfitPerc / 100) shortStopLoss = close >= entryPrice * (1 + stopLossPerc / 100) // 执行平仓操作 if (strategy.position_size > 0 and (longTakeProfit or longStopLoss)) strategy.close("多单", comment="止盈/止损平仓") if (strategy.position_size < 0 and (shortTakeProfit or shortStopLoss)) strategy.close("空单", comment="止盈/止损平仓")
关键逻辑说明:
- 参数化配置:新增可调整的止盈止损百分比参数,方便后续测试不同比例效果
- 持仓均价获取:通过
strategy.position_avg_price获取当前持仓的平均入场价,确保盈亏比例计算准确 - 多单规则:持仓后价格较入场价上涨3%触发止盈,下跌1%触发止损
- 空单规则:持仓后价格较入场价下跌3%触发止盈(做空盈利逻辑),上涨1%触发止损
- 精准平仓:通过订单名称指定平仓对象,避免误平其他方向持仓
内容的提问来源于stack exchange,提问作者Esteban G. Gutierrez
相关产品推荐
相关产品推荐

