Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to Read from Chart Trader's Instrument Selector

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

    How to Read from Chart Trader's Instrument Selector

    Hi,

    I have a chart with 2 panels, each loaded with a different instrument. Both of them share a chart trader. As such, I need to read from the chart trader's instrument selector in order to know which instrument it is pointing to.

    I have a working code for reading from the main chart's instrument selector:

    PHP Code:
            private NinjaTrader.Gui.Tools.InstrumentSelector    myInstrumentSelector;
            private Instrument                                   myInstrument;
            
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                 .
                 .
                }
    
                else if (State == State.DataLoaded)
                {
                    FindAssignUIElementsByAutomationID();
                }
                
                else if (State == State.Terminated)
                {
                    if (ChartControl != null)
                    {
                        ChartControl.Dispatcher.InvokeAsync((Action)(() =>
                        {
                            if (myInstrumentSelector != null)
                            {
                                myInstrumentSelector.InstrumentChanged -= OnInstrumentChanged;
                            }
                        }));
                    }
                }
            }
    
    
            private void FindAssignUIElementsByAutomationID()
            {
                if (ChartControl != null)
                {
                    ChartControl.Dispatcher.Invoke((Action)(() =>
                    {
                        myInstrumentSelector                         = Window.GetWindow(ChartControl.OwnerChart).FindFirst("ChartWindowInstrumentSelector") as InstrumentSelector;
                        myInstrumentSelector.InstrumentChanged       += OnInstrumentChanged;
                        myInstrument                                 = myInstrumentSelector.Instrument;
                        
                        Print("Instrument loaded : " + myInstrument);
                    }));
                }
            }
            
    
            private void OnInstrumentChanged(object sender, EventArgs e)
            {
                myInstrument = sender as Cbi.Instrument;
                
                Print("Instrument changed to : " + myInstrument);
            }
            
            
            protected override void OnBarUpdate()
            {
                if ((State == State.Historical && CurrentBar != Count - 2))
                    return;
            
                if (myInstrumentSelector == null)
                {
                    FindAssignUIElementsByAutomationID();
                }
                
                Print ("myInstrument : " + myInstrument);
            }
            
        }​ 
    


    When I replace the Automation ID from
    PHP Code:
    ChartWindowInstrumentSelector 
    
    to
    PHP Code:
    ChartTraderControlInstrumentSelector 
    
    , I get the error Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.

    Can you advise what should I do to get it working?

    Thanks so much.

    #2
    Hello Rainmakersg,

    Try using the <NinjaTrader.Gui.Chart.ChartTrader>.Instrument instead. The forum post below has sample code.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello Rainmakersg,

      Try using the <NinjaTrader.Gui.Chart.ChartTrader>.Instrument instead. The forum post below has sample code.
      https://forum.ninjatrader.com/forum/...91#post1285491
      Hi Chelsea, thanks for the reply. After applying the code, I am able to read the instrument from Chart Trader's instrument selector upon loading the indicator.

      My next step is to be able to detect updates to the instrument selector whenever a new instrument is selected from the list.

      I am using the following for reference:
      PHP Code:
      Detecting updates in Chart Trader's account selector -> myAccountSelector.SelectionChanged
      Detecting updates in Chart Trader's quantity selector -> myQuantitySelector.ValueChanged 
      

      I tried the following for Chart Trader's instrument selector:
      PHP Code:
      myChartTraderControl.SourceUpdated
      myChartTraderControl.TargetUpdated 
      


      However, both don't work. Can you advise how to get it working?
      Thanks.

      Comment


        #4
        Hello Rainmakersg,

        Well.. this was not quite what I was expecting..

        The chart trader instrument selector doesn't appear to be an actual InstrumentSelector object but is just a ComboBox. Likely because it's just listing instruments on this specific chart (not all instruments) and doesn't actually change the instrument of the chart.

        So, I casted as a ComboBox just to use the .SelectionChanged event, and when the event fires, I still reference the <ChartTrader>.Instrument (which is updated).
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Chelsea, thanks so much for the reply. Yes it works now!

          I have a suggestion if you don't mind. I am thinking your new indicator "ChartTraderUIElementsFromAutomationIDsIndicatorExa mple_NT8.zip" has exactly the same name as the old one and it would be helpful to add to the name a version number or date of release so people will not be confused and know which is the latest version.

          I also hope you can be kind enough to answer the following question.
          I have 2 panels, each loaded with a different instrument. They both share the same chart trader. I have a buy button on each panel's main chart to open trades. After I click the buy button in a panel, a trade is opened. However, in order to see the order lines displayed in the panel, I need to select the correct instrument from the chart trader's instrument selector. This is a hassle and quite time consuming and I run the risk of clicking the wrong combo box in the midst of a live trade as they are very close to one another.

          Is it possible to use Ninjascript to switch the instrument selected on the chart trader?
          I wish to bind this code to the buy button so that whenever I click on the buy button in a panel, it will open a trade, and then it will run the code to switch to the corresponding instrument so that the corresponding order lines will be displayed in the panel.

          Your assistance would be very much appreciated.​
          Last edited by Rainmakersg; 08-30-2024, 04:19 AM.

          Comment


            #6
            Hello Rainmakersg,

            You could try setting the ComboBox SelectedIndex or SelectedValue.

            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi Chelsea, thanks very much. It works!

              Comment

              Latest Posts

              Collapse

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