英文:
Handling arrays when request.security_lower_tf for 1 minute does not return values for each 1 min bar [pinescript v5]
问题
I'm working with individual bars from lower time frames, e.g. working with 1 minute bars on a 5 minute chart. In principle request.security_lower_tf(syminfo.tickerid, '1',close)
should return an array with 5 values for every bar on the 5 minute chart. However, 1 minute bars are often missing, even for highly liquid tickers. As an example, I've used the code below to plot the # number of 1 minute bars on a 5 minute chart for the ticker XLK. As you can see from the image, some 5 minute bars only have 3 or 4 one minute bars associated with them. The frequency of these events is even higher for medium and low volume tickers.
indicator("No of 1 min bars")
array_1min_close = request.security_lower_tf(syminfo.tickerid, '1',close) // gets the value of all 1 minute closes
num_1min_bars = array.size(array_1min_close) // gets the size of the array, which is the total number of bars
plot(num_1min_bars,title="# of 1 min bars", color=color.yellow) // plots the total number of 1 min bars
Here's the problem. Let's say the first of the expected 5 bars is missing. One would think that when a bar is missing that Pinescript would fill that position of the array with na
, but instead it ignores that it is missing and fills it with the second 1 minute bar. This creates two coding issues:
- not all arrays have uniform sizes, so errors often pop up when calling for index 3 or 4 from an expected 5 member array.
- the placement of 1 minute bars is not uniform within the array, making it difficult to call the correct sub-bar e.g. in the case above bar #2 would be in array position 0 instead of the usual position 1
Is there a way to:
- produce these arrays in the expected manner, i.e., with all missing values filled with
na
in the proper position/order? - is it possible to achieve this in a manner that works for all time frames above 1 minute, e.g., the 5 minute chart always returning 5-membered arrays, a 30 min chart always returning 30-membered arrays, etc.?
英文:
I'm working with individual bars from lower time frames, e.g. working with 1 minute bars on a 5 minute chart. In principle request.security_lower_tf(syminfo.tickerid, '1',close)
should return an array with 5 values for every bar on the 5 minute chart. However, 1 minute bars are often missing, even for highly liquid tickers. As an example, I've used the code below to plot the # number of 1 minute bars on a 5 minute chart for the ticker XLK. As you can see from the image, some 5 minute bars only have 3 or 4 one minute bars associated with them. The frequency of these events is even higher for medium and low volume tickers.
indicator("No of 1 min bars")
array_1min_close = request.security_lower_tf(syminfo.tickerid, '1',close) // gets the value of all 1 minute closes
num_1min_bars = array.size(array_1min_close) // gets the size of the array, which is the total number of bars
plot(num_1min_bars,title="# of 1 min bars", color=color.yellow) // plots the total number of 1 min bars
Here's the problem. Let's say the first of the expected 5 bars is missing. One would think that when a bar is missing that Pinescript would fill that position of the array with na
, but instead it ignores that it is missing and fills it with the second 1 minute bar. This creates two coding issues:
- not all arrays have uniform sizes, so errors often pop up when
calling for index 3 or 4 from an expected 5 member array. - the placement of 1 minute bars is not uniform within the array, making it difficult to call the correct sub-bar e.g. in the case
above bar #2 would be in array position 0 instead of the usual
position 1
Is there a way to:
- produce these arrays in the expected manner, ie with all missing values filled with
na
in the proper position/order - is it possible to achieve this in a manner that works for all time frames above 1 minute, e.g. the 5 minute chart always returning 5-membered arrays, a 30 min chart always returning 30-membered arrays, etc.
答案1
得分: 0
我已经找到了解决上述问题1的方法。
解决方法是在request.security
未能添加na
值到数组末尾时,将其添加,并持续添加,直到数组具有预期数量的元素。下面的代码将原始代码(黄色图)与修改后的代码进行了比较,修改后的代码使用array.push()
将na
值添加到数组末尾(品红色):
indicator("1分钟柱数量")
// 原始代码,其中request.security没有进行任何修改就输入元素
array_1min_close = request.security_lower_tf(syminfo.tickerid, '1', close) // 获取所有1分钟收盘价的值
num_1min_bars = array.size(array_1min_close) // 获取数组的大小,即总柱数量
plot(num_1min_bars, title="# 未使用push的1分钟柱数量", color=color.yellow)
// 修改后的代码,其中添加na值到数组末尾以填充到预期大小(如果request.security未添加正确数量的元素)
array_1min_close1 = request.security_lower_tf(syminfo.tickerid, '1', close)
num_1min_bars1 = array.size(array_1min_close1)
while barstate.isconfirmed and timeframe.isintraday and timeframe.multiplier > num_1min_bars1
array.push(id=array_1min_close1, value=na)
num_1min_bars1 := array.size(array_1min_close1)
plot(num_1min_bars1, title="# 使用解决方法1的1分钟柱数量", color=color.fuchsia)
下面的图片是从5分钟图表中获取的。理论上,request.security_lower_tf(syminfo.tickerid, '1',close)
应该每分钟填充数组中的一个元素,总共填充5个元素。原始代码(黄色)显示在许多情况下未发生这种情况,而修改后的代码(品红色)始终有5个数组元素。
由于此解决方案将缺失的数组元素添加到数组末尾,它无法解决上述问题2,其中元素以错误的位置输入数组。如果您的目标是获取数组的最大值或最小值,计算平均值等,那么无需解决此问题。但如果您需要处理数组中特定时间指定的元素,例如提取较低时间框架的第4个1分钟柱,则此问题仍然存在。原则上,解决该问题的方法是跟踪经过的时间,并在每分钟结束时,如果request.security_lower_tf
没有推送值,则将na
推入数组。然而,我不确定是否可以实现这一点,因为据我所知,request.security_lower_tf
仅在较高时间框架的柱结束后才将较低时间框架的值填充到数组中。如果这是真的,那么在request.security_lower_tf
没有推送值的时间点插入na
将变得不可能。
英文:
I have found a solution to issue 1 described above.
The solution is to add na
values to the end of the array when request.security
fails to do so, and to keep adding until the array has the expected number of elements. The code below compares the original code above (yellow plot) to the modified code where array.push()
is used to add na values to the end of the array (fucschia):
indicator("No of 1 min bars")
// Original code where request.security enters elements without any modification
array_1min_close = request.security_lower_tf(syminfo.tickerid, '1',close) // gets the value of all 1 minute closes
num_1min_bars = array.size(array_1min_close) // gets the size of the array, which is the total number of bars
plot(num_1min_bars,title="# of 1 min bars without push", color=color.yellow)
// Modified code where na values are added to the end of the array to fill it to expected size (if request.security does not add the correct # of elements)
array_1min_close1 = request.security_lower_tf(syminfo.tickerid, '1',close)
num_1min_bars1 = array.size(array_1min_close1)
while barstate.isconfirmed and timeframe.isintraday and timeframe.multiplier > num_1min_bars1
array.push(id=array_1min_close1, value=na)
num_1min_bars1 := array.size(array_1min_close1)
plot(num_1min_bars1, title="# of 1 min bars w/solution 1", color=color.fuchsia)
The image below is taken from a 5 minute chart. request.security_lower_tf(syminfo.tickerid, '1',close)
should theoretically have filled the array with 1 element per minute for a total of 5 elements in the array. The original code (yellow) shows this didn't happen in many instances, whereas there are always 5 array elements in the modified code (fuchsia).
Since this solution adds the missing array elements to the end of the array, it doesn't solve issue #2 above, where elements are entered into the array in the wrong position. There is no need to resolve this problem if your goal is to take the max or min of the array, to calculate an average, etc. But this remains an issue if you need to work with specific time-designated elements within the array, e.g. pick out the 4th 1 minute bar from the lower time frame. In principle, the solution to that problem would be to keep track of elapsed time and after every minute push na
into the array if request.security_lower_tf
did not push a value. However, I'm not sure if this can be accomplished because as far as I can tell, request.security_lower_tf
only fills the array with lower time frame values AFTER the bar on the higher time frame is complete. If that is true, then it becomes impossible to retroactively insert na
at time points where request.security_lower_tf
did not.
答案2
得分: 0
Luckily this is the first coding language I've learned so I dont have much room to complain about how things SHOULD HAVE been formed a different way from the start. I just see the workarounds as being the normal way to punch through the walls to achieve the desired result. Sure, erasing the point in time where a candle would have formed but didnt bc there was no update to price/volume helps with reigning in the overuse of resources when possible (as well as other notable benefits to the overall product) but it also literally skews the data/calculations on scripts that use a Lookback(which is basically the entirety of the logic on the platform). But of course, theyll find some way to make it seem like any new issue that has popped up that requires a work-around was originally built with that intended purpose rather than it being an issue of foresight when creating the language...but nonetheless, we are here. Ill further add that I am very pleased with the product, even though it may be so only bc of my naivety to other coding languages in general. With almost every Scanner script I have built for the 1min TF, I have made use of this "desired effect" to enhance the end result (using it as a volatility filter to filter out alert triggers from coins that would otherwise take a lot more time to get the % gained (or lost) that I was looking for. Ok...enough jabbb.
One way you could address issue #2 in your inquiry would be to get a timestamp from all the printed bars and loop back through them to determine if the difference between a bars timestamp and the previous bars timestamp was the correct amount of time or if it showed that the time difference between the 2 timestamps is larger than the timespan of ONE of the bars. Hope this makes sense....and that it works but please do find me on TV (@chasinalts) and let me know if it worked or not (if you were going to give it a shit). If it doesnt work I have a few other ideas that I've implemented in some of my scanners that I know we could get working and get you on your way past this debacle. Peace brother....or sister.
英文:
Luckily this is the first coding language I've learned so I dont have much room to complain about how things SHOULD HAVE been formed a different way from the start. I just see the workarounds as being the normal way to punch through the walls to achieve the desired result. Sure, erasing the point in time where a candle would have formed but didnt bc there was no update to price/volume helps with reigning in the overuse of resources when possible (as well as other notable benefits to the overall product) but it also literally skews the data/calculations on scripts that use a Lookback(which is basically the entirety of the logic on the platform). But of course, theyll find some way to make it seem like any new issue that has popped up that requires a work-around was originally built with that intended purpose rather than it being an issue of foresight when creating the language...but nonetheless, we are here. Ill further add that I am very pleased with the product, even though it may be so only bc of my naivety to other coding languages in general. With almost every Scanner script I have built for the 1min TF, I have made use of this "desired effect" to enhance the end result (using it as a volatility filter to filter out alert triggers from coins that would otherwise take a lot more time to get the % gained (or lost) that I was looking for. Ok...enough jabbb.
One way you could address issue #2 in your inquiry would be to get a timestamp from all the printed bars and loop back through them to determine if the difference between a bars timestamp and the previous bars timestamp was the correct amount of time or if it showed that the time difference between the 2 timestamps is larger than the timespan of ONE of the bars. Hope this makes sense....and that it works but please do find me on TV (@chasinalts) and let me know if it worked or not (if you were going to give it a shit). If it doesnt work I have a few other ideas that I've implemented in some of my scanners that I know we could get working and get you on your way past this debacle. Peace brother....or sister.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论