Pine 脚本 table 适用于某些符号但不适用于其他符号
Pine script table working for some symbols but not working for other symbols
我有一个可以正确显示 tables 的工作脚本。然而,当我选择另一个符号或股票时,table 不显示任何计算并且 return NaN
所以这在“Nifty”上运行良好,但如果我选择“NIFTY1!”则不起作用例如符号,在结果中只显示 NaN。对此的任何指导都将非常有用
=============================================
下面是可重现的脚本
//@version=4
study(title="test", shorttitle="test",overlay=true,format=format.price)
i_startTime = input(defval = timestamp("01 Jan 2021 09:00 +0530"), title = "Start Time", type = input.time)
i_endTime = input(defval = timestamp("31 Mar 2022 15:30 +0530"), title = "End Time", type = input.time)
inDateRange = time >= i_startTime and time <= i_endTime
StrategyLongCond = crossover(ema(close, 8), ema(open, 9) )
StrategyShortCond = crossunder(ema(close, 8), ema(open, 9))
plotshape(StrategyLongCond ,title="Buy Signal", style=shape.triangleup, location=location.belowbar,color=color.blue, display=display.all)
plotshape(StrategyShortCond,title="Sell Signal", style=shape.triangledown, location=location.abovebar,color=color.red, display=display.all)
StrategyLongCondCount= sum((StrategyLongCond and inDateRange? 1 : 0), 4999)
StrategyShortCondCount= sum((StrategyShortCond and inDateRange? 1 : 0), 4999)
TradeSignalCount = StrategyLongCondCount + StrategyShortCondCount
var table Display = table.new(position.bottom_right, 5, 50)
if barstate.islast
table.cell(Display, 1, 4, "Sgnl", bgcolor = color.new(color.blue, 90), text_size=size.small)
table.cell(Display, 1, 5, tostring(TradeSignalCount), bgcolor = color.new(color.gray,90), text_size=size.small)
嗯,这是因为您在 sum()
函数中使用了 4999
。这意味着您要检查 4999 根柱以进行计算。
但是,如果图表上的柱数少于 4999,就会出现您描述的问题。它不是特定于图表的。如果您在 1 周或更长时间内访问 Nifty,您将遇到同样的问题。
您实际上可以使用一个简单的计数器来完成您想做的事情。
var buy_cnt = 0
var sell_cnt = 0
buy_cnt := (StrategyLongCond and inDateRange) ? buy_cnt + 1 : buy_cnt
sell_cnt := (StrategyShortCond and inDateRange) ? sell_cnt + 1 : sell_cnt
total_cnt = buy_cnt + sell_cnt
var table Display = table.new(position.bottom_right, 5, 50)
if barstate.islast
table.cell(Display, 1, 1, "Sgnl", bgcolor = color.new(color.blue, 20), text_size=size.normal)
table.cell(Display, 1, 2, tostring(TradeSignalCount), bgcolor = color.new(color.gray,20), text_size=size.normal)
table.cell(Display, 1, 3, tostring(total_cnt), bgcolor = color.new(color.gray,20), text_size=size.normal)
我有一个可以正确显示 tables 的工作脚本。然而,当我选择另一个符号或股票时,table 不显示任何计算并且 return NaN
所以这在“Nifty”上运行良好,但如果我选择“NIFTY1!”则不起作用例如符号,在结果中只显示 NaN。对此的任何指导都将非常有用
=============================================
下面是可重现的脚本
//@version=4
study(title="test", shorttitle="test",overlay=true,format=format.price)
i_startTime = input(defval = timestamp("01 Jan 2021 09:00 +0530"), title = "Start Time", type = input.time)
i_endTime = input(defval = timestamp("31 Mar 2022 15:30 +0530"), title = "End Time", type = input.time)
inDateRange = time >= i_startTime and time <= i_endTime
StrategyLongCond = crossover(ema(close, 8), ema(open, 9) )
StrategyShortCond = crossunder(ema(close, 8), ema(open, 9))
plotshape(StrategyLongCond ,title="Buy Signal", style=shape.triangleup, location=location.belowbar,color=color.blue, display=display.all)
plotshape(StrategyShortCond,title="Sell Signal", style=shape.triangledown, location=location.abovebar,color=color.red, display=display.all)
StrategyLongCondCount= sum((StrategyLongCond and inDateRange? 1 : 0), 4999)
StrategyShortCondCount= sum((StrategyShortCond and inDateRange? 1 : 0), 4999)
TradeSignalCount = StrategyLongCondCount + StrategyShortCondCount
var table Display = table.new(position.bottom_right, 5, 50)
if barstate.islast
table.cell(Display, 1, 4, "Sgnl", bgcolor = color.new(color.blue, 90), text_size=size.small)
table.cell(Display, 1, 5, tostring(TradeSignalCount), bgcolor = color.new(color.gray,90), text_size=size.small)
嗯,这是因为您在 sum()
函数中使用了 4999
。这意味着您要检查 4999 根柱以进行计算。
但是,如果图表上的柱数少于 4999,就会出现您描述的问题。它不是特定于图表的。如果您在 1 周或更长时间内访问 Nifty,您将遇到同样的问题。
您实际上可以使用一个简单的计数器来完成您想做的事情。
var buy_cnt = 0
var sell_cnt = 0
buy_cnt := (StrategyLongCond and inDateRange) ? buy_cnt + 1 : buy_cnt
sell_cnt := (StrategyShortCond and inDateRange) ? sell_cnt + 1 : sell_cnt
total_cnt = buy_cnt + sell_cnt
var table Display = table.new(position.bottom_right, 5, 50)
if barstate.islast
table.cell(Display, 1, 1, "Sgnl", bgcolor = color.new(color.blue, 20), text_size=size.normal)
table.cell(Display, 1, 2, tostring(TradeSignalCount), bgcolor = color.new(color.gray,20), text_size=size.normal)
table.cell(Display, 1, 3, tostring(total_cnt), bgcolor = color.new(color.gray,20), text_size=size.normal)