AccountResponceMessage Receive(CloseOrderVolumeMessage message)
        {
            IImplementation implementation = _implementation;

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

            decimal  closingPrice;
            DateTime closingTime;
            string   modifiedId;
            string   operationResultMessage;

            CloseOrderVolumeResponceMessage responce;

            if (implementation.CloseOrCancelOrder(message.AccountInfo, message.OrderId, message.OrderTag, message.Slippage, message.Price,
                                                  out closingPrice, out closingTime, out modifiedId, out operationResultMessage))
            {
                responce = new CloseOrderVolumeResponceMessage(message.AccountInfo, message.OrderId, modifiedId,
                                                               closingPrice, closingTime, true);
            }
            else
            {
                responce = new CloseOrderVolumeResponceMessage(message.AccountInfo, message.OrderId, string.Empty,
                                                               decimal.Zero, DateTime.MinValue, false);
            }

            responce.ResultMessage = operationResultMessage;
            return(responce);
        }
Example #2
0
        /// <summary>
        /// Helper.
        /// </summary>
        bool DoCloseOrder(AccountInfo account, Order order, decimal?allowedSlippage, decimal?desiredPrice, out decimal closingPrice,
                          out DateTime closingTime, out string modifiedId, out string operationResultMessage)
        {
            TracerHelper.Trace(this.Name);

            closingPrice = decimal.MinValue;
            closingTime  = DateTime.MinValue;
            modifiedId   = order.Id;

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

            allowedSlippage = ProcessSlippage(allowedSlippage);

            CloseOrderVolumeMessage message = new CloseOrderVolumeMessage(account, order.Symbol, order.Id,
                                                                          order.Tag, desiredPrice, allowedSlippage);

            message.PerformSynchronous = true;

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

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

            if (responce.OperationResult)
            {
                CloseOrderVolumeResponceMessage responceMessage = (CloseOrderVolumeResponceMessage)responce;
                operationResultMessage = "Order closed.";
                closingPrice           = responceMessage.ClosingPrice;
                closingTime            = responceMessage.ClosingDateTime;

                SystemMonitor.CheckError(order.Id == responceMessage.OrderId.ToString(), "Order id mismatch [" + order.Id + " / " + responceMessage.OrderId + "].");

                modifiedId = responceMessage.OrderModifiedId.ToString();
                return(true);
            }

            operationResultMessage = responce.OperationResultMessage;
            return(false);
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        public bool DecreaseOrderVolume(AccountInfo account, Order order, decimal volumeDecreasal, decimal?allowedSlippage,
                                        decimal?desiredPrice, out decimal decreasalPrice, out string modifiedId, out string operationResultMessage)
        {
            TracerHelper.Trace(this.Name);
            modifiedId     = order.Id;
            decreasalPrice = decimal.MinValue;

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

            allowedSlippage = ProcessSlippage(allowedSlippage);

            CloseOrderVolumeMessage message = new CloseOrderVolumeMessage(account, order.Symbol, order.Id,
                                                                          order.Tag, volumeDecreasal, desiredPrice, allowedSlippage);

            message.PerformSynchronous = true;

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

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

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

            CloseOrderVolumeResponceMessage responceMessage = (CloseOrderVolumeResponceMessage)responce;

            operationResultMessage = "Order volume decreased.";
            decreasalPrice         = responceMessage.ClosingPrice;

            // When modified, order changes its Id.
            modifiedId = responceMessage.OrderModifiedId;

            RaiseOrderUpdateEvent(account, responceMessage.OrderId, order.Info, ActiveOrder.UpdateTypeEnum.VolumeChanged);

            return(true);
        }
        // >>
        public void OrderClosed(string symbol, int operationID, int orderTicket, int orderNewTicket, decimal closingPrice, int orderCloseTime, bool operationResult, string operationResultMessage)
        {
            TracerHelper.Trace(symbol);

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

                string orderNewTicketString = orderNewTicket.ToString();
                if (orderNewTicket < 1)
                {// Anything below 1 (0, -1 etc) is considered empty.
                    orderNewTicketString = string.Empty;
                }

                DateTime? closeTime = GeneralHelper.GenerateDateTimeSecondsFrom1970(orderCloseTime);

                if (closeTime.HasValue == false)
                {
                    if (operation != null)
                    {
                        base.CompleteOperation(operationID, new ResponceMessage(false, "Failed to convert order close time."));
                    }
                    SystemMonitor.Error("Failed to convert order close time.");
                    return;
                }

                if (operation != null)
                {
                    CloseOrderVolumeResponceMessage message;
                    lock (this)
                    {
                        message = new CloseOrderVolumeResponceMessage(_accountInfo, orderTicket.ToString(), orderNewTicketString, closingPrice, closeTime.Value, operationResult);
                        message.OperationResultMessage = operationResultMessage;
                    }

                    base.CompleteOperation(operationID, message);
                }
                else
                {
                    SystemMonitor.Error("Failed to finish order close operation as expected.");
                }

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

            decimal closingPrice;
            DateTime closingTime;
            string modifiedId;
            string operationResultMessage;

            CloseOrderVolumeResponceMessage responce;
            if (implementation.CloseOrCancelOrder(message.AccountInfo, message.OrderId, message.OrderTag, message.Slippage, message.Price,
                out closingPrice, out closingTime, out modifiedId, out operationResultMessage))
            {
                responce = new CloseOrderVolumeResponceMessage(message.AccountInfo, message.OrderId, modifiedId,
                    closingPrice, closingTime, true);
            }
            else
            {
                responce = new CloseOrderVolumeResponceMessage(message.AccountInfo, message.OrderId, string.Empty,
                    decimal.Zero, DateTime.MinValue, false);
            }

            responce.ResultMessage = operationResultMessage;
            return responce;
        }