I have followed the examples given by NT using AutomationID to grab the account name and any changes therein using
ChartTrader - it works fine but I get the odd InvokeTarget error and my Print to output only works for one account??
This is the account i set here
if (State == State.SetDefaults) { currentAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101");
In Data Loaded I find my UIElements as such AND load up account item handlers
else if (State == State.DataLoaded) { FindAssignUIElementsByAutomationID(); if ( currentAccount != null) { currentAccount.AccountItemUpdate += OnAccountItemUpdate; currentAccount.PositionUpdate += OnPositionUpdate; currentAccount.ExecutionUpdate += OnExecutionUpdate; } }
private void FindAssignUIElementsByAutomationID() { if (ChartControl != null) { // chart controls run on the UI thread. Dispatcher Invoke is used to access the thread. // Typically, the InvokeAsync() is used access the UI thread asynchronously when it is ready. However, if this information is needed immediately, use Invoke so that this blocks the NinjaScript thread from continuing until this operation is complete. // Beware that using Invoke improperly can result in deadlocks. // This example uses Invoke so that the UI control values are available as the historical data is processing ChartControl.Dispatcher.Invoke((Action)(() => { // the window of the chart Window chartWindow = Window.GetWindow(ChartControl.Parent); // find the ChartTrader account selector by AutomationID accountSelector = chartWindow.FindFirst("ChartTraderControlAccountSelector") as AccountSelector; if (accountSelector != null) { accountSelector.SelectionChanged += AccountSelector_SelectionChanged; currentAccount = accountSelector.SelectedAccount; Print("ďn Find "+currentAccount); } // find the ChartTrader atmStrategy selector by AutomationID atmStrategySelector = chartWindow.FindFirst("ChartTraderControlATMStrategySelector") as Gui.NinjaScript.AtmStrategy.AtmStrategySelector; if (atmStrategySelector != null) { atmStrategySelector.SelectionChanged += AtmStrategySelector_SelectionChanged; currentAtmStrategy = atmStrategySelector.SelectedAtmStrategy; } })); } }
private void AccountSelector_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { if (currentAccount != null) currentAccount.OrderUpdate -= CurrentAccount_OrderUpdate; currentAccount = accountSelector.SelectedAccount; if (currentAccount != null) Print("ChartTraderControlAccountSelector SelectedAccount: '" + currentAccount.DisplayName + "' assigned to local variable"); else Print("ChartTraderControlAccountSelector null"); }
I have spent hours and numerous iterations to sort this and I can't do it.
If I remove the line in set Defaults ( where I set account to Sim101) the handlers are not initiated. So I definitely need to set the account object to something but why does it 'stick'.
Can anyone assist and tell me where I am going wrong?
Comment