5.1 Plotting Lines and Shapes
One of the most powerful features of Pinescript is the ability to draw directly on the chart. This allows you to visualize indicators, levels, and other important information. In this section, we'll learn how to use the plot() function to draw lines and other shapes on the chart.
The plot() function takes several arguments, such as the data series to plot and optional parameters like line color and style. Here's an example of how to plot a Simple Moving Average (SMA) on the chart:
smaValue = ta.sma(close, 14)
plot(smaValue, title="14-day SMA", color=color.red, linewidth=2)
5.2 Adding Labels and Text
In addition to plotting lines and shapes, Pinescript allows you to add labels and text annotations to your chart. Labels can be useful for displaying information about specific points on the chart or marking important levels.
To create a label, use the label.new() function. Here's an example of how to create a label at the current bar's high, displaying the current price:
price = close
label.new(x=bar_index, y=high, text="Price: " + str.tostring(price), color=color.blue, textcolor=color.white, style=label.style_label_down)
5.3 Drawing Custom Indicators
When creating custom indicators, you'll often want to combine multiple drawing elements to create a clear and informative visualization. For example, you might want to plot a line representing an indicator value, along with labels or shapes to highlight specific events or conditions.
Here's an example of a custom indicator that plots a moving average and adds a label when the close price crosses above the moving average:
smaValue = ta.sma(close, 14)
plot(smaValue, title="14-day SMA", color=color.red, linewidth=2)
crossAbove = ta.crossover(close, smaValue)
if crossAbove
label.new(x=bar_index, y=high, text="Cross Above", color=color.green, textcolor=color.white, style=label.style_label_down)
In this example, we used the ta.crossover() function to detect when the close price crosses above the moving average, and then created a label at that point on the chart.
That concludes Lesson 5! You should now have a solid understanding of how to draw on the chart using Pinescript, including plotting lines, shapes, and adding labels and text annotations. In the next lesson, we'll delve into creating alerts and executing trades using Pinescript strategies.
Go to Lesson 6: Conditional Statements and Loops
Comments
0 comments
Please sign in to leave a comment.