为什么 `close` 和 `open` 与图表上的价格不匹配?

Why is `close` and `open` not matching the price on chart?

FOREX, 1H Chart, //version=3 pinescript

我还是 Pinescript 的新手,但我注意到使用 closeopen returns 数据不是当前收盘价或开盘价。即使使用 close[1] returns 也与前一根蜡烛的收盘价完全不同。

这是为什么?我是否错误地解释了这些数据?

在我所做的研究中,我看到了这篇文章:https://www.tradingcode.net/tradingview/operators/history-referencing-operator/

Technically, the history referencing operator doesn’t return a single value but returns a series of values with a certain offset, even though we generally think that the history referencing operator accesses the nth element.

This means that, for example, close[5] doesn’t return a single closing price but a series of closing prices that are equal to the closing price of 5 bars ago.

上面那个大胆的声明 - "A series of closing prices";这是否意味着 close[5] 本身不是第 5 根蜡烛的收盘价?

如果是这样,那么我将如何使用类似以下内容显示该蜡烛的当前收盘价:

strategy.entry("SHORT", strategy.short, comment=tostring(close[1]) )

I'm still new to Pinescript but I noticed that using close or open returns data that is not the current close or open price. Even using close[1] returns an amount completely different than the closing of that previous candle.

(...)

strategy.entry("SHORT", strategy.short, comment=tostring(close[1]) )

不幸的是,这是 TradingView 的限制。当您对 comment 参数使用 tostring() 函数时(如您的代码片段中所示),TradingView 只会为回测发生的第一个柱线生成该字符串。

但是该文本在整个回测中保持不变,这解释了为什么您看到 'Strategy Tester' 中出现的价格与您根据对 close[1] 的理解所期望的价格大不相同,并且之类的

我们可以很容易地用下面的代码自己测试一下:

//@version=3
strategy(overlay=true, title="Example strategy")

longCondition = crossover(sma(close, 14), sma(close, 28))
shortCondition = crossunder(sma(close, 14), sma(close, 28))

if (longCondition)
    strategy.entry("My Long Entry Id", long=strategy.long, 
         comment=tostring(dayofmonth) + "-" + 
         tostring(month) + "-" + tostring(year))

if (shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)

这里我们生成一个带有当前柱的年、月、日的订单评论。或者至少,这就是它应该的工作方式。

对于 'Strategy Tester' 中的第一笔交易,它正确显示:

然后对于回测中很晚的交易,TradingView 仍然使用旧的、缓存的订单评论:

总结一下:您遇到的奇怪行为是由于 tostring() 函数和订单评论的 TradingView 限制。

此问题已在 Pine v3 和 v4 中修复。发布的代码现在在图表上显示了这一点。日期是订单执行前柱的日期,对应于订单发出的时间: