예제 #1
0
        public bool SavePendingOrder(PendingOrder order, out int orderID)
        {
            orderID = -1;
            using (var ctx = DatabaseContext.Instance.Make())
            {
                var account = ctx.ACCOUNT.FirstOrDefault(ac => ac.ID == order.AccountID);
                if (account == null)
                {
                    return(false);
                }

                // найти парный ордер, чтобы прописать OCO
                PENDING_ORDER pairOrder = null;

                if (order.PairOCO.HasValue && order.PairOCO.Value > 0)
                {
                    pairOrder = ctx.PENDING_ORDER.FirstOrDefault(o => o.ID == order.PairOCO.Value);
                    if (pairOrder == null)
                    {
                        order.PairOCO = null;
                    }
                    else
                    {
                        var anotherPair = ctx.PENDING_ORDER.FirstOrDefault(o => o.ID == pairOrder.ID);
                        if (anotherPair != null)
                        {
                            anotherPair.PairOCO          = null;
                            ctx.Entry(anotherPair).State = EntityState.Modified;
                        }
                    }
                }

                var pendingOrder = LinqToEntity.UndecorateLiveActiveOrder(order);
                account.PENDING_ORDER.Add(pendingOrder);

                try
                {
                    ctx.SaveChanges();
                    orderID = pendingOrder.ID;

                    // таки прописать ID парному ордеру
                    if (pairOrder != null)
                    {
                        pairOrder.PairOCO = orderID;
                        ctx.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error("Ошибка сохранения сущности PENDING_ORDER", ex);
                    ServiceManagerClientManagerProxy.Instance.NewOrderResponse(null,
                                                                               RequestStatus.ServerError, "crudsav");
                    return(false);
                }
            }
            // отправить уведомление клиенту
            ServiceManagerClientManagerProxy.Instance.NewPendingOrderResponse(order, RequestStatus.OK, "");
            return(true);
        }
예제 #2
0
        public bool DeletePendingOrder(PendingOrder orderDecorated,
                                       PendingOrderStatus status, int?positionID, string closeReason)
        {
            var order = LinqToEntity.UndecorateLiveActiveOrder(orderDecorated);

            using (var ctx = DatabaseContext.Instance.Make())
            {
                // получить текущую цену
                var pair        = QuoteStorage.Instance.ReceiveValue(order.Symbol);
                var priceClosed = pair == null ? 0 : order.Side > 0 ? pair.bid : pair.ask;

                var hist = new PENDING_ORDER_CLOSED
                {
                    OrderID       = order.ID,
                    AccountID     = order.AccountID,
                    Comment       = order.Comment,
                    ExpertComment = order.ExpertComment,
                    Magic         = order.Magic,
                    PairOCO       = order.PairOCO,
                    PriceFrom     = order.PriceFrom,
                    PriceTo       = order.PriceTo,
                    Side          = order.Side,
                    Status        = (int)status,
                    Stoploss      = order.Stoploss,
                    Takeprofit    = order.Takeprofit,
                    Symbol        = order.Symbol,
                    TimeClosed    = DateTime.Now,
                    Volume        = order.Volume,
                    PriceClosed   = (decimal)priceClosed,
                    Position      = positionID,
                    CloseReason   = closeReason
                };
                try
                {
                    ctx.PENDING_ORDER_CLOSED.Add(hist);

                    var ord = ctx.PENDING_ORDER.FirstOrDefault(p => p.ID == order.ID);
                    if (ord == null)
                    {
                        Logger.ErrorFormat("DeletePendingOrder - ордер {0} не найден", order.ID);
                        ServiceManagerClientManagerProxy.Instance.NewOrderResponse(null,
                                                                                   RequestStatus.ServerError, "crudsav");
                        return(false);
                    }
                    ctx.PENDING_ORDER.Remove(ord);
                    // сохранить изменения
                    ctx.SaveChanges();

                    return(true);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("Ошибка в DeletePendingOrder (#{0}): {1}", order.ID, ex);
                }
            }
            return(false);
        }