Ejemplo n.º 1
0
 public void DoOrderSend(ref OrderField[] ordersArray, Order order)
 {
     if (Convert.ToInt32(order.Qty) == int.MaxValue) {
         SetExternalOrder(order, ref ordersArray[0]);
     }
     else {
         DoOrderSend(ref ordersArray, new List<Order>() { order });
     }
 }
Ejemplo n.º 2
0
 private void SetExternalOrder(Order order, ref OrderField field)
 {
     var orderRecord = new OrderRecord(order);
     ExternalOrderRecord record = new ExternalOrderRecord();
     record = externalOrders.GetOrAdd(order.Instrument.Symbol, record);
     if (field.OpenClose == OpenCloseType.Open) {
         if (field.Side == XAPI.OrderSide.Buy)
         {
             record.BuyOpen = orderRecord;
         }
         else {
             record.SellOpen = orderRecord;
         }
     }
     else {
         if (field.Side == XAPI.OrderSide.Buy)
         {
             record.BuyClose = orderRecord;
         }
         else {
             record.SellClose = orderRecord;
         }
     }
 }
Ejemplo n.º 3
0
 static void OnRtnOrder(object sender, ref OrderField order)
 {
     Console.WriteLine("AA " + order.Status + order.ExecType + order.Text() + "AA " + order.ID + "AA " + order.OrderID);
 }
Ejemplo n.º 4
0
 static void OnRtnOrder(object sender, ref OrderField order)
 {
     Console.WriteLine("AA " + order.Status + order.ExecType + order.Text() + "AA " + order.ID + "AA " + order.OrderID);
 }
Ejemplo n.º 5
0
 public static string Text([In] this OrderField field)
 {
     return(PInvokeUtility.GetUnicodeString(field.Text));
 }
Ejemplo n.º 6
0
 public static string InstrumentName([In] this OrderField field)
 {
     return(PInvokeUtility.GetUnicodeString(field.InstrumentName));
 }
Ejemplo n.º 7
0
        public void DoOrderSend(ref OrderField[] ordersArray, List<Order> ordersList)
        {
            // 这里其实返回的是LocalID
            string outstr = provider._TdApi.SendOrder(ref ordersArray);
            string[] OrderIds = outstr.Split(';');

            int i = 0;
            foreach (var orderId in OrderIds)
            {
                if (string.IsNullOrEmpty(orderId))
                {
                    // 直接将单子拒绝
                    EmitExecutionReport(new OrderRecord(ordersList[i]), SQ.ExecType.ExecRejected, SQ.OrderStatus.Rejected, "ErrorCode:" + orderId);
                }
                else
                {
                    this.pendingOrders.TryAdd(orderId, new OrderRecord(ordersList[i]));
                    // 记下了本地ID,用于立即撤单时供API来定位
                    this.orderIDs.Add(ordersList[i].Id, orderId);
                }
                ++i;
            }
        }
