Home » Drawing On Charts » Understanding shape.flag in Pine Script

Understanding shape.flag in Pine Script

Photo of author
Published on

This article will explore how to use the shape.flag style within the plotshape function, providing a comprehensive guide on its application and utility.

Utilizing shape.flag in plotshape

The plotshape function is a versatile tool used to draw shapes on a chart at specific bars or candles, based on conditions defined in the script. The shape.flag style, when applied, renders a flag-shaped marker on the chart. This style is particularly useful for flagging specific conditions such as breakout points or key levels reached by the price.

Syntax and Application

Here is a basic example of how to use the shape.flag style with the plotshape function in Pine Script:

//@version=5
indicator("My Flag Indicator", overlay=true)
price = close > open 
plotshape(series=priceReached, style=shape.flag, location=location.abovebar, color=color.green, title="Green Flag")
Application

In this example, we’re creating an indicator called “My Flag Indicator” that plots a green flag above the bar whenever the closing price is greater than the opening price and the closing price is also greater than 100.

  • Line 1: We specify the version of Pine Script being used and declare our script as an indicator with overlay=true to ensure it’s drawn over the price chart.
  • Line 2: We define a boolean condition price that checks if the closing price of a bar is greater than its opening price.
  • Line 3: We use the plotshape function to plot a shape based on the price condition. The parameters specify the use of the shape.flag style, location above the bar, color of the shape, and a title.

Detailed Walkthrough of the Code

  • indicator("My Flag Indicator", overlay=true): This line sets up our script as an indicator and allows it to be drawn on the same layer as the price chart.
  • priceReached = close > open and close > 100: Here, we’re defining a logical condition that must be met for our shape to be plotted. This line checks two conditions: if the close of a bar is higher than its open (indicating a bullish candle) and if the close is also above a value of 100.
  • plotshape(series=price, style=shape.flag, location=location.abovebar, color=color.green, title="Green Flag"): This is where the magic happens. We call plotshape with several arguments:
  • series=price: The condition under which the shape is plotted.
  • style=shape.flag: Specifies the shape to be a flag.
  • location=location.abovebar: Places the shape above the bar that meets the condition.
  • color=color.green: Sets the color of the shape to green, making it visually distinct and easy to spot.
  • title="Green Flag": Gives a title to our shape plot, which can be useful for identifying the indicator in the chart’s legend or when hovering over the shape.

Key Features and Takeaways

  • Function Usability: The plotshape function, with the shape.flag style, is highly useful for marking specific events on a chart, enhancing visual analysis and decision-making.
  • Syntax: The function allows for customization of the shape’s location, color, and title, making it versatile for different use cases.
  • Application: Ideal for highlighting key price levels, trend changes, or signal confirmations in trading strategies.

Using shape.flag in your Pine Script indicators can significantly improve the interpretability of your trading charts, making it easier to identify and act on crucial market events.

Leave a Comment