//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

        private void butStartStrategy_Click(object sender, RoutedEventArgs e)
        //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        {
            if (threadStrategy == null)
            //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
            {            //if (threadStrategy == null)
                threadStrategy = new Thread
                                 (
                    new ThreadStart
                        (() =>
                {
                    try
                    {
                        while (true)
                        //------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                        {                                                                //бесконечный цикл обработки стратегии
                            if (analizationPool.Count != 0)
                            {
                                ProcessStrategy();
                            }
                            Thread.Sleep(processStrategyRate);
                        }                                                                //бесконечный цикл обработки стратегии
                                                                                         //------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    }
                    catch (Exception ex)
                    {
                        //SendMessage("ERROR", "Error on Thread startegy:>>" + ex.ToString());
                    }
                    finally
                    {
                        //status = StrategyStatus.Stopped;
                    }
                }
                        )
                                 );
                threadStrategy.Name         = "StrategyThread_" + DateTime.Now.Ticks.ToString();
                threadStrategy.IsBackground = true;
                threadStrategy.Priority     = ThreadPriority.Normal;
            }            //if (threadStrategy == null)
            //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
            if (threadTrader == null)
            //------------------------------------------------------------------------------------------------------------------------------------------------------------------------
            {            //if (threadTrader == null)
                threadTrader = new Thread
                               (
                    new ThreadStart
                        (() =>
                {
                    try
                    {
                        while (true)
                        //------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                        {                                                                //бесконечный цикл обработки стратегии
                            if (ordersCancel.Count != 0)
                            //------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                            {                                                                    //отмена ордеров
                                MyOrder myOrder;
                                while (ordersCancel.TryDequeue(out myOrder))
                                {
                                    if (myOrder.ssOrder == null)
                                    {
                                        throw new Exception("Unexpected state of order");
                                        continue;
                                    }
                                    //lock (order)
                                    //{
                                    //if (order.Status == Trade.Order.OrderStatus.Cancelling)
                                    //{
                                    myOrder.Status = MyOrderStatus.SentToCancel;
                                    trader.CancelOrder(myOrder.ssOrder);
                                    //}
                                    //}
                                    //Thread.Sleep(1000);
                                }
                            }                                                                    //отмена ордеров
                            //------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                            if (ordersSend.Count != 0)
                            //------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                            {                                                                    //отправка ордеров
                                MyOrder myOrder;
                                while (ordersSend.TryDequeue(out myOrder))
                                {
                                    //_o.rwLock.EnterWriteLock();
                                    MyOrder _order = myOrder;
                                    StockSharp.BusinessEntities.Order ssOrd = new StockSharp.BusinessEntities.Order()
                                    {
                                        Trader    = trader,
                                        Portfolio = portfolio,
                                        Security  = security,
                                        Comment   = "GISMO",
                                        Direction = StockSharp.BusinessEntities.OrderDirections.Buy,                                                                                       // : StockSharp.BusinessEntities.OrderDirections.Sell,
                                        Price     = (decimal)_order.price,
                                        Type      = StockSharp.BusinessEntities.OrderTypes.Limit,
                                        Volume    = _order.amount
                                    };
                                    _order.ssOrder = ssOrd;
                                    //_order.rulerContainer = StockSharp.Algo.MarketRuleHelper.DefaultRuleContainer;

                                    var ruleRegFail = ssOrd.WhenRegisterFailed();
                                    var ruleReg     = ssOrd.WhenRegistered();
                                    //_order.RegisterToken(ruleRegFail.Token);
                                    //_order.RegisterToken(ruleReg.Token);
                                    ruleReg.Do((StockSharp.BusinessEntities.Order _ssOrd) =>
                                    {
                                        try
                                        {
                                            //GISMO.Trade.Order __order = QTrader._orders[_ssOrd.TransactionId];
                                            //__order.rwLock.EnterWriteLock();
                                            _order.Status = MyOrderStatus.AcceptedByServer;
                                            _order.AddStage("WhenRegistered:OrderRegistered");
                                            //__order.rwLock.ExitWriteLock();

                                            _order.OnOrderRegistered();
                                        }
                                        catch (Exception ex)
                                        {
                                            int i = 0;
                                        }
                                    })
                                    .Once()
                                    .Apply(StockSharp.Algo.MarketRuleHelper.DefaultRuleContainer)
                                    .Exclusive(ruleRegFail);

                                    ruleRegFail.Do((StockSharp.BusinessEntities.OrderFail f) =>
                                    {
                                        try
                                        {
                                            //GISMO.Trade.Order __order = QTrader._orders[f.Order.TransactionId];
                                            //__order.rwLock.EnterWriteLock();
                                            _order.Status = MyOrderStatus.Failed;
                                            _order.AddStage("WhenRegisteredFail:OrderRegisteredFail");
                                            //__order.rwLock.ExitWriteLock();

                                            _order.OnOrderRegisterFailed(new OrderFailEventArg()
                                            {
                                                order = _order,
                                                fail  = new OrderFail()
                                                {
                                                    ssFail = f
                                                }
                                            });
                                        }
                                        catch (Exception ex)
                                        {
                                            int i = 0;
                                        }
                                    })
                                    .Once()
                                    .Apply(StockSharp.Algo.MarketRuleHelper.DefaultRuleContainer)
                                    .Exclusive(ruleReg);

                                    var ruleCancelled     = ssOrd.WhenCanceled();
                                    var ruleCancelledFail = ssOrd.WhenCancelFailed();
                                    //_order.RegisterToken(ruleCancelled.Token);
                                    //_order.RegisterToken(ruleCancelledFail.Token);
                                    ruleCancelled.Do((StockSharp.BusinessEntities.Order _ssOrd) =>
                                    {
                                        try
                                        {
                                            //GISMO.Trade.Order __order = QTrader._orders[_ssOrd.TransactionId];
                                            //__order.rwLock.EnterWriteLock();
                                            _order.Status = MyOrderStatus.Cancelled;
                                            //__order.rwLock.ExitWriteLock();

                                            //StockSharp.Algo.MarketRuleHelper.DefaultRuleContainer.Rules.RemoveRulesByToken(ruleCancelled.Token, ruleCancelled);
                                            _order.OnOrderCancelled();
                                        }
                                        catch (Exception ex)
                                        {
                                            int i = 0;
                                        }
                                    })
                                    .Once()
                                    .Apply(StockSharp.Algo.MarketRuleHelper.DefaultRuleContainer)
                                    .Exclusive(ruleCancelledFail);

                                    ruleCancelledFail.Do((StockSharp.BusinessEntities.OrderFail f) =>
                                    {
                                        try
                                        {
                                            //GISMO.Trade.Order __order = QTrader._orders[f.Order.TransactionId];
                                            //__order.rwLock.EnterWriteLock();
                                            _order.Status = MyOrderStatus.Failed;
                                            _order.AddStage("WhenCancelFail:OrderCancelFail");
                                            //__order.rwLock.ExitWriteLock();

                                            _order.OnOrderCancelFailed(new OrderFailEventArg()
                                            {
                                                order = _order,
                                                fail  = new OrderFail()
                                                {
                                                    ssFail = f
                                                }
                                            });
                                        }
                                        catch (Exception ex)
                                        {
                                            int i = 0;
                                        }
                                    })
                                    .Once()
                                    .Apply(StockSharp.Algo.MarketRuleHelper.DefaultRuleContainer)
                                    .Exclusive(ruleCancelled);

                                    var ruleMatched = _order.ssOrder.WhenMatched();
                                    //_order.RegisterToken(ruleMatched.Token);
                                    ruleMatched.Do(() =>
                                    {
                                        try
                                        {
                                            //_order.rwLock.EnterWriteLock();
                                            _order.Status = MyOrderStatus.Filled;
                                            //_order.rwLock.ExitWriteLock();

                                            StockSharp.Algo.MarketRuleHelper.DefaultRuleContainer.Rules.RemoveRulesByToken(ruleMatched.Token, ruleMatched);
                                            _order.OnOrderMatched();
                                        }
                                        catch (Exception ex)
                                        {
                                            int i = 0;
                                        }
                                    })
                                    .Once()
                                    .Apply();

                                    var rulePartMatched = _order.ssOrder.WhenPartiallyMatched();
                                    //_order.RegisterToken(rulePartMatched.Token);
                                    rulePartMatched.Do(() =>
                                    {
                                        try
                                        {
                                            //_order.rwLock.EnterWriteLock();
                                            _order.Status = MyOrderStatus.PartlyFilled;
                                            //_order.rwLock.ExitWriteLock();

                                            _order.OnOrderPartlyMatched();
                                        }
                                        catch (Exception ex)
                                        {
                                            int i = 0;
                                        }
                                    }).Apply();

                                    var ruleNewTrades = _order.ssOrder.WhenNewTrades();
                                    //_order.RegisterToken(ruleNewTrades.Token);
                                    ruleNewTrades.Do((IEnumerable <MyTrade> trades) =>
                                    {
                                        try
                                        {
                                            //List<OrderExecution> execs = new List<OrderExecution>();
                                            //foreach (MyTrade mt in trades) execs.Add(new OrderExecution() { ssMyTrade = mt });
                                            //_order.OnOrderTrades(new OrderExecutionEventArg()
                                            //{
                                            //	order = _order,
                                            //	executions = execs
                                            //});
                                        }
                                        catch (Exception ex)
                                        {
                                            int i = 0;
                                        }
                                    })
                                    .Apply();
                                    _order.Status = MyOrderStatus.SentToServer;
                                    trader.RegisterOrder(ssOrd);
                                    //Thread.Sleep(1000);
                                    //QTrader._orders.Add(ssOrd.TransactionId, _order);

                                    //_o.rwLock.ExitWriteLock();
                                    //if (_order.Strategy != null) _order.Strategy.SendMessage("QSEND", "Order price " + _order.price.ToString() + " Sent (TransID = " + ssOrd.TransactionId + ")");
                                }
                            }                                                                    //отправка ордеров
                            //------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                            Thread.Sleep(processStrategyRate);
                        }                                                                //бесконечный цикл обработки стратегии
                                                                                         //------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    }
                    catch (Exception ex)
                    {
                        //SendMessage("ERROR", "Error on Thread startegy:>>" + ex.ToString());
                    }
                    finally
                    {
                        //status = StrategyStatus.Stopped;
                    }
                }
                        )
                               );
                threadTrader.Name         = "TraderThread_" + DateTime.Now.Ticks.ToString();
                threadTrader.IsBackground = true;
                threadTrader.Priority     = ThreadPriority.Normal;
            }            //if (threadTrader == null)
                         //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

            try
            {
                //bool tRunning = threadStrategy...ThreadState.HasFlag(ThreadState.Running);
                if (!threadStrategy.IsAlive)
                {
                    threadStrategy.Start();
                }
                if (!threadTrader.IsAlive)
                {
                    threadTrader.Start();
                }
            }
            catch (Exception ex)
            {
            }
        }