// **********************************************************************

        public void PutLastPrice(double price)
        {
            if (orders.Count > 0)
            {
                lock (orders)
                {
                    int i = 0;

                    while (i < orders.Count)
                    {
                        StopOrder order = orders[i];

                        if (order.Quantity > 0)
                        {
                            if (price >= order.StopPrice)
                            {
                                tmgr.ExecAction(new OwnAction(TradeOp.Buy, BaseQuote.Absolute, order.ExecPrice, order.Quantity));

                                orders.RemoveAt(i);
                                dataReceiver.PutOwnOrder(new OwnOrder(order.Id, order.StopPrice, 0, order.Quantity));
                            }
                            else
                            {
                                i++;
                            }
                        }
                        else
                        {
                            if (price <= order.StopPrice)
                            {
                                tmgr.ExecAction(new OwnAction(TradeOp.Sell, BaseQuote.Absolute, order.ExecPrice, -order.Quantity));

                                orders.RemoveAt(i);
                                dataReceiver.PutOwnOrder(new OwnOrder(order.Id, order.StopPrice, 0, order.Quantity));
                            }
                            else
                            {
                                i++;
                            }
                        }
                    }
                }
            }
        }
        private void PredictFXChartsMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            tmgr.ExecAction(new OwnAction(
                                TradeOp.Cancel,
                                BaseQuote.None,
                                0,
                                0));             // отменяем ордер

            MarketProvider.Deactivate();
            ChartsManager.Deactivate();

            tmgr.Disconnect();
        }
        // **********************************************************************

        public void PutOwnTrade(OwnTrade trade)
        {
            // ------------------------------------------------------------

            int nq = this.Quantity + trade.Quantity;

            if (Math.Sign(nq) != Math.Sign(this.Quantity))
            {
                this.pricesum = trade.Price * nq;
            }
            else
            {
                this.pricesum += trade.Price * trade.Quantity;
            }

            this.Quantity = nq;

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

            if (stopOrderId != 0)
            {
                tmgr.KillStopOrder(stopOrderId);
                stopOrderId = 0;
            }

            if (takeProfit != null)
            {
                tmgr.CancelAction(takeProfit);
                takeProfit = null;
            }

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

            if (Quantity == 0)
            {
                Price = 0;
            }
            else
            {
                Price = pricesum / Quantity;

                if (Quantity > 0)
                {
                    if (cfg.u.StopOffset != 0)
                    {
                        stopOrderPrice = PredictFXCharts.Price.Ceil(Price - cfg.u.StopOffset);

                        stopOrderId = tmgr.CreateStopOrder(
                            stopOrderPrice,
                            stopOrderPrice - cfg.u.StopSlippage,
                            -Quantity);
                    }

                    if (cfg.u.TakeOffset != 0)
                    {
                        takeProfit = tmgr.ExecAction(new OwnAction(
                                                         TradeOp.Sell,
                                                         BaseQuote.Absolute,
                                                         PredictFXCharts.Price.Ceil(Price + cfg.u.TakeOffset),
                                                         Quantity));
                    }
                }
                else
                {
                    if (cfg.u.StopOffset != 0)
                    {
                        stopOrderPrice = PredictFXCharts.Price.Floor(Price + cfg.u.StopOffset);

                        stopOrderId = tmgr.CreateStopOrder(
                            stopOrderPrice,
                            stopOrderPrice + cfg.u.StopSlippage,
                            -Quantity);
                    }

                    if (cfg.u.TakeOffset != 0)
                    {
                        takeProfit = tmgr.ExecAction(new OwnAction(
                                                         TradeOp.Buy,
                                                         BaseQuote.Absolute,
                                                         PredictFXCharts.Price.Floor(Price - cfg.u.TakeOffset),
                                                         -Quantity));
                    }
                }
            }

            dataReceiver.PutPosition(Quantity, Price);
        }