仅在特定日期编辑系列 [英] Edit series only on certain dates
问题描述
我有一个系列 myLine,用值 na 填充
I have a series myLine, which I fill with value na
myLine = 1==1 ? na : na // Series with na
现在我想创建一个函数来更新 myLine
系列,使其仅在某些柱线(日内)上具有值.
Now I want to create a function that updates the myLine
series to have a value only on certain bars (intraday).
isDate(y,m,d) => y==year and m==month and d==dayofmonth ? true:false // Is the date of the current bar equal to the date provided by the parameters?
setMyData(y,m,d,lineValue) =>
if timeframe.isintraday and isDate(y,m,d)
myLine := lineValue
setMyData(2020,03,31,1234)
setMyData(2020,04,01,2345)
但是,这似乎不起作用,我收到此错误
However, this doesn't seem to work, and I get this error
Cannot modify global variable 'myLine' in function.
我也尝试过使用 myLine[bar_index] := lineValue
但这似乎也不起作用.
I also tried using myLine[bar_index] := lineValue
but that doesn't seem to work either.
有谁知道如何仅针对某些数据点更新系列的值?
我试图仅在某些日期(盘中)绘制水平线.
Does anyone know how to update values of a series only for certain datapoints?
I'm trying to plot horizontal lines only on certain dates (intraday).
我特别想使用系列(而不是线对象),因为这允许我在样式选项卡中更改颜色.
I specifically want to use a series (instead of a line object) because that allows me to change to color in the styles tab.
推荐答案
Pine 不允许从函数的局部范围修改全局变量.这 2 种方法应该可以完成工作,#2 是最健壮的,因为它不会受到编译器对三元中嵌套 if
块数量的限制:
Pine does not allow global variables to be modified from a function's local scope. These 2 ways should get the job done, with #2 being the most robust because it will not be constrained by compiler limits on the number of nested if
blocks in the ternary:
//@version=4
study("")
// ————— #1
isDate(y,m,d) => y==year and m==month and d==dayofmonth // Is the date of the current bar equal to the date provided by the parameters?
float myLine1 = na
myLine1 :=
isDate(2020,03,31) ? 1234 :
isDate(2020,04,01) ? 2345 : na
plot(myLine1, "myLine1", color.silver, 10, plot.style_circles, transp = 50)
// ————— #2
initOnDate(y,m,d, prev, init) =>
if y==year and m==month and d==dayofmonth
init
else
prev
float myLine2 = na
myLine2 := initOnDate(2020,03,31,myLine2,1234)
myLine2 := initOnDate(2020,04,01,myLine2,2345)
plot(myLine2, "myLine2", color.orange, 3, plot.style_circles)
这篇关于仅在特定日期编辑系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!