Ejemplo n.º 8
0
        public void Process(ref OrderField order)
        {
            // 所有的成交信息都不处理,交给TradeField处理
            if (order.ExecType == XAPI.ExecType.Trade)
                return;

            OrderRecord record;

            switch (order.ExecType) {
                case XAPI.ExecType.New:
                    if (this.pendingOrders.TryRemove(order.LocalID, out record)) {
                        this.workingOrders.Add(order.ID, record);
                        // 将LocalID更新为ID
                        this.orderIDs[record.Order.Id] = order.ID;
                        EmitExecutionReport(record, (SQ.ExecType)order.ExecType, (SQ.OrderStatus)order.Status);
                    }
                    break;
                case XAPI.ExecType.Rejected:
                    if (this.pendingOrders.TryRemove(order.LocalID, out record))
                    {
                        orderIDs.Remove(record.Order.Id);
                        EmitExecutionReport(record, (SQ.ExecType)order.ExecType, (SQ.OrderStatus)order.Status, order.Text());
                    }
                    else if (this.workingOrders.TryGetValue(order.ID, out record)) {
                        // 比如说出现超出涨跌停时,先会到ProcessNew,所以得再多判断一次
                        workingOrders.Remove(order.ID);
                        orderIDs.Remove(record.Order.Id);
                        EmitExecutionReport(record, (SQ.ExecType)order.ExecType, (SQ.OrderStatus)order.Status, order.Text());
                    }
                    break;
                case XAPI.ExecType.Cancelled:
                    if (this.workingOrders.TryGetValue(order.ID, out record)) {
                        workingOrders.Remove(order.ID);
                        orderIDs.Remove(record.Order.Id);
                        EmitExecutionReport(record, SQ.ExecType.ExecCancelled, SQ.OrderStatus.Cancelled);
                    }
                    else if (this.pendingOrders.TryRemove(order.LocalID, out record))
                    {
                        orderIDs.Remove(record.Order.Id);
                        EmitExecutionReport(record, (SQ.ExecType)order.ExecType, (SQ.OrderStatus)order.Status, order.Text());
                    }
                    break;
                case XAPI.ExecType.PendingCancel:
                    if (this.workingOrders.TryGetValue(order.ID, out record)) {
                        EmitExecutionReport(record, SQ.ExecType.ExecPendingCancel, SQ.OrderStatus.PendingCancel);
                    }
                    break;
                case XAPI.ExecType.CancelReject:
                    if (this.pendingCancels.TryRemove(order.ID, out record)) {
                        EmitExecutionReport(record, SQ.ExecType.ExecCancelReject, (SQ.OrderStatus)order.Status, order.Text());
                    }
                    break;
            }
        }
 private void OnRtnOrder_callback(object sender, ref OrderField order)
 {
     (sender as XApi).Log.Debug("OnRtnOrder:" + order.ToFormattedString());
     try
     {
         orderMap.Process(ref order);
     }
     catch (Exception ex)
     {
         (sender as XApi).Log.Error(ex);
     }
 }
        private void ToOrderStruct(ref OrderField field, Order order, string apiSymbol, string apiExchange)
        {
            field.InstrumentID = apiSymbol;
            field.ExchangeID = apiExchange;
            field.Price = order.Price;
            field.Qty = order.Qty;
            field.OpenClose = GetOpenClose(order);
            field.HedgeFlag = GetHedgeFlag(order);
            field.Side = GetSide(order);
            field.Type = (XAPI.OrderType)order.Type;
            field.StopPx = order.StopPx;
            field.TimeInForce = (XAPI.TimeInForce)order.TimeInForce;
            field.ClientID = order.ClientID;
            field.AccountID = order.Account;

            field.PortfolioID1 = GetPortfolioID1(order);
            field.PortfolioID2 = GetPortfolioID2(order);
            field.PortfolioID3 = GetPortfolioID3(order);
            field.Business = GetBusiness(order);
        }
        private void CmdNewOrderList(ExecutionCommand command)
        {
            // 先查出所有的单子
            List<Order> orders = command.Order.GetSameTimeOrderList();

            OrderField[] fields = new OrderField[orders.Count];
            for (int i = 0; i < orders.Count; ++i)
            {
                string altSymbol;
                string altExchange;
                string apiSymbol;
                string apiExchange;
                double apiTickSize;

                GetApi_Symbol_Exchange_TickSize(orders[i].Instrument,
                    out altSymbol, out altExchange,
                    out apiSymbol, out apiExchange,
                    out apiTickSize);

                ToOrderStruct(ref fields[i], orders[i], apiSymbol, apiExchange);
            }

            orderMap.DoOrderSend(ref fields, orders);
        }
        private void CmdNewOrderSingle(ExecutionCommand command)
        {
            string altSymbol;
            string altExchange;
            string apiSymbol;
            string apiExchange;
            double apiTickSize;

            GetApi_Symbol_Exchange_TickSize(command.Instrument,
                out altSymbol, out altExchange,
                out apiSymbol, out apiExchange,
                out apiTickSize);

            OrderField[] fields = new OrderField[1];

            ToOrderStruct(ref fields[0], command.Order, apiSymbol, apiExchange);
            double price = fields[0].Price;

            //市价修正,如果不连接行情,此修正不执行,得策略层处理
            MarketDataRecord record;
            if (marketDataRecords.TryGetValue(command.Instrument.Symbol, out record))
            {
                switch(command.OrdType)
                {
                    case SQ.OrderType.Market:
                    case SQ.OrderType.MarketOnClose:
                    case SQ.OrderType.Stop:
                    case SQ.OrderType.TrailingStop:
                        {
                            if (command.Side == SQ.OrderSide.Buy)
                            {
                                price = record.DepthMarket.LastPrice + LastPricePlusNTicks * apiTickSize;
                            }
                            else
                            {
                                price = record.DepthMarket.LastPrice - LastPricePlusNTicks * apiTickSize;
                            }

                            // 市价单使用限价单模拟
                            if(SwitchMakertOrderToLimitOrder)
                            {
                                fields[0].Type = XAPI.OrderType.Limit;
                            }
                        }
                        break;
                }
                if (HasPriceLimit)
                {
                    //price = FixPrice(price, command.Side, apiTickSize, record.DepthMarket.LowerLimitPrice, record.DepthMarket.UpperLimitPrice);
                    price = FixPrice(record,price, command.Side, apiTickSize);
                }
                
                fields[0].Price = price;
            }

            orderMap.DoOrderSend(ref fields, command.Order);
        }