public void SendOrderCancelRequest(FIXOrderCancelRequest request)
        {
            if (!this.isConnected)
            {
                this.EmitError(this.Id, -1, "The TDXTradeProvider is not connected.");
            }
            string      origOuterOrderID = request.OrigClOrdID;
            OrderRecord orderRecord      = this.orders.GetRecord(origOuterOrderID, 1);

            if (orderRecord == null)
            {
                string errorInfo = "要取消的订单没有记录。";
                this.EmitError(this.Id, -1, errorInfo);
                OrderCancelReject report = new OrderCancelReject();
                report.TransactTime     = DateTime.Now;
                report.OrigClOrdID      = request.OrigClOrdID;
                report.ClOrdID          = request.ClOrdID;
                report.CxlRejReason     = CxlRejReason.UnknownOrder;
                report.CxlRejResponseTo = CxlRejResponseTo.CancelRequest;
                report.Text             = errorInfo;
                report.OrdStatus        = OrdStatus.Rejected;
                if (OrderCancelReject != null)
                {
                    OrderCancelReject(this, new OrderCancelRejectEventArgs(report));
                }
                return;
            }
            string     origInnerOrderID = orderRecord.InnerOrderID;
            ReportArgs rargs            = this.trader.CancelOrder(origInnerOrderID);

            if (rargs.Succeeded)
            {
                Thread.Sleep(1000);
                this.updateOrders();
            }
            else
            {
                this.EmitError(this.Id, -1, rargs.ErrorInfo);
                OrderCancelReject report = new OrderCancelReject();
                report.TransactTime     = DateTime.Now;
                report.OrigClOrdID      = request.OrigClOrdID;
                report.ClOrdID          = request.ClOrdID;
                report.CxlRejReason     = CxlRejReason.BrokerOption;
                report.CxlRejResponseTo = CxlRejResponseTo.CancelRequest;
                report.Text             = rargs.ErrorInfo;
                report.OrdStatus        = OrdStatus.Rejected;
                if (OrderCancelReject != null)
                {
                    OrderCancelReject(this, new OrderCancelRejectEventArgs(report));
                }
            }
        }
        private void updateOrders()
        {
            if (!this.isConnected)
            {
                this.EmitError(this.Id, -1, "The TDXTradeProvider is not connected.");
            }
            lock (this.updating)
            {
                ReportArgs rargs = this.trader.QueryOrders();
                if (rargs.Succeeded)
                {
                    List <OrderRecord> orderRecords = (List <OrderRecord>)rargs.Result;
                    foreach (OrderRecord orderRecord in orderRecords)
                    {
                        bool        needReport      = false;
                        OrderRecord prevOrderRecord = this.orders.GetRecord(orderRecord.InnerOrderID, 0);
                        if (prevOrderRecord == null)
                        {
                            prevOrderRecord = this.orders.SearchRecord(orderRecord);
                        }
                        if (prevOrderRecord != null)
                        {
                            if (prevOrderRecord.OrderStatus != orderRecord.OrderStatus)
                            {
                                needReport = true;
                            }
                            else if (orderRecord.OrderStatus == OrderStatus.PartiallyFilled &&
                                     prevOrderRecord.CumQty < orderRecord.CumQty)
                            {
                                needReport = true;
                            }
                        }
                        if (needReport)
                        {
                            ExecutionReport report = new ExecutionReport();
                            report.TransactTime = orderRecord.TransactTime;
                            switch (orderRecord.OrderStatus)
                            {
                            case OrderStatus.New:
                                report.ExecType  = ExecType.New;
                                report.OrdStatus = OrdStatus.New;
                                break;

                            case OrderStatus.PartiallyFilled:
                                report.ExecType  = ExecType.PartialFill;
                                report.OrdStatus = OrdStatus.PartiallyFilled;
                                break;

                            case OrderStatus.Filled:
                                report.ExecType  = ExecType.Fill;
                                report.OrdStatus = OrdStatus.Filled;
                                break;

                            case OrderStatus.PendingCancel:
                                report.ExecType  = ExecType.PendingCancel;
                                report.OrdStatus = OrdStatus.PendingCancel;
                                break;

                            case OrderStatus.Cancelled:
                                report.ExecType  = ExecType.Cancelled;
                                report.OrdStatus = OrdStatus.Cancelled;
                                break;

                            case OrderStatus.Rejected:
                                report.ExecType  = ExecType.Rejected;
                                report.OrdStatus = OrdStatus.Rejected;
                                break;

                            default:
                                report.ExecType  = ExecType.Undefined;
                                report.OrdStatus = OrdStatus.Undefined;
                                break;
                            }
                            report.ClOrdID = prevOrderRecord.OuterOrderID;
                            if (orderRecord.OrderStatus == OrderStatus.PendingCancel || orderRecord.OrderStatus == OrderStatus.Cancelled)
                            {
                                report.OrigClOrdID = prevOrderRecord.OuterOrderID;
                            }

                            report.OrderQty  = orderRecord.OrderQty;
                            report.AvgPx     = orderRecord.AvgPx;
                            report.CumQty    = orderRecord.CumQty;
                            report.LeavesQty = orderRecord.OrderQty - orderRecord.CumQty;
                            switch (orderRecord.OrderSide)
                            {
                            case (OrderSide.Buy):
                                report.Side = Side.Buy;
                                break;

                            case (OrderSide.Sell):
                                report.Side = Side.Sell;
                                break;

                            default:
                                report.Side = Side.Undefined;
                                break;
                            }
                            //计算费率
                            if (report.CumQty > 0)
                            {
                                report.Commission = this.commission * report.CumQty * report.AvgPx;
                                report.CommType   = CommType.Absolute;
                                if (report.Commission < this.minCommission)
                                {
                                    report.Commission = this.minCommission;
                                }
                                if (report.Side == Side.Sell)
                                {
                                    report.Commission += this.stampDuty * report.CumQty * report.AvgPx;
                                }
                            }
                            if (ExecutionReport != null)
                            {
                                ExecutionReport(this, new ExecutionReportEventArgs(report));
                            }
                            prevOrderRecord.OrderStatus = orderRecord.OrderStatus;
                            prevOrderRecord.AvgPx       = orderRecord.AvgPx;
                            prevOrderRecord.CumQty      = orderRecord.CumQty;
                        }
                    }
                }
                else
                {
                    this.EmitError(this.Id, -1, rargs.ErrorInfo);
                }
            }
        }
        public void SendOrderStatusRequest(FIXOrderStatusRequest request)
        {
            if (!this.isConnected)
            {
                this.EmitError(this.Id, -1, "The TDXTradeProvider is not connected.");
            }
            string      outerOrderID = request.OrderID;
            OrderRecord orderRecord  = this.orders.GetRecord(outerOrderID, 1);

            if (orderRecord == null)
            {
                orderRecord = new OrderRecord();
                SingleOrder order = (SingleOrder)OrderManager.Orders.All[request.OrderID];
                if (order != null)
                {
                    orderRecord.TransactTime = order.TransactTime;
                    orderRecord.OrderQty     = (long)order.OrderQty;
                    orderRecord.OrderPrice   = order.Price;
                    orderRecord.SecurityID   = order.SecurityID;
                    switch (order.OrdStatus)
                    {
                    case OrdStatus.New:
                        orderRecord.OrderStatus = OrderStatus.New;
                        break;

                    case OrdStatus.PartiallyFilled:
                        orderRecord.OrderStatus = OrderStatus.PartiallyFilled;
                        break;

                    case OrdStatus.Filled:
                        orderRecord.OrderStatus = OrderStatus.Filled;
                        break;

                    case OrdStatus.PendingCancel:
                        orderRecord.OrderStatus = OrderStatus.PendingCancel;
                        break;

                    case OrdStatus.Cancelled:
                        orderRecord.OrderStatus = OrderStatus.Cancelled;
                        break;

                    case OrdStatus.Rejected:
                        orderRecord.OrderStatus = OrderStatus.Rejected;
                        break;

                    default:
                        orderRecord.OrderStatus = OrderStatus.New;
                        break;
                    }
                    orderRecord.OuterOrderID = order.ClOrdID;
                    orderRecord.AvgPx        = order.AvgPx;
                    orderRecord.CumQty       = (long)order.CumQty;
                    switch (order.Side)
                    {
                    case (Side.Buy):
                        orderRecord.OrderSide = OrderSide.Buy;
                        break;

                    case (Side.Sell):
                        orderRecord.OrderSide = OrderSide.Sell;
                        break;

                    default:
                        throw new Exception("不支持的买卖指令。");
                    }
                    lock (this.orders)
                    {
                        this.orders.AddRecord(orderRecord, null, outerOrderID);
                    }
                }
            }
            this.updateOrders();
        }
        public void SendNewOrderSingle(NewOrderSingle order)
        {
            if (!this.isConnected)
            {
                this.EmitError(this.Id, -1, "The TDXTradeProvider is not connected.");
            }
            try
            {
                Instrument curInstrument = InstrumentManager.Instruments[order.Symbol];
                string     securityID    = curInstrument.SecurityID;
                OrderSide  orderSide;
                switch (order.Side)
                {
                case Side.Buy:
                    orderSide = OrderSide.Buy;
                    break;

                case Side.Sell:
                    orderSide = OrderSide.Sell;
                    break;

                default:
                    throw new Exception("不支持的买卖指令。");
                }
                OrderType orderType;
                switch (order.OrdType)
                {
                case OrdType.Market:
                    orderType = OrderType.CancelAfterFiveQuoteTransaction;
                    break;

                case OrdType.Limit:
                    orderType = OrderType.Limit;
                    break;

                default:
                    throw new Exception("不支持的订单类型");
                }
                ReportArgs rargs = this.trader.SendOrder(orderSide, orderType, securityID, (float)order.Price, (long)order.OrderQty);
                if (rargs.Succeeded)                                //下单成功
                {
                    string      orderID     = (string)rargs.Result; //委托编号
                    OrderRecord orderRecord = new OrderRecord();
                    orderRecord.InnerOrderID = orderID;
                    orderRecord.OuterOrderID = order.ClOrdID;
                    orderRecord.SecurityID   = order.SecurityID;
                    orderRecord.OrderStatus  = OrderStatus.New;
                    orderRecord.OrderSide    = orderSide;
                    orderRecord.OrderType    = orderType;
                    orderRecord.OrderPrice   = order.Price;
                    orderRecord.OrderQty     = (long)order.OrderQty;
                    orderRecord.AvgPx        = 0.0;
                    orderRecord.CumQty       = 0;
                    orderRecord.TransactTime = Clock.Now;
                    lock (this.orders)
                    {
                        this.orders.AddRecord(orderRecord, orderID, order.ClOrdID);
                    }
                    ExecutionReport report = new ExecutionReport();
                    report.TransactTime = orderRecord.TransactTime;
                    report.ExecType     = ExecType.New;
                    report.ClOrdID      = order.ClOrdID;
                    report.OrdStatus    = OrdStatus.New;
                    report.AvgPx        = 0.0;
                    report.CumQty       = 0;
                    report.OrderQty     = order.OrderQty;
                    report.Price        = order.Price;
                    report.LeavesQty    = order.OrderQty;
                    report.Side         = order.Side;
                    if (order.OrdType == OrdType.Limit)
                    {
                        report.TimeInForce = TimeInForce.Day;//强制限价单为当日有效
                    }
                    if (ExecutionReport != null)
                    {
                        ExecutionReport(this, new ExecutionReportEventArgs(report));
                    }
                    Thread.Sleep(1000);
                    this.updateOrders();
                }
                else//下单失败
                {
                    this.EmitError(this.Id, -1, rargs.ErrorInfo);
                    ExecutionReport report = new ExecutionReport();
                    report.TransactTime = DateTime.Now;
                    report.ExecType     = ExecType.Rejected;
                    report.ClOrdID      = order.ClOrdID;
                    report.OrdStatus    = OrdStatus.Rejected;
                    report.AvgPx        = 0.0;
                    report.CumQty       = 0;
                    report.OrderQty     = order.OrderQty;
                    report.Price        = order.Price;
                    report.LeavesQty    = order.OrderQty;
                    report.Side         = order.Side;
                    report.Text         = rargs.ErrorInfo;
                    if (ExecutionReport != null)
                    {
                        ExecutionReport(this, new ExecutionReportEventArgs(report));
                    }
                }
            }catch (Exception ex)
            {
                this.EmitError(this.Id, -1, ex.Message);
            }
        }
