Home » Symbol Information Functions » Prices and Volume in Pine Script

Prices and Volume in Pine Script

Photo of author
Published on

In this article, we will dive into how to utilize prices and volume in Pine Script Version 5, exploring the syntax, functionalities, and practical applications.

Understanding Basic Price and Volume Variables

Price Variables

In Pine Script, price data is accessed through built-in variables. These variables represent different price points of a trading instrument:

  • close: Refers to the closing price of the current bar.
  • open: Indicates the opening price of the current bar.
  • high: Represents the highest price reached in the current bar.
  • low: Denotes the lowest price in the current bar.

Volume Variable

  • volume: This variable is used to access the trading volume of the current bar.
indicator("OHLCV Display", shorttitle="OHLCV", overlay=true)

// Getting the OHLCV values
currentOpen = open
currentHigh = high
currentLow = low
currentClose = close
currentVolume = volume

// Creating labels to display the OHLCV data
if barstate.islast
    label.new(bar_index, high, 
              text="O: " + str.tostring(currentOpen) + "\nH: " + str.tostring(currentHigh) +
              "\nL: " + str.tostring(currentLow) + "\nC: " + str.tostring(currentClose) + 
              "\nV: " + str.tostring(currentVolume),
              style=label.style_labeldown, color=color.blue, textcolor=color.white, yloc=yloc.abovebar)

Walkthrough of the Code

  1. Script Setup
    • //@version=5: Uses Pine Script Version 5.
    • indicator(...): Defines the script as an overlay indicator named “OHLCV Display”.
  2. Variable Assignment
    • Assigns the current bar’s open, high, low, close, and volume to currentOpen, currentHigh, currentLow, currentClose, and currentVolume.
  3. Label Creation (Conditional)
    • if barstate.islast: A condition to check if the current bar is the latest on the chart.
    • label.new(...): Creates a label on the latest bar, displaying the OHLCV values (O: Open, H: High, L: Low, C: Close, V: Volume).
  4. Label Styling
    • The label is positioned above the high of the bar.
    • It uses a blue background (color=color.blue) and white text (textcolor=color.white).

Key Features and Takeaways

  1. Real-Time Data Display: The script effectively displays real-time Open, High, Low, Close, and Volume (OHLCV) data for the latest bar, aiding in immediate market analysis.
  2. Conditional Labeling: Labels are only created for the most recent bar (barstate.islast), ensuring the chart remains uncluttered and focused on current data.
  3. Custom Label Styling: The use of a blue background and white text for labels enhances readability and distinguishes the OHLCV data visually on the chart.
  4. Simplicity and Efficiency: The script is concise yet efficient, demonstrating the capability of Pine Script to deliver crucial trading information straightforwardly.
  5. Versatility for Further Customization: This example serves as a foundation, which can be easily customized or expanded upon for more complex trading strategies and analyses.

Leave a Comment