Reputation: 8031
FOREX, 1H Chart, //version=3 pinescript
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.
Why is this? Am I interpreting this data incorrectly?
In the research I've done, I came across this article: 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.
That bold statement above - "A series of closing prices"; does this mean that close[5]
itself is not the 5th candle's closing price?
If that's the case, then how would I go about displaying the current close price for that candle with something like:
strategy.entry("SHORT", strategy.short, comment=tostring(close[1]) )
Upvotes: 2
Views: 5110
Reputation: 8789
This issue has been fixed in both Pine v3 and v4. The posted code now shows this on the chart. The date is that of the bar preceding the order's execution, which corresponds to when the order was issued:
Upvotes: 2
Reputation: 1852
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]) )
This is unfortunately a TradingView limitation. When you use the tostring()
function for the comment
argument (as in your code snippet), TradingView only generates that string for the first bar the backtest happens on.
But that text then remains the same for the whole backtest, which explains why you see prices appearing in the 'Strategy Tester' that are wildly different from what you expect based on your understanding of close[1]
and the like.
We can easily test this ourselves with the following code:
//@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)
Here we generate an order comment with the day, month, and year of the current bar. Or at least, that's how it should work.
For the first trade in the 'Strategy Tester' it shows correctly:
Then for trades much later in the backtest, TradingView still uses the old, cached order comment:
To summarise: the weird behaviour you experienced is due to a TradingView limitation with the tostring()
function and order comments.
Upvotes: 5