/// <summary> /// Main SynchronousExecute method. /// </summary> public bool SynchronousExecute(AccountInfo account, Order order, Symbol symbol, OrderTypeEnum orderType, int volume, decimal?allowedSlippage, decimal?desiredPrice, decimal?takeProfit, decimal?stopLoss, string comment, TimeSpan operationTimeOut, out OrderInfo?info, out string operationResultMessage) { TracerHelper.Trace(this.Name); info = null; operationResultMessage = string.Empty; if (OperationalState != OperationalStateEnum.Operational) { operationResultMessage = "Attempted operations on non operational order executioner."; SystemMonitor.Error(operationResultMessage); return(false); } if (account.IsEmpty || string.IsNullOrEmpty(account.Id) || string.IsNullOrEmpty(account.Name)) { operationResultMessage = "Account info on order execution provider not properly assigned."; return(false); } allowedSlippage = ProcessSlippage(allowedSlippage); ExecuteMarketOrderMessage request = new ExecuteMarketOrderMessage(account, symbol, orderType, volume, desiredPrice, allowedSlippage, takeProfit, stopLoss, comment); request.PerformSynchronous = true; ResponseMessage response = this.SendAndReceiveResponding <ResponseMessage> (SourceTransportInfo, request, operationTimeOut); 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); } //if (orderType == OrderTypeEnum.BUY_MARKET // || orderType == OrderTypeEnum.SELL_MARKET) //{// Immediate order. // resultState = OrderStateEnum.Executed; //} //else //{// Delayed pending order. // resultState = OrderStateEnum.Submitted; //} ExecuteMarketOrderResponseMessage responseMessage = (ExecuteMarketOrderResponseMessage)response; operationResultMessage = "Order opened."; info = responseMessage.Info; //RaiseOrderUpdateEvent(account, order.Info, ActiveOrder.UpdateTypeEnum.Submitted); return(true); }
// >> 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 ResponseMessage(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) { ResponseMessage message = null; lock (this) { if (operation.Request is SubmitOrderMessage) { message = new SubmitOrderResponseMessage(_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 ExecuteMarketOrderResponseMessage(_accountInfo, info, operationResult); message.OperationResultMessage = operationResultMessage; } else { SystemMonitor.Error("Failed to establish placed order request type."); message = new ResponseMessage(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); } }