Home » Drawing On Charts » Understanding plotshape() Function in Pine Script

Understanding plotshape() Function in Pine Script

Photo of author
Published on

This article delves deep into the plotshape() function in Pine Script version 5, providing a comprehensive tutorial to leverage its capabilities for your trading strategies.

Understanding plotshape()

The plotshape() function is used to plot symbols on your chart at specific points, based on conditions you define. It’s particularly useful for highlighting indicators’ signals, such as entry or exit points, making them visually striking and easy to identify.

Syntax and Parameters

The basic syntax of plotshape() in Pine Script version 5 is as follows:

plotshape(series, title, location, color, style, size, offset, text, textcolor, editable, show_last, display, join, tooltip)

Here’s a breakdown of the most commonly used parameters:

  • series (required): A boolean series where true conditions will have the shape plotted.
  • title: The name of the shape series, visible in the indicator settings and the legend.
  • location: Determines where the shape is plotted relative to the price bars. Common values include location.abovebar and location.belowbar.
  • color: Specifies the color of the shape. Can be a constant color value or a conditional color expression.
  • style: The shape’s style, such as shape.circle, shape.triangleup, shape.flag, etc.
  • size: The size of the shape, with options like size.small, size.normal, and size.large.

Example: Highlighting Cross-Overs

Let’s create an example where plotshape() is used to highlight crossovers between a fast and a slow moving average, a common trading signal.

//@version=5
indicator("MA Crossover Shapes", overlay=true)

// Define moving averages
fastMA = ta.sma(close, 9)
slowMA = ta.sma(close, 21)

// Crossover condition
bullishSignal = ta.crossover(fastMA, slowMA)

// Plot shapes for bullish signals
plotshape(bullishSignal, title="Bullish Signal", location=location.abovebar, color=color.green, style=shape.triangleup, size=size.small)
Example

Deep Dive into Each Line of Code

  1. Indicator Declaration: We begin by declaring the script as an indicator with indicator() and setting overlay=true to ensure our shapes are plotted directly on the price chart.
  2. Moving Averages Calculation: We calculate the fast and slow moving averages using the ta.sma() function, which computes the Simple Moving Average based on the specified period.
  3. Crossover Condition: The ta.crossover() function checks if the fast moving average crosses above the slow moving average, returning true when the condition is met.
  4. Plotting Shapes: The plotshape() function then takes the bullishSignal series as its condition. When true, it plots green triangles above the price bars where the condition is met.

Key Features

  • Function Usability and Syntax: plotshape() is versatile, allowing customization of the plotted shapes to fit any visual strategy requirement.
  • Application: Ideal for marking specific conditions on charts, such as trend reversals, crossovers, or any custom condition defined in your strategy.

Takeaways

  • The plotshape() function is a powerful tool for visually highlighting conditions or signals on your charts.
  • Understanding the function’s parameters allows for customizing the visual representation of your trading strategies.
  • Through examples, we’ve seen how to effectively apply plotshape() to mark bullish signals with specific shapes, enhancing the interpretability of trading strategies.

Leave a Comment