Esempio n. 1
0
 void Receive(OrdersInformationUpdateResponceMessage message)
 {
     if (OrdersUpdatedEvent != null)
     {
         OrdersUpdatedEvent(this, message.AccountInfo, new string[] { }, message.OrderInformations, message.OrdersUpdates);
     }
 }
        AccountResponceMessage Receive(GetOrdersInformationMessage message)
        {
            IImplementation implementation = _implementation;

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

                return(null);
            }

            string operationResultMessage;

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

            OrdersInformationUpdateResponceMessage responce = new OrdersInformationUpdateResponceMessage(message.AccountInfo,
                                                                                                         orderInfos, operationResult);

            responce.ResultMessage = operationResultMessage;

            if (message.RequestResponce)
            {
                return(responce);
            }
            else
            {
                if (operationResult)
                {
                    SendResponding(message.TransportInfo, responce);
                }
            }
            return(null);
        }
        OrdersInformationUpdateResponceMessage 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();

            OrdersInformationUpdateResponceMessage responce = new OrdersInformationUpdateResponceMessage(_accountInfo, result.ToArray(), true);

            if (message.RequestResponce)
            {
                return responce;
            }

            SendToSubscribers(responce);
            return null;
        }
        ResponceMessage 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);

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

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

            if (message.RequestResponce)
            {
                return new ResponceMessage(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();

            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;

                lock (this)
                {
                    if (orderTicket > -1)
                    {// Store call information for later use.
                        TracerHelper.TraceEntry("Storing information.");

                        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());

                        isNewlyAcquired = _orders.ContainsKey(orderTicket.ToString()) == false || _orders[orderTicket.ToString()].HasValue == false;
                        _orders[orderTicket.ToString()] = orderInformation;
                    }
                    else
                    {// This is the flush call - send all stored to user.

                        SystemMonitor.NotImplementedWarning("Case not implemented.");

                    //    //TracerHelper.TraceEntry("Sending information.");
                    //    //OrdersInformationResponceMessage message = new OrdersInformationResponceMessage(_sessionInformation.Info, operationID, _pendingInformations.ToArray(), operationResult);
                    //    //message.OperationResultMessage = operationResultMessage;
                    //    //SendToSubscriber(message);
                    //    //// Clear for new operations.
                    //    //_totalOrderInformationsOperationID = -1;
                    //    //_pendingInformations.Clear();
                    }
                }

                //if (isNewlyAcquired)
                {// Send a notification to subscribers, an order orderInfo was acquired.
                    OrdersInformationUpdateResponceMessage message = new OrdersInformationUpdateResponceMessage(_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);
            }
        }
 void Receive(OrdersInformationUpdateResponceMessage message)
 {
     if (OrdersUpdatedEvent != null)
     {
         OrdersUpdatedEvent(this, message.AccountInfo, new string[] { }, message.OrderInformations, message.OrdersUpdates);
     }
 }
        AccountResponceMessage Receive(GetOrdersInformationMessage message)
        {
            IImplementation implementation = _implementation;
            if (implementation == null || OperationalState != OperationalStateEnum.Operational)
            {
                if (message.RequestResponce)
                {
                    return new AccountResponceMessage(message.AccountInfo, false);
                }

                return null;
            }

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

            OrdersInformationUpdateResponceMessage responce = new OrdersInformationUpdateResponceMessage(message.AccountInfo,
                orderInfos, operationResult);
            responce.ResultMessage = operationResultMessage;

            if (message.RequestResponce)
            {
                return responce;
            }
            else
            {
                if (operationResult)
                {
                    SendResponding(message.TransportInfo, responce);
                }
            }
            return null;
        }