Pine Script指标当前未收K线不绘图及表格不显示问题求助
解决Pine Script指标绘图与表格显示问题
一、让绘图延伸至未收K线
- 确保绘图函数(如
plot()、line.new()等)不被限制在已收K线的判断条件内(比如不要放在if barstate.islastconfirmedhistory块中)。 - 对于需要延伸到实时K线最新价格的线条,直接基于当前K线的实时数据计算绘图值即可;若使用
line.new()绘制线段,需在barstate.islast时更新线段终点到当前实时价格:// 示例:更新线段至实时K线 var line trendLine = na if barstate.islast line.set_x2(trendLine, bar_index) line.set_y2(trendLine, close)
二、表格显示逻辑适配不同周期
1. 日线周期:实时显示表格
- 保留
barstate.islast作为表格更新触发条件,同时确保table.new()用var声明,避免重复创建表格:var table myTable = table.new(position = position.top_right, columns = 2, rows = 5, bgcolor = color.new(color.gray, 60))
2. 1分钟周期:仅收线后显示且不重复
- 通过周期判断+
barstate.islastconfirmedhistory控制表格更新,仅在1分钟K线收线后执行,同时新增table.clear()避免内容重复叠加:var table myTable = table.new(position = position.top_right, columns = 2, rows = 5, bgcolor = color.new(color.gray, 60)) // 区分周期设置更新条件 isDaily = timeframe.isdaily is1Min = timeframe.isintraday and timeframe.multiplier == 1 updateTable = isDaily ? barstate.islast : (is1Min ? barstate.islastconfirmedhistory : barstate.islast) if updateTable table.clear(myTable) // 清空旧内容,避免重复 table.cell(myTable, 0, 0, "Analysis Summary", text_color = color.orange) table.cell(myTable, 0, 1, "Long alerts", text_color = color.orange) table.cell(myTable, 0, 2, "Short alerts", text_color = color.orange) table.cell(myTable, 0, 3, "stats", text_color = color.orange) txt_trend = switch trend_dir 0 => "Horizontal channel" 1 => "Up trend" -1 => "Down trend" table.cell(myTable, 1, 0, txt_trend, text_color = color.white, bgcolor = (trend_dir >0 ? color.green : (trend_dir < 0 ? color.red : color.black)))
三、关键注意事项
var关键字仅初始化一次对象,务必用于table.new()和无需重复创建的变量(如array.new<string>)。- 高频周期(如1分钟)用
barstate.islastconfirmedhistory限制表格更新,避免实时tick触发频繁刷新,浪费资源。
内容的提问来源于stack exchange,提问作者user14554500
相关产品推荐
相关产品推荐

