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);
        }
        /// <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;

            ResponseMessage response = this.SendAndReceiveResponding <ResponseMessage>
                                           (SourceTransportInfo, message);

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

            if (response.OperationResult)
            {
                CloseOrderVolumeResponseMessage responseMessage = (CloseOrderVolumeResponseMessage)response;
                operationResultMessage = "Order closed.";
                closingPrice           = responseMessage.ClosingPrice;
                closingTime            = responseMessage.ClosingDateTime;

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

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

            operationResultMessage = response.OperationResultMessage;
            return(false);
        }
        /// <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;

            ResponseMessage response = this.SendAndReceiveResponding <ResponseMessage>
                                           (SourceTransportInfo, message);

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

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

            CloseOrderVolumeResponseMessage responseMessage = (CloseOrderVolumeResponseMessage)response;

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

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

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

            return(true);
        }
        /// <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;

            ResponseMessage response = this.SendAndReceiveResponding<ResponseMessage>
                (SourceTransportInfo, message);

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

            if (response.OperationResult)
            {
                CloseOrderVolumeResponseMessage responseMessage = (CloseOrderVolumeResponseMessage)response;
                operationResultMessage = "Order closed.";
                closingPrice = responseMessage.ClosingPrice;
                closingTime = responseMessage.ClosingDateTime;

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

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

            operationResultMessage = response.OperationResultMessage;
            return false;
        }
        /// <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;

            ResponseMessage response = this.SendAndReceiveResponding<ResponseMessage>
                (SourceTransportInfo, message);

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

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

            CloseOrderVolumeResponseMessage responseMessage = (CloseOrderVolumeResponseMessage)response;

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

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

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

            return true;
        }
        AccountResponseMessage Receive(CloseOrderVolumeMessage message)
        {
            IImplementation implementation = _implementation;
            if (implementation == null || OperationalState != OperationalStateEnum.Operational)
            {
                return new AccountResponseMessage(message.AccountInfo, false);
            }

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

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

            response.ResultMessage = operationResultMessage;
            return response;
        }