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:
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
ChartWindowInstrumentSelector
ChartTraderControlInstrumentSelector
Can you advise what should I do to get it working?
Thanks so much.

Comment