Pinescript 策略未在图表上正确打印(可能是时间戳问题)

Pinescript strategy is not printing correctly on charts (possible timestamp problem)

我试图回测依赖于市场深度 (DOM) 数据的策略。

不幸的是,在 Pinescript 中无法访问数据,我也无法通过调用 API 访问(如果错误请纠正我!)...

因此,我尝试按照此常见问题解答将这些值硬编码到我的脚本中:https://www.pinecoders.com/faq_and_code/#how-can-i-initialize-a-series-on-specific-dates-using-external-data

我正在尝试从上面 link 中获取的代码的修改版本,但我无法正确显示它。

我期望发生的事情: 我认为它会在 5 月 5 日凌晨 1 点、2 点、3 点、4 点和 5 点打印 5 个单点。每个点都比下一个高 1 级。

相反会发生什么: 它打印 24 个点,一个点代表 5 月 5 日的每一个小时。所有的点都在同一层,没有步进。

这是我使用的代码:

//@version=4
strategy("Initialize External Data")

// Dates must appear in chronological order and match chart dates.
// The limit of lines is ~900. Variables used in your calcs will decrease this amount.
float data = na
timestamp = timestamp(year, month, dayofmonth, 0, 0, 0)
data := timestamp == timestamp(2021, 05, 01, 0, 0, 0) ? 1 : data
data := timestamp == timestamp(2021, 05, 01, 1, 0, 0) ? 2 : data
data := timestamp == timestamp(2021, 05, 01, 2, 0, 0) ? 3 : data
data := timestamp == timestamp(2021, 05, 01, 3, 0, 0) ? 4 : data
data := timestamp == timestamp(2021, 05, 01, 4, 0, 0) ? 5 : data

plot(data, "data", color.fuchsia, 2, plot.style_circles)

这是我在图表上看到的屏幕截图(时区设置为“交换”的 1 小时图表):

谁能描述一下我做错了什么?

您的 timestamp 没有 hour 变量,因此它始终固定在 5 月 1 日,00:00h。
这只匹配您的第一行,因此输出将始终为 1.

这会起作用:

//@version=4
strategy("Initialize External Data")

// Dates must appear in chronological order and match chart dates.
// The limit of lines is ~900. Variables used in your calcs will decrease this amount.
float data = na

timestamp = timestamp(year, month, dayofmonth, hour, 0, 0)

data := timestamp == timestamp(2021, 05, 01, 0, 0, 0) ? 1 : data
data := timestamp == timestamp(2021, 05, 01, 1, 0, 0) ? 2 : data
data := timestamp == timestamp(2021, 05, 01, 2, 0, 0) ? 3 : data
data := timestamp == timestamp(2021, 05, 01, 3, 0, 0) ? 4 : data
data := timestamp == timestamp(2021, 05, 01, 4, 0, 0) ? 5 : data

plot(data, "data", color.fuchsia, 2, plot.style_circles)

产生: