DrawObjects

View as Markdown

Definition

A collection holding all of the drawn chart objects on the chart, for all series. The draw objects can be manually drawn or script generated objects.

  • When reloading NinjaScript, all objects (including manual drawing tools) are reloaded at the same time. There is no guarantee a manually drawn object will be added to the DrawObjects collection before an indicator starts processing data.
  • DrawObjects.ToList() is thread safe. DrawObjects collection itself is still dynamic (meaning it updates live) and as a result you can still run the risk of the collection being modified while you try to read it (and thus would see the related C# log entry). However, DrawObjects.ToList() is a snapshot of DrawObjects collection at the time the call is made.
  • Also please keep in mind that iterating over a large DrawObjects collection could have an impact on performance.
  • Draw objects are disposed (for example on chart closing) after State.Terminated is seen for your custom NinjaScript studies potentially working with those.

Property Value

A collection of IDrawingTool objects.

Syntax

DrawObjects*

DrawObjects[string tag]

DrawObjects.Count

Examples

Finding the draw object of a specific tag

1protected override void OnBarUpdate()
2{
3 if (DrawObjects["someTag"] != null && DrawObjects["someTag"] is DrawingTools.Line)
4 {
5 // Do something with the drawing tool line
6 }
7
8 // An alternative approach to find the draw object by a tag
9 if (DrawObjects["someTag"] as DrawingTools.Line != null)
10 {
11 // Do something drawing tool line
12 }
13
14 // Yet another way to find a drawing tool by a tag
15 if (DrawObjects["someTag"].GetType().Name == "Line")
16 {
17 // Do something drawing tool line
18 }
19}

Get the number of draw objects on a chart

1protected override void OnBarUpdate()
2{
3 if (DrawObjects.Count == 3)
4 {
5 // Do something
6 }
7}

Looping through the collection to find specific draw objects

1protected override void OnBarUpdate()
2{
3 // Loops through the DrawObjects collection via a threadsafe list copy
4 foreach (DrawingTool draw in DrawObjects.ToList())
5 {
6 // Finds line objects that are attached globally to all charts of the same instrument
7 if (draw.IsGlobalDrawingTool && draw is DrawingTools.Line)
8 {
9 DrawingTools.Line globalLine = draw as DrawingTools.Line;
10
11 // Changes the line color and prints its starting and end points
12 globalLine.Stroke.Brush = Brushes.Black;
13
14 Print("Start: " + globalLine.StartAnchor.SlotIndex + " End: " + globalLine.EndAnchor.SlotIndex);
15 }
16
17 // Finds non-global line objects
18 else if (draw is DrawingTools.Line)
19 {
20 // Indicates if this is a manually drawn or script generated line
21 Print("Line Object: " + draw.Tag + " Manually Drawn: " + draw.IsUserDrawn);
22 }
23 }
24}

Typecasting as in the example above will not function the same way in a compiled assembly (DLL). For an alternative approach, see the Considerations For Compiled Assemblies page.