如何修改Pinescript策略平仓信号标签以显示盈亏点数?
给Pinescript策略平仓信号添加盈亏点数标签
方法一:给strategy.close添加带盈亏的注释
直接利用strategy.close的comment参数,结合开仓均价计算盈亏点数,修改后的核心代码如下:
// 自动获取品种最小变动单位(点值),适配绝大多数品种 pip = syminfo.mintick // 多单开仓 if LongCondition strategy.entry("Long", strategy.long) // 多单平仓:添加盈亏点数注释 if LongExit // 计算盈亏点数:(平仓价 - 开仓均价) / 点值 pips_profit = (close - strategy.position_avg_price) / pip // 格式化注释文本,保留1位小数 comment_text = str.format("Close entry(s) order Long {0} Pips", str.tostring(pips_profit, "#.0")) strategy.close("Long", comment=comment_text) // 空单开仓 if ShortCondition strategy.entry("Short", strategy.short) // 空单平仓:添加盈亏点数注释 if ShortExit // 空单盈亏计算:(开仓均价 - 平仓价) / 点值 pips_profit = (strategy.position_avg_price - close) / pip comment_text = str.format("Close entry(s) order Short {0} Pips", str.tostring(pips_profit, "#.0")) strategy.close("Short", comment=comment_text)
方法二:手动绘制盈亏标签(更直观)
如果注释显示不够醒目,可以在平仓K线位置手动绘制标签,代码如下:
pip = syminfo.mintick if LongCondition strategy.entry("Long", strategy.long) if LongExit pips_profit = (close - strategy.position_avg_price) / pip label_text = str.format("Close Long\n{0} Pips", str.tostring(pips_profit, "#.0")) // 在K线上方绘制向下标签 label.new(bar_index, high, label_text, color=color.red, textcolor=color.white, style=label.style_label_down) strategy.close("Long") if ShortCondition strategy.entry("Short", strategy.short) if ShortExit pips_profit = (strategy.position_avg_price - close) / pip label_text = str.format("Close Short\n{0} Pips", str.tostring(pips_profit, "#.0")) // 在K线下方绘制向上标签 label.new(bar_index, low, label_text, color=color.green, textcolor=color.white, style=label.style_label_up) strategy.close("Short")
注意事项
syminfo.mintick会自动匹配当前交易品种的最小变动单位,无需手动设置,比如EURUSD对应0.0001,GBPJPY对应0.01。- 盈亏点数的正负逻辑符合交易习惯:多单盈利为正、亏损为负;空单盈利为正、亏损为负。
- 若需要调整小数位数,将
str.tostring(pips_profit, "#.0")中的#.0改为#.00即可显示两位小数。
内容的提问来源于stack exchange,提问作者Space James
相关产品推荐
相关产品推荐

