Non-series drawings
Main idea of non-series drawings
This section describes another way to visualize data. It is convenient for:
- displaying data that is not a time series;
- changes and erasures of previously drawn;
- displaying something not only in chart coordinates (time-price), but also, for example, just in the corner of the screen on top of the chart;
- displaying something in the future or retroactively;
Advantages and disadvantages:
+ it is a very powerful tool that can significantly increase the informative value of the indicator;
- this is more difficult to write and debug than working with time series.
To control the drawing of elements, you will need the self.chart
object, which is available in your indicator by default as part of the MainContext
base class. You can do three things with objects:
- adding a new element to the chart
- changing what was drawn earlier, even if it was drawn on previous bars
- removing an item from the chart
Or more specifically, for example, adding a line to the chart:
Different types of elements can be positioned differently. Two types of positions are currently supported:
- AbsolutePosition - the item’s position is set in chart absolute coordinates (time-price), the item will scroll along with the chart;
- RelativePosition - the item’s position is set relative to the edges of the chart, the element will hang in one place on the screen on top of the chart.
The ends of the LineSegments can only be positioned absolutely. Labels can have either AbsolutePosition or RelativePosition. Relative positioned Labels can be useful for displaying some statistics.
The Relative Position can be selected from the enum, or manually determined. The following pre-prepared positions are available to choose from:
You can also set a custom relative position:
vertical_anchor
and horizontal_anchor
set which point of the element will be bound to this position.
top_bottom_ratio
and left_right_ratio
set how large margins should be made from the left and upper edges of the chart.
There are no presets for absolute positions, they are always set by the user:
LineSegment
A LineSegment is both lines, segments, and rays, three in one. You can customize the line style, color, width, as well as adjust the ends of the segment (arrows, circles, etc.). For a complete list of parameters, see the Library Reference. And here let’s look at a simple example with segments.
An indicator that creates lines across the entire history with a length of 3 bars (configurable by parameter), connecting close candles:
Label
Labels also have many different parameters, see their list in the Library Reference. Now let’s look at a couple of examples of using labels.
Absolute positioned
An indicator that draws a close line and draws a price label for each hundredth bar:
Relative positioned
The indicator that draws the close price in the corner of the chart:
Demo example: lines + labels
This example shows how you can use lines and labels in your indicators. Add it to your chart and try to play with the drawing items style.
Source code: