var 不会跨条保持它的值 [英] var does NOT keep it's value across bars
问题描述
//@version=4研究(保持数据跨条(var)",overlay=true)//变量var bool canGoShort = truevar bool canGoLong = truevar int myTest = 5//职能f_print(_txt) =>t = time + (time - time[1]) * 3, var _lbl = label.new(t, high, _txt, xloc.bar_time, yloc.price, #00000000, label.style_none, color.gray, size.large), label.set_xy(_lbl, t, high + 3 * tr)//主要的如果 canGoShortmyTest := myTest + 1如果可以去龙myTest := myTest + 1f_print("myTest=" + tostring(myTest))
我对这个似乎没有按预期工作的简单脚本感到困惑.
我用值 5 初始化了一个 var
变量 myTest
.
由于 canGoShort
和 canGoLong
都是 true
,我希望 myTest
的值增加 1, 每个条上两次:
在 if canGoShort
中一次,在 if canGoLong
中一次.
假设我们有 5000 根柱线,那么我希望它以 5 + (5000 * 2) = 10005 结束.
但是,运行脚本时,打印myTest=7
?
我的印象是声明 var int myTest = 5
是在每个条形上执行的.
我的理解是,使用关键字 var
定义的变量在所有条形图中都保持其值,但似乎并非如此.
文档清楚地说明它应该保持它的价值:
- 再次为给您带来的麻烦表示歉意.我们在常见问题解答中更新了
f_print()
的所有实例.请注意,顺便说一句,我们主要使用该版本进行调试,因为它保留在一行中.这是我们使用 Ctrl+Shift+p 生成我们的f_print()
用于调试的 AHK 代码:^+P:: SendInput f_print(_txt) =>var _lbl = label.new(bar_index,highest(10)[1], _txt, xloc.bar_index, yloc.price, {#}00000000, label.style_none, color.gray, size.large, text.align_left), label.set_xy(_lbl, bar_index,highest(10)[1]), label.set_text(_lbl,_txt)`nf_print(){Left}
在即将发布的脚本中,我们使用这个版本,更灵活,更高效,但由于
if
语句不能压缩为一行://————— 在图表末尾打印标签.f_print(_txt, _y, _color, _offsetLabels) =>var 标签 _lbl = na_t = int(time + (time - time[1]) * _offsetLabels)如果 barstate.islast如果 na(_lbl)//只创建一次标签._lbl := label.new(_t, _y, _txt, xloc.bar_time, yloc.price, #00000000, label.style_none, color.gray, size.large)//捏造 `if` 块的返回类型,因此编译器不会抱怨(感谢 midtownsk8rguy 的技巧).整数(无)别的//不是在每次实时柱更新时删除并重新创建标签,而是更新标签信息;它更有效率.label.set_xy(_lbl, _t, _y)label.set_text(_lbl, _txt)label.set_textcolor(_lbl, _color)整数(无)
//@version=4 study("Keep data across bars (var)", overlay=true) // Variables var bool canGoShort = true var bool canGoLong = true var int myTest = 5 // Functions f_print(_txt) => t = time + (time - time[1]) * 3, var _lbl = label.new(t, high, _txt, xloc.bar_time, yloc.price, #00000000, label.style_none, color.gray, size.large), label.set_xy(_lbl, t, high + 3 * tr) // MAIN if canGoShort myTest := myTest + 1 if canGoLong myTest := myTest + 1 f_print("myTest=" + tostring(myTest))
I'm baffled by this simple script that doesn't seem to work as intended.
I initialize a
var
variablemyTest
with value 5.
SincecanGoShort
andcanGoLong
both are alwaystrue
, I'd expect the value ofmyTest
to be incremented by 1, twice on each bar:
Once in theif canGoShort
, and once in theif canGoLong
.
Say we have 5000 bars, then I'd expect it to finish at 5 + (5000 * 2) = 10005.However, when running the script,
myTest=7
is printed?I'm getting the impression that the declaration
var int myTest = 5
is executed on each bar.
It was my understanding that a variable defined with the keywordvar
keeps its value across all bars, but it doesn't appear to be that way.The documentation clearly says that it should keep it's value:
- https://www.tradingview.com/pine-script-reference/#op_var
var is the keyword used for assigning and one-time initializing of the variable. - https://www.pinecoders.com/faq_and_code/#techniques
Since v4 the var keyword provides a way to initialize variables on the first bar of the dataset only, rather than on every bar the script is run on, as was the case before. This has the very useful benefit of automatically taking care of the value’s propagation throughout bars
What am I missing here?
解决方案Calcs are happening as expected. What's wrong there is our
f_print()
function. Apologies for that. The version you picked up doesn't refresh the text. This code has a newer version off_print()
that does that.Note that to inspect values bar by bar, as you would want here, our
plotchar()
trick is more useful, if you also have your Data Window open to inspect the value, as you can inspect each bar's value when you mouse bar to bar. See our answer here for our AHK macro that generates the statement from a variable name, if you have a Windows setup and are interested.//@version=4 study("Keep data across bars (var)", overlay=true) // Variables var bool canGoShort = true var bool canGoLong = true var int myTest = 5 // Functions f_print(_txt) => var _lbl = label.new(bar_index, highest(10)[1], _txt, xloc.bar_index, yloc.price, #00000000, label.style_none, color.gray, size.large, text.align_left), label.set_xy(_lbl, bar_index, highest(10)[1]), label.set_text(_lbl, _txt) // MAIN if canGoShort myTest := myTest + 1 if canGoLong myTest := myTest + 1 f_print("myTest=" + tostring(myTest, "0.0")) plotchar(myTest, "myTest", "", location.top)
Again, apologies for the trouble. We updated all the instances of
f_print()
in our FAQ. Note, btw, that we use that version mostly for debugging because it holds on one line. This is the AHK code we use to generate ourf_print()
with Ctrl+Shift+p for debugging:^+P:: SendInput f_print(_txt) => var _lbl = label.new(bar_index, highest(10)[1], _txt, xloc.bar_index, yloc.price, {#}00000000, label.style_none, color.gray, size.large, text.align_left), label.set_xy(_lbl, bar_index, highest(10)[1]), label.set_text(_lbl, _txt)`nf_print(){Left}
In scripts that will be published, we use this version, which is more flexible and more efficient, but can't be compressed to one line because of the
if
statements:// ————— Print a label at end of chart. f_print(_txt, _y, _color, _offsetLabels) => var label _lbl = na _t = int(time + (time - time[1]) * _offsetLabels) if barstate.islast if na(_lbl) // Only create label once. _lbl := label.new(_t, _y, _txt, xloc.bar_time, yloc.price, #00000000, label.style_none, color.gray, size.large) // Fudge return type of `if` block so compiler doesn't complain (thx midtownsk8rguy for the trick). int(na) else // Rather than delete and recreate the label on every realtime bar update, update the label's information; it's more efficient. label.set_xy(_lbl, _t, _y) label.set_text(_lbl, _txt) label.set_textcolor(_lbl, _color) int(na)
这篇关于var 不会跨条保持它的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- https://www.tradingview.com/pine-script-reference/#op_var