自编多空仓位Indicator频繁消失,求解决方法
多空仓位指标频繁消失问题修复
我编写的多空仓位指标功能正常,但会频繁消失,需执行「隐藏-重新显示」操作才能恢复。尝试过修改If语句、使用varip均未解决,代码如下:
//@version=5 indicator("Long & Short Position", "L & S",overlay=true,precision = 4) Price = input.price(defval = 0,confirm = true, title = "Entry Price") sl = input.price(defval = 0,confirm = true, title = "Stop Loss") LotSize = input.defval = 1000.000000, title = "Lot Size") rr = input.float(title="Risk Reward Ratio", defval=1.5, minval = 1.00, step=0.10) Close = close //Take Profit Calculation take_profit = (Price - sl) * rr + Price // Take Profit Value @ 1.5 Risk Reward Ratio Buy = take_profit > Price //Pips Calculation for Realized Profit pips = if Buy //take_profit > Price (take_profit - Price) else (Price - take_profit) PL = (pips * LotSize) //Pips Calculations Unrealized Profit Pips = if Buy // Price > close (close - Price) else (Price - close) Un_PL = (Pips * LotSize) Un_PL_Col = if Un_PL > 0 color.green else color.red //Drawing Profit Value on chart tblPos = "Top Right" tblposition = tblPos == 'Top Left' ? position.top_left : tblPos == 'Top Right' ? position.top_right : tblPos == 'Bottom Left' ? position.bottom_left : tblPos == 'Bottom Right' ? position.bottom_right : tblPos == 'Middle Left' ? position.middle_left : position.middle_right tblBorderColor = color.black celllBgColor = color.white cellTextColor = color.black var resultsTable = table.new(position=tblposition, columns=3, rows=3, bgcolor=#ffffff, border_width=1, frame_color=tblBorderColor, frame_width=4) table.clear(resultsTable, 0, 0) table.cell(resultsTable, column=0, row=1, text='Potential P&L : ' + str.tostring(PL,"$ ###.##"), text_color=cellTextColor, text_halign=text.align_right, text_valign=text.align_center, bgcolor=celllBgColor) table.cell(resultsTable, column=0, row=2, text='Unrealized P&L : ' + str.tostring(Un_PL,"$ ###.##"), text_color=Un_PL_Col, text_halign=text.align_right, text_valign=text.align_center, bgcolor=celllBgColor) // Plot box on chart a = box.new(bar_index[1]+ 6, ta.highest(sl, 1), bar_index[1]+ 12, ta.lowest(Price, 1), color.white, 1, line.style_solid, extend.none, xloc.bar_index, color.new(color.red, 60)) b = box.new(bar_index[1]+ 6, ta.highest(Price, 1), bar_index[1]+ 12, ta.lowest(take_profit, 1), color.white, 1, line.style_solid, extend.none, xloc.bar_index, color.new(color.lime, 60)) c = box.new(bar_index[2]+ 9, ta.highest(Price, 2), bar_index[1]+ 10, ta.lowest(Close, 1), color.white, 1, line.style_solid, extend.none, xloc.bar_index, Un_PL_Col) box.delete(a[1]) box.delete(b[1]) box.delete(c[1])
问题根源
- 输入默认值异常:Entry Price和Stop Loss默认值为0,未设置有效值时会导致
take_profit计算错误,使box坐标无效,触发指标消失。 - Box绘制逻辑缺陷:固定
bar_index偏移(如bar_index[1]+6)在图表缩放、切换时间框架时,会让box超出可视范围,看似消失。 - 多空逻辑不完整:原
take_profit计算仅适用于多头,空头场景下计算错误,引发绘图异常。
修复后的代码
//@version=5 indicator("Long & Short Position", "L & S", overlay=true, precision = 4) Price = input.price(defval = na, confirm = true, title = "Entry Price") sl = input.price(defval = na, confirm = true, title = "Stop Loss") LotSize = input.float(defval = 1000.0, title = "Lot Size") rr = input.float(title="Risk Reward Ratio", defval=1.5, minval = 1.00, step=0.10) // 输入有效性检查:仅当Entry Price和Stop Loss都设置且不相等时执行后续逻辑 valid_input = not na(Price) and not na(sl) and Price != sl // 区分多空计算止盈价格 is_long = Price > sl take_profit = na if valid_input take_profit = is_long ? Price + (Price - sl) * rr : Price - (sl - Price) * rr // 潜在盈亏计算 pips = 0.0 PL = 0.0 if valid_input pips = math.abs(take_profit - Price) PL = pips * LotSize // 浮动盈亏计算 Un_PL = 0.0 Un_PL_Col = color.gray if valid_input unrealized_pips = is_long ? close - Price : Price - close Un_PL = unrealized_pips * LotSize Un_PL_Col = Un_PL > 0 ? color.green : color.red // 表格位置配置 tblPos = input.string("Top Right", title="Table Position", options=["Top Left", "Top Right", "Bottom Left", "Bottom Right", "Middle Left", "Middle Right"]) tblposition = switch tblPos "Top Left" => position.top_left "Top Right" => position.top_right "Bottom Left" => position.bottom_left "Bottom Right" => position.bottom_right "Middle Left" => position.middle_left => position.middle_right tblBorderColor = color.black celllBgColor = color.white cellTextColor = color.black var resultsTable = table.new(position=tblposition, columns=3, rows=3, bgcolor=#ffffff, border_width=1, frame_color=tblBorderColor, frame_width=4) // 更新表格内容 if valid_input table.clear(resultsTable, 0, 0) table.cell(resultsTable, column=0, row=1, text='Potential P&L : ' + str.tostring(PL,"$ ###.##"), text_color=cellTextColor, text_halign=text.align_right, text_valign=text.align_center, bgcolor=celllBgColor) table.cell(resultsTable, column=0, row=2, text='Unrealized P&L : ' + str.tostring(Un_PL,"$ ###.##"), text_color=Un_PL_Col, text_halign=text.align_right, text_valign=text.align_center, bgcolor=celllBgColor) else table.clear(resultsTable, 0, 0) table.cell(resultsTable, column=0, row=1, text='请设置Entry Price和Stop Loss', text_color=color.red, text_halign=text.align_center, bgcolor=celllBgColor) // 使用var存储box对象,避免重复创建 var box sl_box = na var box tp_box = na var box unrealized_box = na // 删除旧box if not na(sl_box) box.delete(sl_box) if not na(tp_box) box.delete(tp_box) if not na(unrealized_box) box.delete(unrealized_box) // 绘制新box(仅输入有效时) if valid_input x_left = bar_index x_right = bar_index + 5 // 可自定义宽度 // 止损区间box sl_top = math.max(sl, Price) sl_bottom = math.min(sl, Price) sl_box = box.new(x_left, sl_top, x_right, sl_bottom, color.white, 1, line.style_solid, extend.none, xloc.bar_index, color.new(color.red, 60)) // 止盈区间box tp_top = math.max(take_profit, Price) tp_bottom = math.min(take_profit, Price) tp_box = box.new(x_left + 6, tp_top, x_right + 6, tp_bottom, color.white, 1, line.style_solid, extend.none, xloc.bar_index, color.new(color.lime, 60)) // 浮动盈亏区间box unrealized_top = math.max(Price, close) unrealized_bottom = math.min(Price, close) unrealized_box = box.new(x_left + 3, unrealized_top, x_right + 3, unrealized_bottom, color.white, 1, line.style_solid, extend.none, xloc.bar_index, color.new(Un_PL_Col, 60))
关键修改点
- 输入校验:新增
valid_input判断,避免无效值引发的计算和绘图异常。 - 多空逻辑修正:分别计算多头/空头的止盈价格,修复原代码仅支持多头的问题。
- Box对象管理:用
var存储box实例,每次先删除旧box再创建新box,避免资源泄漏。 - 位置优化:以当前bar为基准设置box位置,避免固定偏移导致的可视范围问题。
- 用户提示:输入无效时显示明确提示,提升体验。
内容的提问来源于stack exchange,提问作者user21637228
相关产品推荐
相关产品推荐

