Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Running code when objects are drawn

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Running code when objects are drawn

    Hello,

    Trying to stream line some actions. Currently after a button is pressed, it takes the last drawn Risk Reward object and calculates certain data. The idea is to calculate the data as soon as the object is DONE drawing. Here's the code:

    Code:
    foreach (ChartScale cs in ChartControl.ChartPanels.SelectMany(cp => cp.Scales))
        cs.ChartObjects.CollectionChanged += OnChartObjectsChanged;​
    
    ...
    
    private void OnChartObjectsChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
        if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
        {
            if (e.NewItems == null) return;    
                    
            dynamic rr = e.NewItems[0];
                    
            if (rr.ToString().Equals("NinjaTrader.NinjaScript.DrawingTools.RiskReward"))
            {
                Print("new risk reward");
                        
                if(rr.DrawingState == DrawingState.Building)
                {
                    Print("Is Building");
                    ?LAUNCH EVENT TO RUN ONCE DONE BUILDING?
                }
                else if(rr.DrawingState == DrawingState.Normal)
                {
                     Print("Not moving or building");
                    CalculatePositionSize(rr);
                }        
            }
        }
    }​
    The problem is as soon as the first anchor is drawn, it triggers the event listener for new draw objects. This works for copy pasting objects but not building new ones. It can be filtered by using the drawing state.

    Solution 1: If the drawing state is building, launch another event listener to trigger once the drawing state property changes. Then calculate the data.
    Solution 2: If the drawing state is building, launch another event listener to trigger on the next mouse down. Then calculate the data.

    Solution 1 is a more accurate solution. How would solution 1 be implemented?

    Best regards,

    Ocean Trading Indicators
    Unsuitable
    NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

    #2
    Hello Unsuitable,

    Thanks for your post.

    The ChartScales has a ChartObjects object which is an observable collection and therefore has a CollectionChanged NotifyCollectionChangedEvent. Note that this is undocumented in the help guide.

    This would be called any time the chart objects collection has changed, such as when a user adds or removes a drawing tool to the chart.

    You could also check when a drawing state changes and do something within the script.

    On the forum thread linked below you could find an example of using this undocumented code in a custom NinjaScript indicator to detect if the chart objects collection has changed and check what drawing state a drawing object is in which you might find helpful.

    Hello, I have looked on the forums and references for this feature, it seems it's not available by default. I wonder if there's any third-party library I could use, or maybe there has been an update for it that I'm not aware of. I would like events for: DrawingTool Created DrawingTool Deleted DrawingTool Properties Changed


    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hello Brandon,

      That's where the original code snippet is from, Ill ask directly in that thread to see if people know how to tackle this.

      Best regards,

      Unsuitable
      Unsuitable
      NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      558 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      324 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      546 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      547 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X