Example #1
0
        /// <summary>
        /// This allows more specific control over the operation.
        /// </summary>
        public bool Submit(OrderTypeEnum orderType, int volume, decimal?allowedSlippage,
                           decimal?desiredPrice, decimal?takeProfit, decimal?stopLoss, string comment, out string operationResultMessage)
        {
            SystemMonitor.CheckThrow(volume > 0, "Misuse of the Order class.");

            if (State != OrderStateEnum.Initialized)
            {
                operationResultMessage = "Misuse of the Order class [Order not initialized; or Must not place trade, that has already been placed].";
                SystemMonitor.Warning(operationResultMessage);
                return(false);
            }

            ISourceOrderExecution executionProvider = _executionProvider;

            operationResultMessage = "Session not assigned.";

            if (desiredPrice.HasValue == false)
            {
                desiredPrice = OrderInfo.TypeIsBuy(orderType) ? QuoteProvider.Bid : QuoteProvider.Ask;
            }

            if (executionProvider == null)
            {// Placement of order failed.
                State = OrderStateEnum.Failed;
                SystemMonitor.Report("Order was not executed [" + operationResultMessage + "].");
                return(false);
            }

            string id = OrderExecutionProvider.SubmitOrder(Account.Info, this, Symbol, orderType, volume,
                                                           allowedSlippage, desiredPrice, takeProfit, stopLoss, comment, out operationResultMessage);

            if (string.IsNullOrEmpty(id))
            {
                State = OrderStateEnum.Failed;
                SystemMonitor.OperationError("Order was not executed [" + operationResultMessage + "].");
                return(false);
            }

            lock (this)
            {
                _info.Type     = orderType;
                _initialVolume = volume;
                _info.Id       = id;
                _info.Volume   = volume;

                _info.StopLoss   = stopLoss;
                _info.TakeProfit = takeProfit;

                State          = OrderStateEnum.Submitted;
                _localOpenTime = DateTime.Now;
            }

            Account.TradeEntities.AddOrder(this);

            if (State == OrderStateEnum.Executed)
            {
                RaiseOrderUpdatedEvent(UpdateTypeEnum.Executed);
            }
            else
            {
                RaiseOrderUpdatedEvent(UpdateTypeEnum.Submitted);
            }

            return(true);
        }