Пример #1
0
        public DispatchedOrder[] DispatchOrder(OrderRequest[] requests, out string[] errors)
        {
            var results = _client.SendOrder(requests, out errors);

            lock (_orderLockObj)
            {
                DispatchedOrder[] orders = new DispatchedOrder[results.Length];

                for (int i = 0; i < results.Length; ++i)
                {
                    if (results[i] != null)
                    {
                        DispatchedOrder dispatchedOrder = new DispatchedOrder()
                        {
                            OrderNo         = results[i].OrderNo,
                            Request         = requests[i],
                            SucceededVolume = 0,
                            LastStatus      = OrderStatus.NotSubmitted,
                        };

                        _allActiveOrders.Add(results[i].OrderNo, dispatchedOrder);

                        orders[i] = dispatchedOrder;
                    }
                    else
                    {
                        orders[i] = null;
                    }
                }

                return(orders.ToArray());
            }
        }
Пример #2
0
 private void NotifyOrderStatusChanged(DispatchedOrder dispatchedOrder)
 {
     if (_onOrderStatusChangedCallback != null)
     {
         _onOrderStatusChangedCallback(dispatchedOrder);
     }
 }
Пример #3
0
        private bool CheckOrderStatusChangeAndNotify(ref DispatchedOrder dispatchedOrder, QueryGeneralOrderResult order)
        {
            bool isStatusChanged = false;

            if (order.Status != dispatchedOrder.LastStatus)
            {
                isStatusChanged = true;
            }
            else if (order.Status == OrderStatus.PartiallySucceeded &&
                     dispatchedOrder.LastStatus == order.Status &&
                     order.DealVolume != dispatchedOrder.SucceededVolume)
            {
                isStatusChanged = true;
            }

            dispatchedOrder.LastStatus      = order.Status;
            dispatchedOrder.SucceededVolume = order.DealVolume;

            if (isStatusChanged)
            {
                NotifyOrderStatusChanged(dispatchedOrder);
            }

            return(isStatusChanged);
        }
        private void OnOrderStatusChanged(DispatchedOrder dispatchedOrder)
        {
            if (dispatchedOrder == null)
            {
                return;
            }

            if (dispatchedOrder.Request.AssociatedObject != null)
            {
                StoplossOrder order = dispatchedOrder.Request.AssociatedObject as StoplossOrder;

                if (order == null)
                {
                    return;
                }

                if (TradingHelper.IsFinalStatus(dispatchedOrder.LastStatus))
                {
                    lock (_orderLockObj)
                    {
                        if (!IsSentOrder(order))
                        {
                            return;
                        }

                        ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                        if (logger != null)
                        {
                            logger.InfoFormat(
                                "Stoploss order executed:  id {0} code {1} status {2} suceeded volume {3}.",
                                order.OrderId,
                                order.SecurityCode,
                                dispatchedOrder.LastStatus,
                                dispatchedOrder.SucceededVolume);
                        }

                        order.UpdateExistingVolume(dispatchedOrder.SucceededVolume);

                        RemoveSentOrder(order);

                        if (order.ExistingVolume > 0)
                        {
                            // the order has not been finished yet, put it back into active order
                            AddActiveStoplossOrder(order);

                            if (TradingHelper.IsSucceededFinalStatus(dispatchedOrder.LastStatus))
                            {
                                // send out order again
                                SendStoplossOrder(order);
                            }
                        }
                    }
                }
            }
        }
        private void SendStoplossOrderWorkItem(object state)
        {
            StoplossOrder order = (StoplossOrder)state;

            OrderRequest request = new OrderRequest(order)
            {
                Category     = OrderCategory.Sell,
                Price        = order.StoplossPrice,
                PricingType  = OrderPricingType.MakertPriceMakeDealInFiveGradesThenCancel,
                Volume       = order.ExistingVolume,
                SecurityCode = order.SecurityCode,
            };

            string          error;
            DispatchedOrder dispatchedOrder = CtpSimulator.GetInstance().DispatchOrder(request, out error);

            if (dispatchedOrder == null)
            {
                ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                if (logger != null)
                {
                    logger.ErrorFormat(
                        "Exception in dispatching stop loss order: id {0} code {1} stoploss price {2}, volume {3}. Error: {4}",
                        order.OrderId,
                        order.SecurityCode,
                        order.StoplossPrice,
                        order.ExistingVolume,
                        error);
                }
            }
            else
            {
                ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                if (logger != null)
                {
                    logger.InfoFormat(
                        "Dispatched stop loss order: id {0} code {1} stoploss price {2}, volume {3}.",
                        order.OrderId,
                        order.SecurityCode,
                        order.StoplossPrice,
                        order.ExistingVolume);
                }

                lock (_orderLockObj)
                {
                    RemoveActiveStoplossOrder(order);
                    AddSentOrder(order);
                }

                // force query order status.
                CtpSimulator.GetInstance().QueryOrderStatusForcibly();
            }
        }
Пример #6
0
        public DispatchedOrder DispatchOrder(OrderRequest request, out string error)
        {
            var result = _client.SendOrder(request, out error);

            if (result == null)
            {
                return(null);
            }

            DispatchedOrder dispatchedOrder = new DispatchedOrder()
            {
                OrderNo         = result.OrderNo,
                Request         = request,
                SucceededVolume = 0,
                LastStatus      = OrderStatus.NotSubmitted,
            };

            lock (_orderLockObj)
            {
                _allActiveOrders.Add(result.OrderNo, dispatchedOrder);
            }

            return(dispatchedOrder);
        }
Пример #7
0
 public bool CancelOrder(DispatchedOrder order, out string error)
 {
     return(_client.CancelOrder(order.Request.SecurityCode, order.OrderNo, out error));
 }
Пример #8
0
 public bool CancelOrder(DispatchedOrder order, out string error)
 {
     return(_orderDispatcher.CancelOrder(order, out error));
 }