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); }
/// <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); }
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; }
// >> 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 ResponseMessage(false, "Failed to convert order close time.")); } SystemMonitor.Error("Failed to convert order close time."); return; } if (operation != null) { CloseOrderVolumeResponseMessage message; lock (this) { message = new CloseOrderVolumeResponseMessage(_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); } }