TradingView Pine Script中close与close[1]的区别及rsi(close[1],lengthMiddle)里close[1]的含义解析
Pine Script:
close vs close[1] Explained Hey there! Let's break down your questions clearly—understanding bar indexing is fundamental to writing effective Pine Script, so this is a great set of questions to clarify.
1. What's the difference between close and close[1] in Pine Script?
Pine Script processes every bar (candle) on your chart sequentially, starting from the oldest and moving to the most recent. Here's the core distinction:
closerefers to the closing price of the current bar being processed. For live, unclosed bars, this will be the latest price as the bar forms; for already closed bars, it's the final closing value of that bar.close[1]refers to the closing price of the previous (1 bar back) completed bar. The number inside the square brackets is a bar offset—you can use any positive integer here (e.g.,close[2]would pull the closing price of the bar two periods before the current one).
Think of it like walking through your chart bar by bar: close is the price of the bar you're standing on right now, and close[1] is the price of the bar you just stepped away from.
2. What does close[1] mean in rsi_value = rsi(close[1],lengthMiddle), and how is it different from using close?
Let's unpack this specific code snippet:
- The
rsi()function calculates the Relative Strength Index using a series of price data. By passingclose[1]as the first argument, you're telling the function to use closing prices from the previous bar (and all earlier completed bars) to compute the RSI value. - If you used plain
closeinstead, the RSI would be calculated using the current bar's closing price—including real-time price updates if the bar hasn't finished forming yet.
The practical difference comes down to timing and reliability:
- Using
closegives you an RSI that updates live as the current bar develops. This is great for real-time monitoring, but can sometimes show temporary values that change as the bar finishes forming. - Using
close[1]makes the RSI "lag" one bar—its value will only update once the current bar closes, since it relies solely on fully completed bar data. This avoids signals based on incomplete, potentially volatile data that might repaint before the bar closes.
内容的提问来源于stack exchange,提问作者Yogesh Pandey




