SubmitOrderResponceMessage Receive(SubmitOrderMessage message)
        {
            IImplementation implementation = _implementation;

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

            SubmitOrderResponceMessage responce;
            string operationResultMessage;
            string id;

            if (message.PerformSynchronous == false)
            {// We need to place order synchronously.
                responce = new SubmitOrderResponceMessage(message.AccountInfo, string.Empty, true);
            }
            else
            {// Just submit the order.
                id = implementation.SubmitOrder(message.AccountInfo, message.Symbol, message.OrderType, message.Volume, message.Slippage,
                                                message.DesiredPrice, message.TakeProfit, message.StopLoss, message.Comment, out operationResultMessage);

                responce = new SubmitOrderResponceMessage(message.AccountInfo, id, string.IsNullOrEmpty(id) == false);
                responce.ResultMessage = operationResultMessage;
            }

            return(responce);
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        public string SubmitOrder(AccountInfo account, Order order, Symbol symbol, OrderTypeEnum orderType,
                                  int volume, decimal?allowedSlippage, decimal?desiredPrice, decimal?takeProfit, decimal?stopLoss,
                                  string comment, out string operationResultMessage)
        {
            TracerHelper.Trace(this.Name);

            if (OperationalState != OperationalStateEnum.Operational)
            {
                operationResultMessage = "Attempted operations on non operational order executioner.";
                SystemMonitor.Error(operationResultMessage);
                return(null);
            }

            if (account.IsEmpty ||
                string.IsNullOrEmpty(account.Id) ||
                string.IsNullOrEmpty(account.Name))
            {
                operationResultMessage = "Account info on order execution provider not properly assigned.";
                return(null);
            }

            allowedSlippage = ProcessSlippage(allowedSlippage);

            SubmitOrderMessage request = new SubmitOrderMessage(account,
                                                                symbol, orderType, volume, desiredPrice, allowedSlippage, takeProfit, stopLoss, comment);

            request.RequestResponce    = true;
            request.PerformSynchronous = true;

            ResponceMessage responce = this.SendAndReceiveResponding <ResponceMessage>
                                           (SourceTransportInfo, request);

            if (responce == null)
            {// Time out.
                operationResultMessage = "Failed receive result for order request. In this scenario inconsistency may occur!";
                SystemMonitor.Error(operationResultMessage);
                return(null);
            }

            if (responce.OperationResult == false)
            {
                operationResultMessage = responce.OperationResultMessage;
                return(null);
            }

            SubmitOrderResponceMessage responceMessage = (SubmitOrderResponceMessage)responce;

            operationResultMessage = "Order submited.";

            //RaiseOrderUpdateEvent(account, order.Info, Order.UpdateTypeEnum.Submitted);

            return(responceMessage.OrderId);
        }
        // >>
        public void OrderOpened(string symbol, int operationID, int orderTicket, decimal openingPrice, 
            int orderOpenTime, bool operationResult, string operationResultMessage)
        {
            TracerHelper.Trace(symbol);

            try
            {
                OperationInformation operation = base.GetOperationById(operationID);

                DateTime? time = GeneralHelper.GenerateDateTimeSecondsFrom1970(orderOpenTime);
                if (time.HasValue == false)
                {
                    if (operation != null)
                    {
                        base.CompleteOperation(operationID, new ResponceMessage(false, "Failed to convert time for order."));
                    }

                    SystemMonitor.Error("Failed to convert time for order.");
                    return;
                }

                string orderId = orderTicket.ToString();
                if (orderTicket < 0)
                {// The system needs orderId empty to recognize the result as failure.
                    orderId = string.Empty;
                    operationResult = false;
                }

                if (operation != null)
                {
                    ResponceMessage message = null;

                    lock (this)
                    {
                        if (operation.Request is SubmitOrderMessage)
                        {
                            message = new SubmitOrderResponceMessage(_accountInfo, orderId, true);
                            message.OperationResultMessage = operationResultMessage;
                            message.OperationResult = operationResult;
                        }
                        else if (operation.Request is ExecuteMarketOrderMessage)
                        {
                            OrderInfo info = new OrderInfo(orderId, CreateSymbol(symbol), OrderTypeEnum.UNKNOWN, OrderStateEnum.Executed, int.MinValue);
                            info.OpenTime = time;
                            info.OpenPrice = openingPrice;

                            message = new ExecuteMarketOrderResponceMessage(_accountInfo, info, operationResult);
                            message.OperationResultMessage = operationResultMessage;
                        }
                        else
                        {
                            SystemMonitor.Error("Failed to establish placed order request type.");
                            message = new ResponceMessage(false);
                        }
                    }

                    base.CompleteOperation(operationID, message);
                }

                if (string.IsNullOrEmpty(orderId) == false)
                {
                    // Do an update of this order.
                    lock (this)
                    {
                        _pendingOrdersInformations.Add(orderId);
                    }
                }
            }
            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);
            }
        }
        SubmitOrderResponceMessage Receive(SubmitOrderMessage message)
        {
            IImplementation implementation = _implementation;
            if (implementation == null || OperationalState != OperationalStateEnum.Operational)
            {
                return new SubmitOrderResponceMessage(message.AccountInfo, string.Empty, false);
            }

            SubmitOrderResponceMessage responce;
            string operationResultMessage;
            string id;

            if (message.PerformSynchronous == false)
            {// We need to place order synchronously.
                responce = new SubmitOrderResponceMessage(message.AccountInfo, string.Empty, true);
            }
            else
            {// Just submit the order.

                id = implementation.SubmitOrder(message.AccountInfo, message.Symbol, message.OrderType, message.Volume, message.Slippage,
                    message.DesiredPrice, message.TakeProfit, message.StopLoss, message.Comment, out operationResultMessage);

                responce = new SubmitOrderResponceMessage(message.AccountInfo, id, string.IsNullOrEmpty(id) == false);
                responce.ResultMessage = operationResultMessage;
            }

            return responce;
        }