示例#1
0
        /// <summary>
        /// Open orders in position management
        /// </summary>
        public bool CancelPending(string openOrderId, out string operationResultMessage)
        {
            ISourceOrderExecution provider = _orderProvider;

            if (provider == null || provider.DefaultAccount == null)
            {
                operationResultMessage = "Position not initialized.";
                return(false);
            }

            Order order = provider.TradeEntities.GetOrderById(openOrderId);

            if (order.State != OrderStateEnum.Submitted)
            {
                operationResultMessage = "Order state not 'submitted', so not able to cancel as a pending order";
                return(false);
            }

            ActiveOrder activeOrder = order as ActiveOrder;

            if (null != activeOrder)
            {
                return(activeOrder.Cancel(out operationResultMessage));
            }

            PassiveOrder passiveOrder = order as PassiveOrder;

            if (null != passiveOrder)
            {
                return(passiveOrder.CloseOrCancel(null, null, out operationResultMessage));
            }

            operationResultMessage = "Not able to cancel order";
            return(false);
        }
        /// <summary>
        ///
        /// </summary>
        protected override string OnExecuteMarket(ISourceOrderExecution provider, OrderTypeEnum orderType, int volume,
                                                  decimal?price, decimal?slippage, decimal?takeProfit, decimal?stopLoss, TimeSpan timeOut,
                                                  out PositionExecutionInfo executionInfo, out string operationResultMessage)
        {
            SystemMonitor.CheckError(provider.SupportsActiveOrderManagement == false, "Wrong position type for this provider.");

            executionInfo = PositionExecutionInfo.Empty;
            PassiveOrder order;

            lock (this)
            {
                order = new PassiveOrder(_manager, _dataDelivery.SourceId, provider.SourceId);
            }

            OrderInfo?infoReference;

            bool result = provider.SynchronousExecute(provider.DefaultAccount.Info, order, _info.Symbol,
                                                      orderType, volume, slippage, price, takeProfit, stopLoss, string.Empty, out infoReference, out operationResultMessage);

            if (result && infoReference.HasValue)
            {
                OrderInfo infoAssign = infoReference.Value;
                if (infoAssign.Type == OrderTypeEnum.UNKNOWN)
                {
                    infoAssign.Type = orderType;
                }

                if (infoAssign.Volume == int.MinValue ||
                    infoAssign.Volume == int.MaxValue)
                {// Volume was not retrieved by integration.
                    infoAssign.Volume = volume;
                }

                if (infoAssign.OpenPrice.HasValue)
                {
                    executionInfo = new PositionExecutionInfo(infoReference.Value.Id, _dataDelivery.SourceId, provider.SourceId, Symbol,
                                                              infoAssign.Type, infoAssign.OpenPrice.Value, volume, volume,
                                                              infoAssign.OpenTime, PositionExecutionInfo.ExecutionResultEnum.Success);
                }
                else
                {
                    SystemMonitor.Error("Received execution result, but price not assigned.");
                }

                order.AdoptInfo(infoAssign);

                provider.TradeEntities.AddOrder(order);

                return(infoReference.Value.Id);
            }

            return(string.Empty);
        }
        /// <summary>
        /// 
        /// </summary>
        protected override string OnExecuteMarket(ISourceOrderExecution provider, OrderTypeEnum orderType, int volume, 
            decimal? price, decimal? slippage, decimal? takeProfit, decimal? stopLoss, TimeSpan timeOut, 
            out PositionExecutionInfo executionInfo, out string operationResultMessage)
        {
            SystemMonitor.CheckError(provider.SupportsActiveOrderManagement == false, "Wrong position type for this provider.");

            executionInfo = PositionExecutionInfo.Empty;
            PassiveOrder order;
            lock (this)
            {
                order = new PassiveOrder(_manager, _dataDelivery.SourceId, provider.SourceId);
            }

            OrderInfo? infoReference;

            bool result = provider.SynchronousExecute(provider.DefaultAccount.Info, order, _info.Symbol,
                orderType, volume, slippage, price, takeProfit, stopLoss, string.Empty, out infoReference, out operationResultMessage);

            if (result && infoReference.HasValue)
            {
                OrderInfo infoAssign = infoReference.Value;
                if (infoAssign.Type == OrderTypeEnum.UNKNOWN)
                {
                    infoAssign.Type = orderType;
                }

                if (infoAssign.Volume == int.MinValue
                    || infoAssign.Volume == int.MaxValue)
                {// Volume was not retrieved by integration.
                    infoAssign.Volume = volume;
                }

                if (infoAssign.OpenPrice.HasValue)
                {
                    executionInfo = new PositionExecutionInfo(infoReference.Value.Id, _dataDelivery.SourceId, provider.SourceId, Symbol,
                        infoAssign.Type, infoAssign.OpenPrice.Value, volume, volume,
                        infoAssign.OpenTime, PositionExecutionInfo.ExecutionResultEnum.Success);
                }
                else
                {
                    SystemMonitor.Error("Received execution result, but price not assigned.");
                }

                order.AdoptInfo(infoAssign);

                provider.TradeEntities.AddOrder(order);

                return infoReference.Value.Id;
            }

            return string.Empty;
        }
        /// <summary>
        ///
        /// </summary>
        protected override string OnSubmit(ISourceOrderExecution provider, OrderTypeEnum orderType, int volume,
                                           decimal?price, decimal?slippage, decimal?takeProfit, decimal?stopLoss, out string operationResultMessage)
        {
            PassiveOrder order = new PassiveOrder(_manager, _dataDelivery.SourceId, provider.SourceId);

            string id = provider.SubmitOrder(provider.DefaultAccount.Info, order, _info.Symbol,
                                             orderType, volume, slippage, price, takeProfit, stopLoss, string.Empty, out operationResultMessage);

            if (string.IsNullOrEmpty(id))
            {
                return(string.Empty);
            }

            if (order.Info.State != OrderStateEnum.Executed)
            {// It is possible that the submit executes the order instantly, so make sure this is not the case.
                OrderInfo info = new OrderInfo(id, Symbol, orderType, OrderStateEnum.Submitted, volume,
                                               price, null, null, null, null, null, null, null, null, null, null, string.Empty, null);
                order.AdoptInfo(info);
            }

            provider.TradeEntities.AddOrder(order);

            return(id);
        }
        void _executor_OrderUpdatedEvent(IOrderSink providerSink, AccountInfo accountInfo, string[] previousOrdersIds, 
            OrderInfo[] ordersInfos, Order.UpdateTypeEnum[] updatesType)
        {
            ISourceOrderExecution provider = _provider;

            if (providerSink != _provider)
            {
                SystemMonitor.Warning("Provider mismatch.");
                return;
            }

            List<Order> updatedOrders = new List<Order>();
            List<Order.UpdateTypeEnum> updatedOrdersUpdateTypes = new List<Order.UpdateTypeEnum>();

            for (int i = 0; i < ordersInfos.Length; i++)
            {
                if (string.IsNullOrEmpty(ordersInfos[i].Id))
                {
                    SystemMonitor.Warning("Order update of order with no ID.");
                    continue;
                }

                if (previousOrdersIds.Length > i && previousOrdersIds[i] != ordersInfos[i].Id
                    && string.IsNullOrEmpty(previousOrdersIds[i]) == false)
                {// Order Id has changed, remove old order.
                    Order superceededOrder = GetOrderById(previousOrdersIds[i]);
                    RemoveOrder(superceededOrder);
                }

                Order order = GetOrderById(ordersInfos[i].Id);
                if (order == null)
                {// Create new order based on incoming information.

                    if (provider.SupportsActiveOrderManagement)
                    {
                        order = new ActiveOrder(_manager, provider, _delivery.SourceId, true);
                    }
                    else
                    {
                        order = new PassiveOrder(_manager, _delivery.SourceId, provider.SourceId);
                    }

                    order.AdoptInfo(ordersInfos[i]);

                    if (AddOrder(order) == false)
                    {
                        SystemMonitor.OperationError("Failed to add order to keeper (id [" + order.Id + "] already used for another order).", TracerItem.PriorityEnum.Medium);
                    }
                }
                else
                {// Existing order, to be updated.

                    OrderInfo info = ordersInfos[i];

                    // First, check for critical modifications (price changes).
                    if (order.Info.IsCriticalUpdate(info))
                    {
                        SystemMonitor.Report(string.Format("Order has received a critical data modication Id[{0}], Open[{1} / {2}], Close[{3} / {4}].", order.Id, order.OpenPrice.ToString(), info.OpenPrice.ToString(), order.ClosePrice.ToString(),
                            info.ClosePrice.ToString()), TracerItem.PriorityEnum.High);

                        if (OrdersCriticalInformationChangedEvent != null)
                        {
                            OrdersCriticalInformationChangedEvent(this, accountInfo, order, info);
                        }
                    }

                    if (order.UpdateInfo(info) == false)
                    {
                        SystemMonitor.OperationError("Failed to update order [" + order.Id + "].");
                        continue;
                    }

                    lock (this)
                    {
                        // Remove from any of the sub arrays it may be in.
                        foreach (OrderStateEnum state in Enum.GetValues(typeof(OrderStateEnum)))
                        {
                            if (_ordersByState.ContainsKey(state) && _ordersByState[state].Contains(order))
                            {
                                _ordersByState[state].Remove(order);
                            }
                        }

                        _ordersByState[info.State].Add(order);
                    }

                    updatedOrders.Add(order);
                    updatedOrdersUpdateTypes.Add(updatesType[i]);
                }
            }

            if (updatedOrders.Count > 0 && OrdersUpdatedEvent != null)
            {
                OrdersUpdatedEvent(this, accountInfo, updatedOrders.ToArray(), updatedOrdersUpdateTypes.ToArray() );
            }
        }
        void _executor_OrderUpdatedEvent(IOrderSink providerSink, AccountInfo accountInfo, string[] previousOrdersIds,
                                         OrderInfo[] ordersInfos, Order.UpdateTypeEnum[] updatesType)
        {
            ISourceOrderExecution provider = _provider;

            if (providerSink != _provider)
            {
                SystemMonitor.Warning("Provider mismatch.");
                return;
            }

            List <Order> updatedOrders = new List <Order>();
            List <Order.UpdateTypeEnum> updatedOrdersUpdateTypes = new List <Order.UpdateTypeEnum>();

            for (int i = 0; i < ordersInfos.Length; i++)
            {
                if (string.IsNullOrEmpty(ordersInfos[i].Id))
                {
                    SystemMonitor.Warning("Order update of order with no ID.");
                    continue;
                }

                if (previousOrdersIds.Length > i && previousOrdersIds[i] != ordersInfos[i].Id &&
                    string.IsNullOrEmpty(previousOrdersIds[i]) == false)
                {// Order Id has changed, remove old order.
                    Order superceededOrder = GetOrderById(previousOrdersIds[i]);
                    RemoveOrder(superceededOrder);
                }

                Order order = GetOrderById(ordersInfos[i].Id);
                if (order == null)
                {// Create new order based on incoming information.
                    if (provider.SupportsActiveOrderManagement)
                    {
                        order = new ActiveOrder(_manager, provider, _delivery.SourceId, true);
                    }
                    else
                    {
                        order = new PassiveOrder(_manager, _delivery.SourceId, provider.SourceId);
                    }

                    order.AdoptInfo(ordersInfos[i]);

                    if (AddOrder(order) == false)
                    {
                        SystemMonitor.OperationError("Failed to add order to keeper (id [" + order.Id + "] already used for another order).", TracerItem.PriorityEnum.Medium);
                    }
                }
                else
                {// Existing order, to be updated.
                    OrderInfo info = ordersInfos[i];

                    // First, check for critical modifications (price changes).
                    if (order.Info.IsCriticalUpdate(info))
                    {
                        SystemMonitor.Report(string.Format("Order has received a critical data modication Id[{0}], Open[{1} / {2}], Close[{3} / {4}].", order.Id, order.OpenPrice.ToString(), info.OpenPrice.ToString(), order.ClosePrice.ToString(),
                                                           info.ClosePrice.ToString()), TracerItem.PriorityEnum.High);

                        if (OrdersCriticalInformationChangedEvent != null)
                        {
                            OrdersCriticalInformationChangedEvent(this, accountInfo, order, info);
                        }
                    }

                    if (order.UpdateInfo(info) == false)
                    {
                        SystemMonitor.OperationError("Failed to update order [" + order.Id + "].");
                        continue;
                    }

                    lock (this)
                    {
                        // Remove from any of the sub arrays it may be in.
                        foreach (OrderStateEnum state in Enum.GetValues(typeof(OrderStateEnum)))
                        {
                            if (_ordersByState.ContainsKey(state) && _ordersByState[state].Contains(order))
                            {
                                _ordersByState[state].Remove(order);
                            }
                        }

                        _ordersByState[info.State].Add(order);
                    }

                    updatedOrders.Add(order);
                    updatedOrdersUpdateTypes.Add(updatesType[i]);
                }
            }

            if (updatedOrders.Count > 0 && OrdersUpdatedEvent != null)
            {
                OrdersUpdatedEvent(this, accountInfo, updatedOrders.ToArray(), updatedOrdersUpdateTypes.ToArray());
            }
        }
        /// <summary>
        /// 
        /// </summary>
        protected override string OnSubmit(ISourceOrderExecution provider, OrderTypeEnum orderType, int volume, 
            decimal? price, decimal? slippage, decimal? takeProfit, decimal? stopLoss, out string operationResultMessage)
        {
            PassiveOrder order = new PassiveOrder(_manager, _dataDelivery.SourceId, provider.SourceId);

            string id = provider.SubmitOrder(provider.DefaultAccount.Info, order, _info.Symbol,
                orderType, volume, slippage, price, takeProfit, stopLoss, string.Empty, out operationResultMessage);

            if (string.IsNullOrEmpty(id))
            {
                return string.Empty;
            }

            if (order.Info.State != OrderStateEnum.Executed)
            {// It is possible that the submit executes the order instantly, so make sure this is not the case.
                OrderInfo info = new OrderInfo(id, Symbol, orderType, OrderStateEnum.Submitted, volume,
                    price, null, null, null, null, null, null, null, null, null, null, string.Empty, null);
                order.AdoptInfo(info);
            }

            provider.TradeEntities.AddOrder(order);

            return id;
        }