要使用Tradingview的PineScript引用存储在系列中的值,可以使用下标整数来调用系列中的值。
//What's the UNIX time of the previous bar?
time[1]
//What's the UNIX time of the 21st bar (counting from current time)
time[20]
我的目标是将日历天数的输入映射到下标整数以调用系列的值,以便它返回该系列的值,x天前
Pseudo code:
SeriesIndexedByDays([series],[day,integer],[period]):
SeriesIndexedByDays(close,30):
Return close[30*Bars_per_day]
//Returns close value 30 days ago
我用PineScript最接近的是:
//Method 1:
days = input(title="Reference Point, # of Days", type=integer, defval=1, minval=0.01)
bars_per_day = 24*60/interval // 24 Hours per day * 60 Minutes per hour / [X (Minutes||Days||Weeks||Months||Years)] per bar
number_of_bars_reference_point = bars_per_day*days // Bars per day * Days
close[number_of_bars_reference_point]
但这并不理想,因为PineScript的interval是该栏的任何period的前缀倍数:[10m,5m,5D] - > [10,5,5] 因此,如果您将周期从5分钟更改为5天,则仍会输出相同的值:5
我做的第二次尝试是:
//Method 2:
sec_per_day=24*60*60 // 24 hours per day * 60 minutes per hour * 60 seconds per minute = Seconds per day
t = time //Dump UNIX time into a variable
deltat=t-t[1] //Difference of UNIX time between this and previous bar, not necessarily the time of one bar (ex. session close, exchange's clock jumps due to offline or non continuous bars)
seconds_per_bar=lowest(deltat,2)/1000 // Use the lowest of the last two deltat values in case bar is the start of a session, divide by 1000convert from milliseconds to seconds
bars_per_day = sec_per_day/seconds_per_bar //Series, number of bars per day //Occationally this produces abberant values, assumption is that its because time dates
retrospective_reference_point_number_of_bars=bars_per_day*days
这将retrospective_reference_point_number_of_bars生成为正确映射[天,期] - > [条]的系列。但是当用来调用另一个系列时
close[retrospective_reference_point_number_of_bars]
TradingView会返回错误:
out of depth at index
方法2的其他问题包括,如果句点小于'days'输入,那么它将返回一个小数/浮点值,不能用于引用一个系列。
可能的方法
如果我可以将方法2的输出系列的第一个值转换为整数,我可以使用它。
您有什么想法来解决[期间,间隔,天] - > [酒吧]的映射?