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

        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);
        }
Пример #2
0
        // **********************************************************************

        void ProcessStrategy(object sender, EventArgs e)
        {
            if (working)
            {
                return;
            }

            working = true;

            if (ChartsManager.tm.Connected == TermConnection.None)
            {
                working = false;
                return;
            }

            var orders = from o in contextDb.orders
                         where o.active == true
                         orderby o.orderdatetime descending
                         select o;

            if (orders.Count() == 0)
            {
                working = false;
                return;
            }

            if (orders.Count() > 1)
            {
                MessageBox.Show("В базе данных слишком много ордеров количество - " + orders.Count().ToString() + ", должно быть - 1.", "Ошибка в программе " + cfg.FullProgName);
                working = false;
                return;
            }



            DateTime dtNow = DateTime.Now;

            var order = orders.First();

            if (order.stocks.ticker != cfg.u.SecCode)
            {
                working = false;
                return;
            }

            if (!isDateTimeValid(order.orderdatetime.Value)) // если данные не актуальные, возврат
            {
                working = false;
                return;
            }

            double priceValue = 0;

            if (order.price == 0)
            {
                if (lastPrice == 0)
                {
                    MessageBox.Show("В базе данных нет цены и lastPrice = 0.", "Ошибка в программе " + cfg.FullProgName);
                    working = false;
                    return;
                }
                else
                {
                    priceValue = lastPrice;
                    if (order.tradetypeid == 1)
                    {
                        priceValue = priceValue + cfg.u.PriceStep * 1;
                    }
                    else
                    if (order.tradetypeid == 2)
                    {
                        priceValue = priceValue - cfg.u.PriceStep * 1;
                    }
                }
            }
            else
            {
                priceValue = order.price.Value;
            }

            int qty = 0;

            if (order.qty == 0 || order.qty == null)
            {
                qty = 1;
            }
            else
            {
                qty = (int)order.qty.Value;
            }

            ChartsManager.tm.ExecAction(new OwnAction(
                                            TradeOp.Cancel,
                                            BaseQuote.None,
                                            0,
                                            0));               // отменяем ордер


            long OrderID = 0;

            switch (order.tradetypeid)
            {
            case 1:     // buy

                TermManager.Transaction tr = ChartsManager.tm.ExecAction(new OwnAction(
                                                                             TradeOp.Buy,
                                                                             BaseQuote.Absolute,
                                                                             priceValue,
                                                                             qty));
                while (tr.OId == 0 || OrderID == 0)
                {
                    OrderID = tr.OId;
                }
                break;

            case 2:     // sell

                TermManager.Transaction tr1 = ChartsManager.tm.ExecAction(new OwnAction(
                                                                              TradeOp.Sell,
                                                                              BaseQuote.Absolute,
                                                                              priceValue,
                                                                              qty));
                while (tr1.OId == 0 || OrderID == 0)
                {
                    OrderID = tr1.OId;
                }
                break;

            case 3:     // close

            {
                var posit = (from p in contextDb.positions
                             where p.active == true
                             select p).FirstOrDefault();

                if (posit == null)
                {
                    working = false;
                    return;
                }

                if (posit.tradetypeid == 1)         // buy -> need sell
                {
                    priceValue = priceValue - cfg.u.PriceStep * 1;
                    TermManager.Transaction tr2 = ChartsManager.tm.ExecAction(new OwnAction(
                                                                                  TradeOp.Sell,
                                                                                  BaseQuote.Absolute,
                                                                                  priceValue,
                                                                                  qty));
                    while (tr2.OId == 0 || OrderID == 0)
                    {
                        OrderID = tr2.OId;
                    }
                }
                else if (posit.tradetypeid == 2)         // sell -> need buy
                {
                    priceValue = priceValue + cfg.u.PriceStep * 1;
                    TermManager.Transaction tr3 = ChartsManager.tm.ExecAction(new OwnAction(
                                                                                  TradeOp.Buy,
                                                                                  BaseQuote.Absolute,
                                                                                  priceValue,
                                                                                  qty));
                    while (tr3.OId == 0 || OrderID == 0)
                    {
                        OrderID = tr3.OId;
                    }
                }
            }

            break;

            default:
                MessageBox.Show("Тип ордера - не реализовано!", "Ошибка в программе " + cfg.FullProgName);
                return;

                break;
            }

            order.OrderNum         = OrderID;
            order.executeddatetime = dtNow;
            order.active           = false;

            contextDb.SubmitChanges();

            working = false;
        }