Ejemplo n.º 5
0
        public ReportArgs QueryOrders()
        {
            ReportArgs reportArgs = new ReportArgs();

            if (this.clientID != -1)
            {
                StringBuilder errInfo = new StringBuilder(256);
                StringBuilder result  = new StringBuilder(1024 * 1024);
                TDXWrapper.QueryData(this.clientID, 2, result, errInfo);
                if (errInfo.ToString() != string.Empty)//查询失败
                {
                    reportArgs.Succeeded = false;
                    reportArgs.ErrorInfo = errInfo.ToString();
                }
                else//查询成功
                {
                    List <string[]> data = this.pickUp(result);
                    reportArgs.Succeeded = true;
                    List <OrderRecord> resultList = new List <OrderRecord>();
                    string             dateString = DateTime.Today.ToString("yyyy-MM-dd");
                    for (int i = 1; i < data.Count; i++)
                    {
                        OrderRecord orderRecord = new OrderRecord();
                        orderRecord.TransactTime = DateTime.Parse(dateString + " " + data[i][0]);
                        orderRecord.SecurityID   = data[i][1];
                        orderRecord.OrderSide    = (OrderSide)int.Parse(data[i][3]);
                        switch (data[i][6])
                        {
                        case "待报":
                            orderRecord.OrderStatus = OrderStatus.PendingNew;
                            break;

                        case "已报":
                            orderRecord.OrderStatus = OrderStatus.New;
                            break;

                        case "部分成交":
                            orderRecord.OrderStatus = OrderStatus.PartiallyFilled;
                            break;

                        case "已成":
                            orderRecord.OrderStatus = OrderStatus.Filled;
                            break;

                        case "已报待撤":
                            orderRecord.OrderStatus = OrderStatus.PendingCancel;
                            break;

                        case "已撤":
                            orderRecord.OrderStatus = OrderStatus.Cancelled;
                            break;

                        default:
                            orderRecord.OrderStatus = OrderStatus.Rejected;
                            break;
                        }
                        orderRecord.OrderPrice   = double.Parse(data[i][7]);
                        orderRecord.OrderQty     = double.Parse(data[i][8]);
                        orderRecord.InnerOrderID = data[i][9];
                        orderRecord.AvgPx        = double.Parse(data[i][10]);
                        orderRecord.CumQty       = double.Parse(data[i][11]);
                        orderRecord.OrderType    = (OrderType)int.Parse(data[i][12]);
                        resultList.Add(orderRecord);
                    }
                    reportArgs.Result = resultList;
                }
            }
            else
            {
                reportArgs.Succeeded = false;
                reportArgs.ErrorInfo = "交易账号未登录.";
            }
            return(reportArgs);
        }