How to update you values live on ChartTrader UI



The task was to get selected chart trader account pnl and print its values out on a chart trader. The numeric value at the bottom of this custom chart trader reads the live pnl and also updates when it changes. 

In order for the value to get updated you have to additionally inherit the strategy class from INotifyPropertyChanged interface. Once you do that you also have to implement it like this:


	private double _accountPnl;
        public double AccountPnl
        {
            get { return _accountPnl; }
            set
            {
                if (_accountPnl != value)
                {
                    _accountPnl = value;
                    OnPropertyChanged();
                }
                
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            if (PropertyChanged != null) {
             PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); }
        } 









So once that is done and assuming you also need to get the selected account itself. I do it inside OnMarketDataUpdateEvent. like this:

        private void GetSelectedAccount()
        {
            ChartControl.Dispatcher.InvokeAsync((Action)(() =>
            {
                //You have to put the stuff below within this ChartControl.Dispatcher.InvokeAsync((Action)(() =>,
                //because you are trying to access something on a different thread.
                _accountSelector = Window.GetWindow(ChartControl.Parent).FindFirst("ChartTraderControlAccountSelector") as 
                    Gui.Tools.AccountSelector;
                var realized = _accountSelector.SelectedAccount
                    .GetAccountItem(AccountItem.RealizedProfitLoss, Currency.UsDollar).Value;
                var unrealized = _accountSelector.SelectedAccount
                    .GetAccountItem(AccountItem.UnrealizedProfitLoss, Currency.UsDollar).Value;
                
                AccountPnl = realized + unrealized;
                //Print(_accountPnl);
            }));
        }


Now finally you will need to implement the label itself like this:


                var label = new Label();
                //label.Content = "PNL: " + _accountPnl;
                label.DataContext = this;
                label.SetBinding(ContentControl.ContentProperty, new System.Windows.Data.Binding("AccountPnl"));
                label.FontSize = 14;
                label.FontWeight = FontWeights.Bold;
                label.HorizontalAlignment = HorizontalAlignment.Center;
                Grid.SetColumn(label, 1);
                _row7ButtonsGrid.Children.Add(label);


Have fun!)