首页 > 解决方案 > 如何计算 pinescript 中特定日期范围的枢轴级别?

问题描述

我正在尝试获取特定日期范围的每周枢轴计算的高/低/收盘系列数据..

示例代码:

//
study(title="Pivot Points", shorttitle="Weekly-Levels", overlay=true)

// defaultTimeFrame = ((isintraday) ? "D" : ((isdaily) ? "W" : ((isweekly) ? "M" : "3M")))
// inputTimeFrame = input(title="Time Frame", type=string, defval="Default")
// chosenTimeFrame = (inputTimeFrame == "Default") ? defaultTimeFrame : inputTimeFrame
startDate = input(title="Start Date", type=integer, defval=1, minval=1, maxval=31)
startMonth = input(title="Start Month", type=integer, defval=2, minval=1, maxval=12)
startYear = input(title="Start Year", type=integer, defval=2020, minval=2020, maxval=2100)

endDate = input(title="End Date", type=integer, defval=30, minval=1, maxval=31)
endMonth = input(title="End Month", type=integer, defval=6, minval=1, maxval=12)
endYear = input(title="End Year", type=integer, defval=2020, minval=1800, maxval=2100)

weekly_cpr = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and  (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))

getSeries(e) => security(tickerid, weekly_cpr, e, lookahead=barmerge.lookahead_on)

H = getSeries(high[1])
L = getSeries(low[1])
C = getSeries(close[1])

// Main Pivot
P = (H + L + C) / 3 

我得到以下错误。

Processing script...
line 40: Cannot call `security` with arguments (string, series[bool], series, literal bool, literal bool, literal string); available overloads: security(string, string, series[integer], const bool, const bool, string) => series[integer]; security(string, string, series, const bool, const bool, string) => series; security(string, string, series[bool], const bool, const bool, string) => series[bool]; security(string, string, series[color], const bool, const bool, string) => series[color];

让我知道我应该如何处理这个?

标签: pine-script

解决方案


这是实现您的目的的更简单方法。我们在图表的上下文中定义我们的计算和条件,然后将它们发送到security()其 HTF 上下文中进行评估。Calcs 不会重绘:

//@version=4
study(title="Pivot Points", shorttitle="Weekly-Levels", overlay=true)

startDate = input(title="Start Date", defval=1, minval=1, maxval=31)
startMonth = input(title="Start Month", defval=2, minval=1, maxval=12)
startYear = input(title="Start Year", defval=2020, minval=2020, maxval=2100)

endDate = input(title="End Date", defval=30, minval=1, maxval=31)
endMonth = input(title="End Month", defval=6, minval=1, maxval=12)
endYear = input(title="End Year", defval=2020, minval=1800, maxval=2100)

weekly_cpr = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and  (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))

H = high
L = low
C = close
P = (H + L + C) / 3 

// We send both the date constraint and the request for the complete, calculated data through `security()`,
// so that all that is evaluated in the context of `security()`'s context.
pHtf = security(syminfo.tickerid, "W", weekly_cpr ? P[1] : na, lookahead = barmerge.lookahead_on)
plot(pHtf, "pHtf", color.blue, 1, plot.style_circles)

在此处输入图像描述


推荐阅读