close
close
how to show price in pinescript

how to show price in pinescript

3 min read 05-02-2025
how to show price in pinescript

Showing price data is fundamental to any Pine Script indicator or strategy. This article will guide you through various methods of displaying price information on your TradingView charts, ranging from simple price labels to sophisticated dynamic price overlays. We'll cover different techniques, providing clear examples and explanations for each.

Displaying Current Price

The simplest way to show the current price is using the close variable. This variable holds the closing price of the current bar.

//@version=5
indicator(title="Current Price", shorttitle="Current Price", overlay=true)

plot(close, title="Close Price", color=color.blue)

This script plots the closing price as a blue line on the chart. The overlay=true parameter ensures the plot is drawn directly on the price chart.

Showing Price with Labels

While plotting the price as a line is useful, displaying the numerical price value directly on the chart can be more informative. We can achieve this using the label.new() function.

//@version=5
indicator(title="Price Label", shorttitle="Price Label", overlay=true)

label_price = label.new(bar_index, high, str.tostring(close), color=color.white, style=label.style_label_down, textcolor=color.black)

This code creates a label at the high of each bar, displaying the closing price. The style and textcolor parameters customize the label's appearance. Note that this creates a new label for every bar, which can clutter the chart for long timeframes. For more concise labeling, consider only showing labels under specific conditions, like a new high or low.

Displaying Different Price Types

Pine Script offers access to various price types beyond the closing price. These include:

  • open: The opening price of the bar.
  • high: The highest price of the bar.
  • low: The lowest price of the bar.
  • hl2: The average of the high and low price ((high + low) / 2).
  • hilo: Returns the High or Low depending on the condition.
  • close: The closing price of the bar (as seen before).

Here's an example showcasing the high and low prices:

//@version=5
indicator(title="High/Low Prices", shorttitle="High/Low", overlay=true)

plot(high, title="High Price", color=color.green)
plot(low, title="Low Price", color=color.red)

This script plots the high price in green and the low price in red.

Dynamic Price Levels

Often, you'll want to display price levels based on calculations or conditions. For example, you might want to show a moving average or a specific percentage above or below the current price.

Here's how to display a simple moving average as a price level:

//@version=5
indicator(title="Moving Average Price", shorttitle="MA Price", overlay=true)

ma = ta.sma(close, 20) // 20-period simple moving average
plot(ma, title="20-SMA", color=color.orange)

This script calculates a 20-period simple moving average and plots it on the chart. You can easily adapt this to other moving averages (e.g., EMA, WMA).

Adding Price Labels to Specific Points

Let's create labels only when a new high is made:

//@version=5
indicator(title="New High Price Label", shorttitle="New High Label", overlay=true)

newHigh = ta.highest(high, 10) //Check for a new 10-bar high
if (high > newHigh)
    label.new(bar_index, high, str.tostring(high) + " New High!", color=color.green, style=label.style_label_up, textcolor=color.black)

This script only creates a label when the current high is greater than the highest high over the last 10 bars. Modify the 10 to adjust the lookback period.

Formatting Price Output

For better readability, you might want to format the price with specific decimal places or currency symbols. Use the str.format function:

//@version=5
indicator(title="Formatted Price", shorttitle="Formatted Price", overlay=true)

formatted_price = str.format("{0,number,#.##}", close)
label.new(bar_index, high, "Price: " + formatted_price, color=color.white, style=label.style_label_down, textcolor=color.black)

This formats the closing price to two decimal places. Adjust #.## to your preferred formatting.

These examples illustrate various ways to display price information in Pine Script. Experiment with different combinations and customizations to create visualizations that suit your trading style and analytical needs. Remember to always test your scripts thoroughly before using them in live trading.

Related Posts