Pine 脚本表适用于某些符号,但不适用于其他符号

发布于 2025-01-20 14:53:59 字数 1537 浏览 2 评论 0原文

我有一个工作脚本可以正确显示表。但是,当我选择另一个符号或库存时,桌子没有显示任何计算并返回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)

I have a working script that display tables correctly. However when i chose another symbol or stock the table do not show any calculations and return NaN

So this works fine on "Nifty" but doesnt work if I choose "NIFTY1!" symbol for example, only shows NaN in the results. Any directions around this will be greatly appreciable

=============================================

Below is the reproducible script

    //@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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹊巢 2025-01-27 14:53:59

嗯,这是因为您在 sum() 函数中使用了 4999 。这意味着您需要检查 4999 个柱来进行计算。

但是,如果您的图表上的柱形图少于 4999 个,您将会遇到您所描述的问题。它不是特定于图表的。如果您前往 Nifty 的时间范围为 1 周或更长,您也会遇到同样的问题。

实际上,您可以使用一个简单的计数器来完成您想做的事情。

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)

Well, it is because you are using 4999 in your sum() function. That means you want to check 4999 bars for your calculation.

However, if there are less than 4999 bars on your chart, you will have the issue you described. It is not chart specific. You will have the same issue if you go to Nifty with a timeframe 1 week or higher.

You can actually use a simple counter for what you want to do.

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文