void Receive(OrdersInformationUpdateResponseMessage message)
 {
     if (OrdersUpdatedEvent != null)
     {
         OrdersUpdatedEvent(this, message.AccountInfo, new string[] { }, message.OrderInformations, message.OrdersUpdates);
     }
 }
        AccountResponseMessage Receive(GetOrdersInformationMessage message)
        {
            IImplementation implementation = _implementation;

            if (implementation == null || OperationalState != OperationalStateEnum.Operational)
            {
                if (message.RequestResponse)
                {
                    return(new AccountResponseMessage(message.AccountInfo, false));
                }

                return(null);
            }

            string operationResultMessage;

            OrderInfo[] orderInfos;
            bool        operationResult = implementation.GetOrdersInfos(message.AccountInfo, message.OrderTickets, out orderInfos, out operationResultMessage);

            OrdersInformationUpdateResponseMessage response = new OrdersInformationUpdateResponseMessage(message.AccountInfo,
                                                                                                         orderInfos, operationResult);

            response.ResultMessage = operationResultMessage;

            if (message.RequestResponse)
            {
                return(response);
            }
            else
            {
                if (operationResult)
                {
                    SendResponding(message.TransportInfo, response);
                }
            }
            return(null);
        }
 void Receive(OrdersInformationUpdateResponseMessage message)
 {
     if (OrdersUpdatedEvent != null)
     {
         OrdersUpdatedEvent(this, message.AccountInfo, new string[] { }, message.OrderInformations, message.OrdersUpdates);
     }
 }
        AccountResponseMessage Receive(GetOrdersInformationMessage message)
        {
            IImplementation implementation = _implementation;
            if (implementation == null || OperationalState != OperationalStateEnum.Operational)
            {
                if (message.RequestResponse)
                {
                    return new AccountResponseMessage(message.AccountInfo, false);
                }

                return null;
            }

            string operationResultMessage;
            OrderInfo[] orderInfos;
            bool operationResult = implementation.GetOrdersInfos(message.AccountInfo, message.OrderTickets, out orderInfos, out operationResultMessage);

            OrdersInformationUpdateResponseMessage response = new OrdersInformationUpdateResponseMessage(message.AccountInfo,
                orderInfos, operationResult);
            response.ResultMessage = operationResultMessage;

            if (message.RequestResponse)
            {
                return response;
            }
            else
            {
                if (operationResult)
                {
                    SendResponding(message.TransportInfo, response);
                }
            }
            return null;
        }
        OrdersInformationUpdateResponseMessage Receive(GetOrdersInformationMessage message)
        {
            TracerHelper.TraceEntry();

            List<OrderInfo> result = new List<OrderInfo>();
            lock (this)
            {
                foreach (string id in message.OrderTickets)
                {
                    if (_orders.ContainsKey(id) && _orders[id].HasValue)
                    {
                        result.Add(_orders[id].Value);
                    }
                    else
                    {
                        _pendingOrdersInformations.Add(id);
                    }
                }
            }

            TracerHelper.TraceExit();

            OrdersInformationUpdateResponseMessage response = new OrdersInformationUpdateResponseMessage(_accountInfo, result.ToArray(), true);

            if (message.RequestResponse)
            {
                return response;
            }

            SendToSubscribers(response);
            return null;
        }
        ResponseMessage Receive(SubscribeToSourceAccountsUpdatesMessage message)
        {
            if (message.TransportInfo.OriginalSenderId.HasValue == false)
            {
                SystemMonitor.Error("Failed to establish original sender id.");
                return null;
            }

            if (message.Subscribe)
            {
                List<OrderInfo> orderInfos = new List<OrderInfo>();
                lock (this)
                {
                    _subscribers[message.TransportInfo.OriginalSenderId.Value] = message.TransportInfo;

                    // Send an update of current orderInfo to new subscriber.
                    foreach (KeyValuePair<string, OrderInfo?> pair in _orders)
                    {
                        if (pair.Value.HasValue)
                        {
                            orderInfos.Add(pair.Value.Value);
                        }
                    }
                }

                if (orderInfos.Count > 0)
                {
                    TransportInfo transportInfo = message.TransportInfo.Clone();

                    GeneralHelper.FireAndForget(delegate()
                    {// Make sure the result of the current call is returned before sending an initial update.
                        Thread.Sleep(500);

                        OrdersInformationUpdateResponseMessage updateMessage = new OrdersInformationUpdateResponseMessage(_accountInfo,
                            orderInfos.ToArray(), true);

                        this.SendResponding(transportInfo, updateMessage);
                    });
                }
            }
            else
            {
                lock (this)
                {
                    _subscribers.Remove(message.TransportInfo.OriginalSenderId.Value);
                }
            }

            if (message.RequestResponse)
            {
                return new ResponseMessage(true);
            }

            return null;
        }
        // >>
        public void OrderInformation(int orderTicket, int operationID, string orderSymbol, int orderType, decimal volume,
                                         decimal inputOpenPrice, decimal inputClosePrice, decimal inputOrderStopLoss, decimal inputOrderTakeProfit,
                                         decimal currentProfit, decimal orderSwap, int inputOrderPlatformOpenTime,
                                         int inputOrderPlatformCloseTime, int inputOrderExpiration, decimal orderCommission,
                                         string orderComment, int orderCustomID, bool operationResult, string operationResultMessage)
        {
            TracerHelper.TraceEntry(string.Format("ticketId[{0}], customId[{1}], operationId[{2}], symbol[{3}], SL[{4}, TP[{5}]", orderTicket.ToString(),
                orderCustomID.ToString(), operationID.ToString(), orderSymbol, inputOrderStopLoss.ToString(), inputOrderTakeProfit.ToString()));

            try
            {
                #region Preprocess Data

                decimal? openPrice = Convert(inputOpenPrice);
                decimal? closePrice = Convert(inputClosePrice);
                decimal? orderStopLoss = Convert(inputOrderStopLoss);
                decimal? orderTakeProfit = Convert(inputOrderTakeProfit);
                int? orderPlatformOpenTime = Convert(inputOrderPlatformOpenTime);
                int? orderPlatformCloseTime = Convert(inputOrderPlatformCloseTime);
                int? orderExpiration = Convert(inputOrderExpiration);

                // Perform dataDelivery fixes to convert to proper cases.
                bool isOpen = orderPlatformCloseTime.HasValue == false || orderPlatformCloseTime == 0;

                OrderStateEnum orderState = OrderStateEnum.Unknown;
                // According to documentataion this is the way to establish if order is closed, see here : http://docs.mql4.com/trading/OrderSelect
                if (isOpen)
                {
                    if (CommonFinancial.OrderInfo.TypeIsDelayed((OrderTypeEnum)orderType) == false
                       && orderPlatformOpenTime > 0)
                    {
                        orderState = OrderStateEnum.Executed;
                    }
                    else
                    {
                        orderState = OrderStateEnum.Submitted;
                    }
                }
                else
                {
                    orderState = OrderStateEnum.Closed;
                }

                if (orderState == OrderStateEnum.Executed)
                {// Since the integration might report close price for opened orders, at the current closing price.
                    closePrice = null;
                }

                DateTime? openTime = GeneralHelper.GenerateDateTimeSecondsFrom1970(orderPlatformOpenTime);
                DateTime? closeTime = GeneralHelper.GenerateDateTimeSecondsFrom1970(orderPlatformCloseTime);
                DateTime? expirationTime = GeneralHelper.GenerateDateTimeSecondsFrom1970(orderExpiration);

                CombinedDataSubscriptionInformation sessionSubscriptionInfo = GetDataSession(orderSymbol);

                if (sessionSubscriptionInfo == null)
                {
                    SystemMonitor.Error("Corresponding symbol [" + orderSymbol + "] session info not found.");
                    return;
                }

                #endregion

                bool isNewlyAcquired = false;
                OrderInfo? orderInformation = null;

                if (orderTicket > -1)
                {// Store call information for later use.
                    orderInformation = new OrderInfo(
                        orderTicket.ToString(), sessionSubscriptionInfo.SessionInformation.Info.Symbol, ConvertOrderType(orderType), orderState,
                        ConvertVolume(sessionSubscriptionInfo.SessionInformation.Info.LotSize, volume), openPrice, closePrice,
                        orderStopLoss, orderTakeProfit, currentProfit,
                        orderSwap, openTime, closeTime, openTime,
                        expirationTime, orderCommission, orderComment, orderCustomID.ToString());

                    lock (this)
                    {

                        isNewlyAcquired = _orders.ContainsKey(orderTicket.ToString()) == false || _orders[orderTicket.ToString()].HasValue == false;
                        _orders[orderTicket.ToString()] = orderInformation;
                    }

                    TracerHelper.TraceEntry(string.Format("Storing information, id[{0}], symb[{1}], customId[{2}], operationId[{3}].", orderTicket.ToString(), orderSymbol, orderCustomID.ToString(), operationID));
                }
                else
                {// This used to be flush call (send all stored to user), but currently not used.
                    SystemMonitor.NotImplementedError("Case not implemented.");
                }

                //if (isNewlyAcquired)
                {// Send a notification to subscribers, an order orderInfo was acquired.
                    OrdersInformationUpdateResponseMessage message = new OrdersInformationUpdateResponseMessage(_accountInfo,
                        new OrderInfo[] { orderInformation.Value }, true);

                    SendToSubscribers(message);
                }

            }
            catch (Exception ex)
            {// Make sure we handle any possible unexpected exceptions, as otherwise they bring the
                // entire package (MT4 included) down with a bad error.
                SystemMonitor.Error(ex.Message);
            }
        }