Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Access other open charts from one chart

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

    Access other open charts from one chart

    Hi, I'm trying to develop an indicator which is supposed to access (when applied on anyone of the opened charts) all other open charts for two purposes

    A): Read which instrument i.e. CL 08-20 is open on each of these chart.
    B): If any of these charts contain the instrument I'm looking for, then add a special indicator on that specific chart.

    I want to ask whether it is technically supported by NinjaTrader 8/NinjaScript C# to obtain the above two objectives.
    ---------------------------------------------------------------------------------------------------------------------------------------------------------
    Secondly, I tried the following code in my indicator's OnBarUpdate() method.

    "Chart chartWindow = Window.GetWindow(ChartControl.Parent) as Chart;"

    This line of code gives me following error: "The calling thread cannot access this object because a different thread owns it." I guess may be OnBarUpdate() is not the right place for this line of code but not sure. I need some help on correct use of a piece of code that starts with this line.
    ----------------------------------------------------------------------------------------------------------------------------------------------------------

    I would be really grateful if someone can share similar type of code that has some resemblance with what I'm trying to achieve. Thanks very much.

    #2
    Hello Mubeen Haider,

    Thanks for your post.

    You can use a Dispatcher to access ChartControl properties from threads that do not have direct access to it. ChartControl would reside on the UI thread when OnBarUpdate is processed historically with a thread pool and in realtime on an instrument thread.

    Multi Threading Considerations - https://ninjatrader.com/support/help...-threading.htm

    If you want to loop through all open windows and print the instruments added in each chart, you can do something like:

    Code:
    foreach (var window in NinjaTrader.Core.Globals.AllWindows)
    {
        // check if the found window is a Chart window, if not continue looking
        if (!(window is NinjaTrader.Gui.Chart.Chart)) continue;
    
        window.Dispatcher.InvokeAsync(new Action(() =>
        {
            // try to cast as a Chart, if it fails it will be null
            var foundChart = window as NinjaTrader.Gui.Chart.Chart;
    
            // make sure we found a chart
            if (foundChart == null) return;
    
            foreach (ChartBars bars in foundChart.ActiveChartControl.BarsArray)
                Print(bars.Bars.Instrument.FullName);
        }));
    
    }
    There is not a supported means to programmatically add an indicator to a chart. We would suggest prebuilding workspaces/templates that would be used with your indicator(s.)

    Please let us know if you have any additional questions.

    Comment


      #3
      Hello Jim, Thanks very much for your response. It's very useful for me. Just one more question.
      My indicator plots lines on the chart on which this indicator is applied. Is it possible to programmatically copy these lines to other charts similar to "Attach to" function available by right clicking a chart -> Drawing Tools -> Drawing Objects -> Attach to. Can I programmatically perform such attachment of lines plotted by an indicator from one chart to other open charts.

      Comment


        #4
        Hello Mubeen Haider,

        Indicators can't be attached to all charts like drawing objects can. However, if you want to get creative, you can consider devising a way to have separate indicators communicate with each other so when one draws, it signals another indicator on another chart to draw.

        This starts to go beyond what we can support since scripts are not designed to communicate with eachother out of box, but if you add some public methods to your indicators, you could use those to tell another indicator to draw. There is certainly going to be a lot more involved, but I have attached an example that may help get you started. (The example is meant to be helpful, but this would be the furthest we could go down this path.)

        We look forward to assisting.
        Attached Files

        Comment


          #5
          Thank you so much Mr. Jim! Your advice has been very very useful for me. Have a nice day!

          Comment

          Latest Posts

          Collapse

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