Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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


    Brandon H.NinjaTrader Customer Service

    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 fx.practic, 10-15-2013, 12:53 AM
      5 responses
      5,404 views
      0 likes
      Last Post Bidder
      by Bidder
       
      Started by Shai Samuel, 07-02-2022, 02:46 PM
      4 responses
      95 views
      0 likes
      Last Post Bidder
      by Bidder
       
      Started by DJ888, Yesterday, 10:57 PM
      0 responses
      8 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by MacDad, 02-25-2024, 11:48 PM
      7 responses
      160 views
      0 likes
      Last Post loganjarosz123  
      Started by Belfortbucks, Yesterday, 09:29 PM
      0 responses
      9 views
      0 likes
      Last Post Belfortbucks  
      Working...
      X