Home » Drawing On Charts » Understanding the shape.diamond in Pine Script

Understanding the shape.diamond in Pine Script

Photo of author
Published on

This tutorial will dive into how to use the shape.diamond style in Pine Script, including syntax, practical examples, and key considerations for its application.

Introduction to shape.diamond

Basic Syntax

Before we proceed to examples, let’s understand the basic syntax for using shape.diamond in Pine Script:

plotshape(series, style=shape.diamond, location=location.abovebar, color=color.red, size=size.small)

In this syntax:

  • series is a boolean series where the shape will be plotted when the condition is true.
  • style specifies the shape style, in this case, shape.diamond.
  • location determines where the shape will be plotted relative to the price bars. For diamonds, a common location is location.abovebar.
  • color sets the color of the diamond shape.
  • size defines the size of the shape.

Example

Let’s create a unique example by slightly modifying variable names and values to ensure the tutorial remains distinct:

//@version=5
indicator("My Diamond Indicator", overlay=true)

priceCross = ta.crossover(close, ta.sma(close, 20))

plotshape(priceCross, style=shape.diamond, location=location.belowbar, color=color.blue, size=size.normal)

In this modified example:

  • We’re creating an indicator called “My Diamond Indicator” that plots blue diamond shapes below the price bars.
  • The condition for plotting diamonds is based on a crossover of the closing price above a 20-period Simple Moving Average (SMA).
  • When the condition priceCross is true, a diamond shape is plotted.
Example

Walkthrough of the Code

  • indicator("My Diamond Indicator", overlay=true): This line declares a new indicator named “My Diamond Indicator” that will be drawn over the price chart (overlay=true).
  • priceCross = ta.crossover(close, ta.sma(close, 20)): Here, we define a condition named priceCross that checks for a crossover between the closing price and a 20-period SMA. If the closing price crosses above the SMA, priceCross becomes true.
  • plotshape(priceCross, style=shape.diamond, location=location.belowbar, color=color.blue, size=size.normal): This line uses the plotshape function to draw a diamond shape below the price bars (location.belowbar) in blue color (color.blue) with a normal size (size.normal) whenever priceCross is true.

Key Features and Takeaways

  • Function Usability and Syntax: The shape.diamond style in Pine Script is straightforward to use, with a simple syntax within the plotshape function. This allows for easy visualization of specific conditions on a price chart.
  • Application: Ideal for highlighting important events like crossovers, divergences, or any custom condition defined in your trading strategy. The flexibility in color, size, and location makes it a versatile tool for chart annotations.
  • Customization: By changing parameters such as color, size, and location, you can customize how and where the diamond shapes appear on the chart, tailoring the indicator to your specific analytical needs.

Leave a Comment