/// <summary>
        /// Set instrument parameters for  Empty
        /// viewmodel. Calling when set real
        /// instrument selected.
        ///
        /// If instrument was trade previously
        /// settings file must be exists, load data from it.
        ///
        ///  If instrument was not trade previously
        ///  settings file is not exist use ticker data receieved
        ///  from the server
        ///
        /// Calling from CKernelTerminal.EditNonConnectedStock
        /// </summary>
        /// <param name="conId"></param>
        /// <param name="ticker">ticker data(instrument, step, decimals, etc)
        ///                      recieved from the server
        /// </param>
        public void SetEmptyVewModelInstrParams(int conId, CTIckerData ticker)
        {
            if (_logger == null)
            {
                _logger = new CLogger("MarketViewModel_" + ticker.TickerName);
            }



            string pathToConfig = CKernelTerminal.GetInstruemntPath(ticker.TickerName);

            //Config file for instrument is exists
            if (File.Exists(pathToConfig))
            {
                _instrumentConfig = _kernelTerminal.LoadInstrumentConfig(ticker.TickerName);
                CDataBinder.LoadMarketConfig(_instrumentConfig, this);

                //added 2018-02-27 for bitfinix capability
                if (_bUpdInstrParamsFromGrid)
                {
                    Step          = (double)ticker.Step;
                    DecimalVolume = ticker.DecimalVolume;
                    Decimals      = ticker.Decimals;
                }
            }
            //Config file for instrument is not exists
            else
            {
                TickerName    = ticker.TickerName;
                Step          = (double)ticker.Step;
                DecimalVolume = ticker.DecimalVolume;
                Decimals      = ticker.Decimals;


                _instrumentConfig.Name = ticker.TickerName;
                _instrumentConfig.MarketProperties.StockProperties.TickerName = ticker.TickerName;
                _instrumentConfig.MarketProperties.StockProperties.Step       = ticker.Step.ToString();
                _instrumentConfig.MarketProperties.StockProperties.Decimals   = ticker.Decimals.ToString();


                _instrumentConfig.MarketProperties.DealsProperties.Step = ticker.Step.ToString();
                _instrumentConfig.FileName = CKernelTerminal.GetInstruemntPath(ticker.TickerName);



                (new Task(SaveInstrumentConfig)).Start();
            }


            //note: ConId use for both cases
            ConnId = conId;
        }
 /// <summary>
 /// Call from  MarketViewModel.SetEmptyVewModelInstrParams
 /// </summary>
 /// <param name="instrumentConfig"></param>
 /// <param name="marketViewModel"></param>
 public static void LoadMarketConfig(CInstrumentConfig instrumentConfig, MarketViewModel marketViewModel)
 {
     try
     {
         LoadOneClassProperties(instrumentConfig.MarketProperties.StockProperties, marketViewModel);
         LoadOneClassProperties(instrumentConfig.MarketProperties.DealsProperties, marketViewModel);
         LoadOneClassProperties(instrumentConfig.MarketProperties.ClusterProperties, marketViewModel);
     }
     catch (Exception e)
     {
         CKernelTerminal.ErrorStatic("LoadMarketConfig");
     }
 }
 public static void SaveMarketConfig(CInstrumentConfig instrumentConfig, MarketViewModel marketViewModel)
 {
     try
     {
         UpdateOneClassProperties(instrumentConfig.MarketProperties.StockProperties, marketViewModel);
         UpdateOneClassProperties(instrumentConfig.MarketProperties.DealsProperties, marketViewModel);
         //2018-08-21 protect against file corruption on file saving when cluster was not loaded yet
         if (marketViewModel.TimeFrame != null)
         {
             UpdateOneClassProperties(instrumentConfig.MarketProperties.ClusterProperties, marketViewModel);
         }
     }
     catch (Exception e)
     {
         CKernelTerminal.ErrorStatic("UpdateMarketConfig", e);
     }
 }
        /// <summary>
        /// Call when Connected stock need replace with the new one
        /// 1) Find VieModel with oldTicker, reset IsAlive (stops network processing
        /// data thread)
        /// 2) Remove old ticker MarketViewModel from dictKIsinVMarketViewModel
        /// 3) Creates new ticker MarketViewModel and add it to _dictKIsinVMarketViewModel
        ///
        /// Call from CKernelTerminal.EditConnectedStock
        /// </summary>
        public void ReplaceMarketViewModel(int stockNum, string oldTicker, CStocksVisual stockVisual,
                                           ControlMarket controlMarket, CInstrumentConfig instrumentConfig)
        {
            lock (_dictKIsinVMarketViewModel)
            {
                var res = _dictKIsinVMarketViewModel.Values.FirstOrDefault(a => a.ControlMarket.StockNum == stockNum); //Where(key => key.StockNum == stockNum);

                if (res != null)
                {
                    res.IsAlive = false;
                    _dictKIsinVMarketViewModel.Remove(oldTicker);

                    _dictKIsinVMarketViewModel[stockVisual.Ticker] = new MarketViewModel(stockNum, controlMarket, _kernelTerminal,
                                                                                         stockVisual, instrumentConfig);
                    //upd 2017_08_27
                    // _dictKIsinVMarketViewModel[stockVisual.Ticker].IsConnectedToServer = true;
                }
            }
        }
        public MarketViewModel(int stockNum, ControlMarket controlMain, CKernelTerminal kernelTerminal,
                               CStocksVisual stockVisual, CInstrumentConfig instrumentConfig)
        {
            StockNum = stockNum;
            string tickerName = stockVisual.Ticker;


            IsAlive = true;
            IsDead  = false;


            SelectionMode = new CSelectionMode();
            string tf = instrumentConfig.MarketProperties.ClusterProperties.TimeFrame;

            _clusterProcessor = new CClusterProcessor(tickerName, tf);
            _userLevels       = new CUserLevels(tickerName);


            _eventRouterViewModel = new CEventRouterViewModel(this);

            // int conId = stockVisual.ConNumm;



            _orders = new OrderData[Params.MaxUserOrdersPerInstr];



            _terminalViewModel = CViewModelDispatcher.GetTerminalViewModel();



            int parUpdateStockPerSec = kernelTerminal.TerminalConfig.UpdateStockPerSec;

            _parSleep = (int)(1 / (double)parUpdateStockPerSec * 1000);

            _instrumentConfig = instrumentConfig;

            //    (new Thread(TestPar)).Start();


            GUIDispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;

            _controlMarket  = controlMain;
            _kernelTerminal = kernelTerminal;
            _communicator   = kernelTerminal.Communicator;

            //TextPriceSize = "10";
            //  StringHeight = "10";

            CurrAmountNum  = "0";
            ListWorkAmount = new List <CWorkAmount>();

            SetInstrumentParams(stockVisual);
            _vmUserPos = new ViewModelUserPos(Decimals, Step);



            _parMaxVolumeBar = Int32.MaxValue;

            //RaisePropertyChanged("Contract_ShortName");


            ConnId = stockVisual.ConNumm;// conId;



            MonitorUserOrders = new Dictionary <long, COrder>();


            if (tickerName != null)
            {
                _logger = new CLogger("MarketViewModel_" + tickerName);
            }

            Log("Start logging__________________________________________");



            InitBidsAsks();



            //TODO remove
            this.InitializeStockData();

            SetMouseMode(true);

            _threadDataReciever = new Thread(ThreadDataReciever);
            _threadDataReciever.Start();


            //BindWorkAmount();
        }
        /// <summary>
        /// Creates new MarketViewModel instance and adds it to dictioanary with viewmodels
        ///
        /// Call from
        /// 1) CKernelTerminal.AddOneStockFromConfig
        /// 2) CKernelTerminal.AddEmptyStock
        /// </summary>
        public void AddMarketViewModelNew(int stockNum, ControlMarket controlMarket, CStocksVisual stockVisual, CInstrumentConfig instrumentConfig)
        {
            //TODO access using unique id of market
            try
            {
                lock (_dictKIsinVMarketViewModel)
                    _dictKIsinVMarketViewModel.Add(stockVisual.Ticker,
                                                   new MarketViewModel(stockNum, controlMarket, _kernelTerminal, stockVisual, instrumentConfig));
            }
            catch (Exception e)

            {
                Error("AddMarketViewModelNew", e);
            }
        }