Exemple #1
0
        /// <summary>
        /// преобразовать запрос на вход / выход из рынка в отложенный ордер
        /// </summary>        
        private bool MakeMarketRequest(Account account, string symbol, int? magic,
            int volume, int side,
            OrderType orderPricing,
            decimal requestedPrice, decimal slippagePoints,  
            string description,
            int? closingPositionId, string comment, string expertComment, float? trailingLevel, float? trailingGoal,
            out RequestStatus error,
            out TradeSharp.ProviderProxyContract.Entity.MarketOrder req)
        {
            Logger.InfoFormat("Dealer [{0}, account: {1}]: MakeMarketRequest({2} {3})",
                DealerCode, account.ID, side > 0 ? "BUY" : "SELL", symbol);
            error = RequestStatus.OK;
            req = new TradeSharp.ProviderProxyContract.Entity.MarketOrder(new BrokerOrder());

            if (orderPricing == OrderType.Instant && slippagePoints != 0)
            {
                var slipAbs = DalSpot.Instance.GetAbsValue(symbol, slippagePoints);
                if (slipAbs == 0)
                {
                    Logger.ErrorFormat("Ошибка в FixDealer - нет информации по символу {0} (пересчет проскальзывания)", symbol);
                    errorStorage.AddMessage(new ErrorMessage(DateTime.Now, ErrorMessageType.ОшибкаОтправки,
                        string.Format("Ошибка в FixDealer - нет информации по символу {0} (пересчет проскальзывания)", symbol),
                        null));
                    error = RequestStatus.ServerError;
                    return false;
                }
                req.brokerOrder.Slippage = slipAbs;
            }
            int posId = 0;
            if (closingPositionId == null || closingPositionId == 0)
            {
                var order = new MarketOrder
                                {
                                    AccountID = account.ID,
                                    Side = side,
                                    ID = 0,
                                    Volume = volume,
                                    Symbol = symbol,
                                    Comment = comment,
                                    ExpertComment = expertComment,
                                    Magic = magic,
                                    TimeEnter = DateTime.Now,
                                    TrailLevel1 = trailingLevel,
                                    TrailTarget1 = trailingGoal,
                                    PriceEnter = (float) requestedPrice,
                                    State = PositionState.StartOpened,
                                };

                // создание позы - обработано
                ServerInterface.SaveOrderAndNotifyClient(order, out posId);
                if (posId <= 0)
                {
                    Logger.ErrorFormat("Ошибка в FixDealer - ошибка создания открытой позиции в БД по {0}", symbol);
                    errorStorage.AddMessage(new ErrorMessage(DateTime.Now, ErrorMessageType.ОшибкаОтправки,
                                                             string.Format(
                                                                 "Ошибка в FixDealer - ошибка создания открытой позиции в БД по {0}",
                                                                 symbol),
                                                             null));
                    error = RequestStatus.ServerError;
                    return false;
                }
            }
            else
            {// закрытие позы
                MarketOrder order;
                ServerInterface.GetMarketOrder(closingPositionId.Value, out order);
                if (order == null)
                {
                    error = RequestStatus.ServerError;
                    Logger.ErrorFormat("FixDealer - MakeMarketRequest - order {0} not found", closingPositionId.Value);
                    return false;
                }
                order.State = PositionState.StartClosed;
                if (!ServerInterface.ModifyMarketOrder(order))
                {
                    error = RequestStatus.ServerError;
                    Logger.ErrorFormat("FixDealer - MakeMarketRequest - cannot modify order {0}", closingPositionId.Value);
                    return false;
                }
            }

            req.AccountGroupCode = account.Group;
            req.brokerOrder.Instrument = Instrument.Spot;
            req.brokerOrder.OrderPricing = orderPricing == OrderType.Instant ? OrderPricing.Instant : OrderPricing.Market;
            if (req.brokerOrder.OrderPricing == OrderPricing.Instant)
                req.brokerOrder.RequestedPrice = requestedPrice == 0 ? (decimal?)null : requestedPrice;
            req.brokerOrder.Side = side;
            req.brokerOrder.Volume = volume;
            req.brokerOrder.Ticker = symbol;
            req.brokerOrder.RequestId = posId != 0 ? posId : (int)RequestUniqueId;
            req.brokerOrder.DealerCode = DealerCode;
            req.brokerOrder.TimeCreated = DateTime.Now;
            req.brokerOrder.ClosingPositionID = closingPositionId;
            req.brokerOrder.AccountID = account.ID;
            req.brokerOrder.Magic = magic;
            req.brokerOrder.Comment = comment;
            req.brokerOrder.ExpertComment = expertComment;

            requestWatchdog.AddRequest(new RequestWatchdogItem
                { requestId = req.brokerOrder.RequestId, requestTime = DateTime.Now,
                    requestSymbol = symbol, requestType = (DealType)side });

            // сохранить сообщение в БД
            try
            {
                ServerInterface.SaveProviderMessage(req.brokerOrder);
            }
            catch (Exception ex)
            {
                error = RequestStatus.SerializationError;
                Logger.ErrorFormat("Дилер {0}: ошибка сохранения запроса брокеру: {1}",
                    DealerCode, ex);
                errorStorage.AddMessage(new ErrorMessage(DateTime.Now, ErrorMessageType.ОшибкаОтправки,
                        string.Format("Дилер {0}: ошибка сохранения запроса брокеру", DealerCode), ex));
                return false;
            }

            return true;
        }
 public RequestedOrder(TradeTransactionRequest request, TradeSharp.Contract.Entity.MarketOrder requestedOrder)
 {
     this.request = request;
     this.requestedOrder = requestedOrder;
 }