public USeOrderBook ConvertModelOrder(OrderBookViewModel order_data_model)
        {
            USeOrderBook order_book = new USeOrderBook();

            if (order_data_model == null)
            {
                return(order_book);
            }

            order_book.OrderNum    = order_data_model.OrderNum;
            order_book.Account     = order_data_model.Account;
            order_book.Instrument  = order_data_model.Instrument;
            order_book.OrderQty    = order_data_model.OrderQty;
            order_book.OrderPrice  = order_data_model.OrderPrice;
            order_book.TradeQty    = order_data_model.TradeQty;
            order_book.TradeAmount = order_data_model.TradeAmount;
            order_book.TradePrice  = order_data_model.TradePrice;
            order_book.TradeFee    = order_data_model.TradeFee;
            order_book.OrderStatus = order_data_model.OrderStatus;
            order_book.CancelQty   = order_data_model.CancelQty;
            //order_book.BlankQty = order_data_model.BlankQty;
            order_book.OrderSide  = order_data_model.OrderSide;
            order_book.OffsetType = order_data_model.OffsetType;
            order_book.Memo       = order_data_model.Memo;
            order_book.OrderTime  = order_data_model.OrderTime;
            //order_book.IsFinish = order_data_model.IsFinish;
            return(order_book);
        }
Beispiel #2
0
        /// <summary>
        /// 委托下单。
        /// </summary>
        /// <param name="instrument">委托产品。</param>
        /// <param name="qty">委托量。</param>
        /// <param name="price">委托价格。</param>
        /// <param name="offsetType">开平仓方向。</param>
        /// <param name="orderSide">买卖方向。</param>
        /// <param name="error">[out]委托失败原因。</param>
        /// <returns>委托单号。</returns>
        /// <remarks>返回为null代表失败,否则为委托单号。</remarks>
        public override USeOrderNum PlaceOrder(USeInstrument instrument, int qty, decimal price, USeOffsetType offsetType, USeOrderSide orderSide, out string error)
        {
            error = "";
            System.Diagnostics.Debug.Assert(instrument != null);

            int orderNumValue = m_orderNumCreateor.Next();

            USeOrderBook orderBook = new USeOrderBook();

            orderBook.OrderNum    = new TestOrderNum(orderNumValue);
            orderBook.Account     = m_investorID;
            orderBook.Instrument  = instrument;
            orderBook.OrderQty    = qty;
            orderBook.OrderPrice  = price;
            orderBook.TradeQty    = 0;
            orderBook.TradeAmount = 0;
            orderBook.TradePrice  = 0;
            orderBook.TradeFee    = 0;
            orderBook.OrderStatus = USeOrderStatus.Unknown;
            orderBook.CancelQty   = 0;
            orderBook.OrderSide   = orderSide;
            orderBook.OffsetType  = offsetType;
            orderBook.Memo        = "";
            orderBook.OrderTime   = DateTime.Now;

            string error_orderInfo = string.Empty;

            VerfiyPlaceOrderToReturn(orderBook, out error_orderInfo);

            if (error_orderInfo == "开仓")
            {
                m_dataBuffer.OrderBookList.Add(orderBook);

                m_pushOrderBookList.Enqueue(orderBook);
                error = "Place Order Ok";

                return(orderBook.OrderNum);
            }
            else if (error_orderInfo == "CloseSuccessed")
            {
                //开仓
                orderBook.Memo = "平仓委托成功";
                m_dataBuffer.OrderBookList.Add(orderBook);

                m_pushOrderBookList.Enqueue(orderBook);
                error = "Place Order Ok";
                return(orderBook.OrderNum);
            }
            else
            {
                //平仓委托失败
                orderBook.Memo        = error_orderInfo;
                orderBook.OrderStatus = USeOrderStatus.BlankOrder;

                m_pushOrderBookList.Enqueue(orderBook);
                error = "Place Order Failed";
                return(orderBook.OrderNum);
            }
        }
        private ArbitrageTaskOrderBookViewModel CreateOrderBookVm(USeOrderBook orderBook, int taskId)
        {
            ArbitrageTaskOrderBookViewModel viewModel = new ArbitrageTaskOrderBookViewModel();

            viewModel.Update(orderBook);
            viewModel.TaskId = taskId;
            return(viewModel);
        }
Beispiel #4
0
        /// <summary>
        /// 更新委托回报。
        /// </summary>
        /// <param name="orderBook">委托回报。</param>
        /// <returns>更新委托回报条数。</returns>
        public OrderBookUpdateResult UpdateOrderBook(USeOrderBook orderBook)
        {
            OrderBookUpdateResult result = m_firstSubTask.UpdateOrderBook(orderBook);

            if (result != null)
            {
                return(result);
            }
            result = m_secondSubTask.UpdateOrderBook(orderBook);
            return(result);
        }
Beispiel #5
0
        private OrderBookUpdateResult GetOrderBookUpdateResult(USeOrderBook oldOrderBook, USeOrderBook newOrderBook)
        {
            OrderBookUpdateResult result = new OrderBookUpdateResult()
            {
                TradeQty  = newOrderBook.TradeQty - oldOrderBook.TradeQty,
                CancelQty = newOrderBook.CancelQty - oldOrderBook.CancelQty,
                BlankQty  = newOrderBook.BlankQty - oldOrderBook.BlankQty
            };

            return(result);
        }
Beispiel #6
0
 //查询队列有任务发布事件
 public void execEventList()
 {
     while (m_runFlag)
     {
         Thread.Sleep(500);
         if (m_pushTradeBookList.Count > 0)
         {
             USeTradeBook tradeBook = m_pushTradeBookList.Dequeue();
             if (tradeBook != null)
             {
                 try
                 {
                     base.FireTradeBookChanged(tradeBook, true);
                 }
                 catch (Exception ex)
                 {
                     System.Diagnostics.Debug.Assert(false);
                 }
             }
         }
         if (m_pushOrderBookList.Count > 0)
         {
             USeOrderBook order_book = m_pushOrderBookList.Dequeue();
             if (order_book != null)
             {
                 try
                 {
                     base.FireOrderBookChanged(order_book);
                 }
                 catch (Exception ex)
                 {
                     System.Diagnostics.Debug.Assert(false);
                 }
             }
         }
         if (m_pushPositionList.Count > 0)
         {
             USePosition position_book = m_pushPositionList.Dequeue();
             if (position_book != null)
             {
                 try
                 {
                     base.FirePositionChanged(position_book);
                 }
                 catch (Exception ex)
                 {
                     System.Diagnostics.Debug.Assert(false);
                 }
             }
         }
     }
 }
        private void UpdateOrderBook(USeOrderBook orderBook)
        {
            OrderBookViewModel marketModel = m_orderDataSource.FirstOrDefault(p => p.OrderNum.Equals(orderBook.OrderNum));

            if (marketModel != null)
            {
                marketModel.Update(orderBook);
            }
            else
            {
                OrderBookViewModel order_data_model = OrderBookViewModel.Creat(orderBook);
                m_orderDataSource.Insert(0, order_data_model);
            }
        }
Beispiel #8
0
        /// <summary>
        /// 查询指定委托单号的委托回报。
        /// </summary>
        /// <param name="orderNum"></param>
        /// <returns></returns>
        public override USeOrderBook QueryOrderBook(USeOrderNum orderNum)
        {
            USeOrderBook order_book = new USeOrderBook();

            order_book = (from p in m_dataBuffer.OrderBookList
                          where p.OrderNum == orderNum
                          select p).FirstOrDefault();
            if (order_book == null)
            {
                return(null);
            }

            return(order_book);
        }
Beispiel #9
0
        public void CanceledOrder(USeOrderNum orderNum)
        {
            USeOrderBook order_book = (from p in m_dataBuffer.OrderBookList
                                       where p.OrderNum.Equals(orderNum)
                                       select p).FirstOrDefault();

            if (order_book == null)
            {
                return;
            }

            string error = string.Empty;

            CancelOrder(order_book.OrderNum, null, out error);
        }
Beispiel #10
0
        private static USeOrderBook CreateUseOrderBook()
        {
            USeOrderBook orderBook = new USeOrderBook();

            orderBook.Account = "090952";

            orderBook.OrderNum   = new USe.TradeDriver.Ctp.CtpOrderNum(1, 1, "1111");
            orderBook.CancelQty  = 0;
            orderBook.Instrument = new USeInstrument("CF1710", "棉花1710", USeMarket.CFFEX);
            orderBook.Memo       = "开仓任务0";
            orderBook.OffsetType = USeOffsetType.Open;
            orderBook.Account    = "090952";
            orderBook.Account    = "090952";
            return(orderBook);
        }
Beispiel #11
0
        /// <summary>
        /// 更新委托回报。
        /// </summary>
        /// <param name="orderBook">委托回报。</param>
        /// <returns>更新委托回报条数。</returns>
        public OrderBookUpdateResult UpdateOrderBook(USeOrderBook orderBook)
        {
            for (int i = 0; i < m_orderBookList.Count; i++)
            {
                USeOrderBook orderBookItem = m_orderBookList[i];
                if (orderBookItem.OrderNum.Equals(orderBook.OrderNum))
                {
                    OrderBookUpdateResult result = GetOrderBookUpdateResult(m_orderBookList[i], orderBook);
                    m_orderBookList[i] = orderBook.Clone();
                    return(result);
                }
            }

            return(null);
        }
Beispiel #12
0
        public bool CanceledOrderActionReturn(USeOrderNum orderNum, bool isCancelSucceed)
        {
            if (isCancelSucceed == false)
            {
                return(false);
            }

            string error = "errorInfo";

            //Orderlist中找到OrderNum对应的单子删除
            if (orderNum == null)
            {
                return(false);
            }
            if (m_dataBuffer.OrderBookList.Count <= 0)
            {
                return(false);
            }
            USeOrderBook order_book = (from p in m_dataBuffer.OrderBookList
                                       where p.OrderNum.Equals(orderNum)
                                       select p).FirstOrDefault();

            if (order_book == null)
            {
                return(false);
            }

            if (order_book.TradeQty == order_book.OrderQty)
            {
                return(false);
            }

            if (order_book.TradeQty == 0)
            {
                order_book.OrderStatus = USeOrderStatus.AllCanceled;
                order_book.CancelQty   = order_book.OrderQty;
            }
            else
            {
                System.Diagnostics.Debug.Assert(order_book.TradeQty > 0);
                order_book.OrderStatus = USeOrderStatus.PartCanceled;
                order_book.CancelQty   = order_book.OrderQty - order_book.TradeQty;
            }

            m_pushOrderBookList.Enqueue(order_book.Clone());

            return(true);
        }
Beispiel #13
0
        /// <summary>
        /// 检查委托单委托类型和数量
        /// </summary>
        /// <param name="orderBook"></param>
        public void VerfiyPlaceOrderToReturn(USeOrderBook orderBook, out string error)
        {
            error = "";
            Debug.Assert(orderBook != null);

            error = "开仓";
            if (orderBook.OffsetType == USeOffsetType.Open)
            {
                return;
            }
            List <USePosition> positionHoldNowList = m_dataBuffer.PositionDataList;

            if (positionHoldNowList == null)
            {
                return;
            }

            foreach (USePosition p in positionHoldNowList)
            {
                //同一合约名,持仓和目前的委托方向不一致开始检查
                if (p.Instrument != orderBook.Instrument)
                {
                    continue;
                }
                if (CheckOrderDirection(p.Direction, orderBook.OrderSide))
                {
                    continue;
                }
                if (orderBook.OrderQty > p.TotalPosition && orderBook.OffsetType == USeOffsetType.Close)
                {
                    error = "平仓仓位不足";
                    return;
                }
                if (orderBook.OrderQty > p.NewPosition && orderBook.OffsetType == USeOffsetType.CloseToday)
                {
                    error = "平今仓位不足";
                    return;
                }
                if (orderBook.OrderQty > p.OldPosition && orderBook.OffsetType == USeOffsetType.CloseHistory)
                {
                    error = "平昨仓位不足";
                    return;
                }
            }
            error = "CloseSuccessed";
            return;
        }
Beispiel #14
0
        public void AllTrade(USeOrderNum orderNum, int slipPoint)
        {
            USeOrderBook orderBook = (from p in m_dataBuffer.OrderBookList
                                      where p.OrderNum.Equals(orderNum)
                                      select p).FirstOrDefault();

            if (orderBook == null)
            {
                return;
            }

            USeInstrumentDetail instrumentDetail = QueryInstrumentDetail(orderBook.Instrument);

            int     tradeQty   = (orderBook.OrderQty - orderBook.TradeQty);
            decimal tradePrice = 0;

            if (orderBook.OrderSide == USeOrderSide.Buy)
            {
                tradePrice = orderBook.OrderPrice - instrumentDetail.PriceTick * slipPoint;
            }
            else if (orderBook.OrderSide == USeOrderSide.Sell)
            {
                tradePrice = orderBook.OrderPrice + instrumentDetail.PriceTick * slipPoint;
            }
            int volumeMultiple    = instrumentDetail.VolumeMultiple;
            int orderBookTradeQty = orderBook.TradeQty + tradeQty;

            orderBook.TradeQty    = orderBookTradeQty;
            orderBook.TradeAmount = tradePrice * orderBookTradeQty * volumeMultiple;
            orderBook.TradePrice  = tradePrice;
            orderBook.TradeFee    = 5 * orderBookTradeQty;
            orderBook.CancelQty   = 0;
            orderBook.Memo        = orderBook.Memo;
            orderBook.OrderTime   = DateTime.Now;
            orderBook.OrderStatus = USeOrderStatus.AllTraded;

            USeTradeBook tradeBook = CreateTradeBook(orderBook, tradeQty, tradePrice, tradeQty * tradePrice * volumeMultiple, 5 * tradeQty);

            USePosition positionBook = CreatePositionBook(tradeBook);

            m_pushTradeBookList.Enqueue(tradeBook);

            m_pushOrderBookList.Enqueue(orderBook.Clone());
            //推送持仓信息
            m_pushPositionList.Enqueue(positionBook.Clone());
        }
Beispiel #15
0
        /// <summary>
        /// 更新委托回报。
        /// </summary>
        /// <param name="orderBook">委托回报。</param>
        /// <returns>影响的任务。</returns>
        public OrderBookUpdateResult UpdateOrderBook(USeOrderBook orderBook)
        {
            OrderBookUpdateResult result = m_openTaskGroup.UpdateOrderBook(orderBook);

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

            result = m_closeTaskGroup.UpdateOrderBook(orderBook);
            if (result != null)
            {
                return(result);
            }

            return(null);
        }
Beispiel #16
0
        /// <summary>
        /// 新增正向委托。
        /// </summary>
        /// <param name="orderBook">正向委托回报。</param>
        public void AddPositiveOrderBook(USeOrderBook orderBook)
        {
            Debug.Assert(ContainsOrderBook(orderBook.OrderNum) == false);
            Debug.Assert(orderBook.Instrument == this.Instrument);
            Debug.Assert(orderBook.OrderSide == this.OrderSide);
            if (this.OffsetType == USeOffsetType.Open)
            {
                Debug.Assert(orderBook.OffsetType == this.OffsetType);
            }
            else
            {
                Debug.Assert((orderBook.OffsetType == USeOffsetType.Close) ||
                             (orderBook.OffsetType == USeOffsetType.CloseHistory) ||
                             (orderBook.OffsetType == USeOffsetType.CloseToday));
            }

            m_orderBookList.Add(orderBook);
        }
Beispiel #17
0
        /// <summary>
        /// 新增反向委托。
        /// </summary>
        /// <param name="orderBook">反向委托回报。</param>
        public void AddNegativeOrderBook(USeOrderBook orderBook)
        {
            Debug.Assert(ContainsOrderBook(orderBook.OrderNum) == false);
            Debug.Assert(orderBook.Instrument == this.Instrument);
            Debug.Assert(orderBook.OrderSide == this.OrderSide.GetOppositeOrderSide());
            if (this.OffsetType == USeOffsetType.Open)
            {
                Debug.Assert(orderBook.OffsetType == USeOffsetType.Close ||
                             orderBook.OffsetType == USeOffsetType.CloseHistory ||
                             orderBook.OffsetType == USeOffsetType.CloseToday);
            }
            else
            {
                Debug.Assert(orderBook.OffsetType == USeOffsetType.Open);
            }

            m_orderBookList.Add(orderBook);
        }
Beispiel #18
0
        private USeTradeBook CreateTradeBook(USeOrderBook orderBook, int tradeQty, decimal tradePrice, decimal amount, decimal fee)
        {
            USeTradeBook tradeBook = new USeTradeBook();

            tradeBook.TradeNum   = m_tradeNumCreateor.Next().ToString();
            tradeBook.Instrument = orderBook.Instrument.Clone();
            tradeBook.OrderNum   = orderBook.OrderNum;
            tradeBook.OrderSide  = orderBook.OrderSide;
            tradeBook.OffsetType = orderBook.OffsetType;
            tradeBook.Price      = tradePrice;
            tradeBook.Qty        = tradeQty;
            tradeBook.Amount     = amount;
            tradeBook.Fee        = fee;
            tradeBook.TradeTime  = DateTime.Now;
            tradeBook.Account    = this.Account;

            return(tradeBook);
        }
Beispiel #19
0
        /// <summary>
        /// 检查套利单是否有异常委托。
        /// </summary>
        /// <remarks>
        /// 1.非今日的历史委托回报未结束,无法更新,列入异常套利单
        /// </remarks>
        private ErrorArbitrageOrder CheckErrorArbitrageOrder(USeArbitrageOrder arbitrageOrder)
        {
            ErrorArbitrageOrder errorArbitrageOrder = new ErrorArbitrageOrder();

            errorArbitrageOrder.ArbitrageOrder = arbitrageOrder;

            List <ErrorUSeOrderBook> errorBookList = new List <ErrorUSeOrderBook>();

            if (arbitrageOrder.HasUnFinishOrderBook == false)
            {
                return(errorArbitrageOrder);
            }

            List <USeOrderBook> unFinishOrderBookList = arbitrageOrder.GetAllUnfinishOrderBooks();

            Debug.Assert(unFinishOrderBookList != null && unFinishOrderBookList.Count > 0);

            foreach (USeOrderBook orderBook in unFinishOrderBookList)
            {
                USeOrderBook newOrderBook = m_orderDriver.QueryOrderBook(orderBook.OrderNum);
                if (newOrderBook != null)
                {
                    OrderBookUpdateResult updateResult = arbitrageOrder.UpdateOrderBook(newOrderBook);
                    updateResult.Task.UpdateTaskState();
                    arbitrageOrder.UpdataArbitrageOrderState();
                    errorArbitrageOrder.HasChanged = true;
                }
                else
                {
                    ErrorUSeOrderBook errorOrderBook = new ErrorUSeOrderBook()
                    {
                        TradeIdentify = arbitrageOrder.TraderIdentify,
                        Alias         = arbitrageOrder.Alias,
                        OrderBook     = orderBook.Clone()
                    };
                    errorBookList.Add(errorOrderBook);
                }
            }

            errorArbitrageOrder.ErrorOrderBooks = errorBookList;

            return(errorArbitrageOrder);
        }
Beispiel #20
0
        /// <summary>
        /// 更新委托回报。
        /// </summary>
        /// <param name="orderBook">委托回报。</param>
        public OrderBookUpdateResult UpdateOrderBook(USeOrderBook orderBook)
        {
            foreach (ArbitrageTask task in m_taskList)
            {
                if (task.ContainsOrderBook(orderBook.OrderNum))
                {
                    OrderBookUpdateResult result = task.UpdateOrderBook(orderBook);
                    Debug.Assert(result != null);

                    if (result != null)
                    {
                        result.Task = task;
                        return(result);
                    }
                }
            }

            return(null);
        }
Beispiel #21
0
 public void Update(USeOrderBook entity)
 {
     this.OrderNum    = entity.OrderNum;
     this.Account     = entity.Account;
     this.Instrument  = entity.Instrument;
     this.OrderQty    = entity.OrderQty;
     this.OrderPrice  = entity.OrderPrice;
     this.TradeQty    = entity.TradeQty;
     this.TradeAmount = entity.TradeAmount;
     this.TradePrice  = entity.TradePrice;
     this.TradeFee    = entity.TradeFee;
     this.OrderStatus = entity.OrderStatus;
     this.CancelQty   = entity.CancelQty;
     this.BlankQty    = entity.BlankQty;
     this.OrderSide   = entity.OrderSide;
     this.OffsetType  = entity.OffsetType;
     this.Memo        = entity.Memo;
     this.OrderTime   = entity.OrderTime;
     this.IsFinish    = entity.IsFinish;
 }
Beispiel #22
0
        ////public static List<OrderActionLog> GetSimulateOrderActionLog()
        //{
        //    List<OrderActionLog> list = new List<OrderActionLog>();
        //    DateTime time = new DateTime(2017, 05, 11, 13, 03, 04);

        //    list.Add(new OrderActionLog()
        //    {
        //        Time = time,
        //        Message = "cu1706与cu1707价差达到100点"
        //    });

        //    time = time.AddSeconds(1);
        //    list.Add(new OrderActionLog()
        //    {
        //        Time = time,
        //        Message = "cu1706买入委托下单5手"
        //    });

        //    time = time.AddMinutes(1);
        //    list.Add(new OrderActionLog()
        //    {
        //        Time = time,
        //        Message = "cu1706买入成交3手"
        //    });
        //    time = time.AddSeconds(5);
        //    list.Add(new OrderActionLog()
        //    {
        //        Time = time,
        //        Message = "cu1706买入成交2手"
        //    });
        //    time = time.AddSeconds(1);
        //    list.Add(new OrderActionLog()
        //    {
        //        Time = time,
        //        Message = "cu1706买入委托下单5手"
        //    });
        //    list.Add(new OrderActionLog()
        //    {
        //        Time = time,
        //        Message = "cu1707卖出委托下单5手"
        //    });

        //    return list;
        //}

        public static void TestHanYu()
        {
            USeOrderBook group = CreateUseOrderBook();

            try
            {
                XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
                XmlAttributes         attrs         = new XmlAttributes();
                XmlElementAttribute   attr          = new XmlElementAttribute("ctpNum", typeof(USe.TradeDriver.Ctp.CtpOrderNum));
                attrs.XmlElements.Add(attr);
                attrOverrides.Add(typeof(USeOrderBook), "OrderNum", attrs);

                string fileFullName = @"D:\hanyu.xml";
                TempUtility.SaveToXml(fileFullName, group, attrOverrides);
            }
            catch (Exception ex)
            {
                throw new Exception("SaveUSeArbitrageOrder failed,Error:" + ex.Message);
            }
        }
        /// <summary>
        /// 创建空的委托回报。
        /// </summary>
        /// <param name="orderNum">委托单号。</param>
        /// <param name="instrument">合约。</param>
        /// <param name="orderQty">委托量。</param>
        /// <param name="orderPrice">委托价格。</param>
        /// <param name="offsetType">开平方向。</param>
        /// <param name="orderSide">买卖方向。</param>
        /// <returns></returns>
        private USeOrderBook CreateOrignalUSeOrderBook(USeOrderNum orderNum, USeInstrument instrument, int orderQty, decimal orderPrice, USeOffsetType offsetType, USeOrderSide orderSide)
        {
            USeOrderBook orderBook = new USeOrderBook();

            orderBook.OrderNum    = orderNum;
            orderBook.Account     = string.Empty;
            orderBook.Instrument  = instrument;
            orderBook.OrderQty    = orderQty;
            orderBook.OrderPrice  = orderPrice;
            orderBook.TradeQty    = 0;
            orderBook.TradeAmount = 0m;
            orderBook.TradePrice  = 0m;
            orderBook.TradeFee    = 0m;
            orderBook.OrderStatus = USeOrderStatus.Unknown;
            orderBook.CancelQty   = 0;
            orderBook.OrderSide   = orderSide;
            orderBook.OffsetType  = offsetType;
            orderBook.Memo        = string.Empty;
            orderBook.OrderTime   = DateTime.Now;

            return(orderBook);
        }
Beispiel #24
0
            /// <summary>
            /// 创建原始委托回报。
            /// </summary>
            /// <param name="orderNum"></param>
            /// <returns></returns>
            public USeOrderBook CreateOrignalOrderBook()
            {
                USeOrderBook orderBook = new USeOrderBook();

                orderBook.OrderNum    = this.OrderNum;
                orderBook.Account     = string.Empty;
                orderBook.Instrument  = this.Instrument;
                orderBook.OrderQty    = this.OrderQty;
                orderBook.OrderPrice  = this.OrderPrice;
                orderBook.TradeQty    = 0;
                orderBook.TradeAmount = 0m;
                orderBook.TradePrice  = 0m;
                orderBook.TradeFee    = 0m;
                orderBook.OrderStatus = USeOrderStatus.Unknown;
                orderBook.CancelQty   = 0;
                orderBook.OrderSide   = this.OrderSide;
                orderBook.OffsetType  = this.OffsetType;
                orderBook.Memo        = string.Empty;
                orderBook.OrderTime   = DateTime.Now;

                return(orderBook);
            }
Beispiel #25
0
        private void FillData(List <ErrorUSeOrderBook> errorOrderBooks, DataTable table)
        {
            if (errorOrderBooks == null || table == null)
            {
                return;
            }
            foreach (ErrorUSeOrderBook errorItem in errorOrderBooks)
            {
                USeOrderBook orderBook = errorItem.OrderBook;

                DataRow row = table.NewRow();
                row["Alisa"]          = errorItem.Alias;
                row["OrderTime"]      = orderBook.OrderTime;
                row["OrderSide"]      = orderBook.OrderSide;
                row["OrderSideDesc"]  = orderBook.OrderSide.ToDescription();
                row["IsDone"]         = global::USeFuturesSpirit.Properties.Resources.red;
                row["OffsetType"]     = orderBook.OffsetType;
                row["OffsetTypeDesc"] = orderBook.OffsetType.ToDescription();
                row["Instrument"]     = orderBook.Instrument;
                row["InstrumentCode"] = orderBook.Instrument.InstrumentCode;
                row["DoneFlag"]       = false;
                row["IsDone"]         = global::USeFuturesSpirit.Properties.Resources.red1;

                row["OrderStatus"]         = orderBook.OrderStatus;
                row["OrderStatusDesc"]     = orderBook.OrderStatus.ToDescription();
                row["SelectedOrderStatus"] = (int)USeOrderStatus.Unknown;
                row["ErrorUSeOrderBook"]   = errorItem;

                row["OrderQty"]   = orderBook.OrderQty;
                row["OrderPrice"] = orderBook.OrderPrice;
                row["TradeQty"]   = 0;
                row["TradePrice"] = 0;
                row["TradeFee"]   = 0;
                row["OrderNum"]   = orderBook.OrderNum;
                row["Memo"]       = "请选择委托单处理状态";
                table.Rows.Add(row);
            }
        }
Beispiel #26
0
        public static OrderBookViewModel Creat(USeOrderBook orderBook)
        {
            OrderBookViewModel viewModel = new OrderBookViewModel();

            viewModel.OrderNum    = orderBook.OrderNum;
            viewModel.Account     = orderBook.Account;
            viewModel.Instrument  = orderBook.Instrument;
            viewModel.OrderQty    = orderBook.OrderQty;
            viewModel.OrderPrice  = orderBook.OrderPrice;
            viewModel.TradeQty    = orderBook.TradeQty;
            viewModel.TradeAmount = orderBook.TradeAmount;
            viewModel.TradePrice  = orderBook.TradePrice;
            viewModel.TradeFee    = orderBook.TradeFee;
            viewModel.OrderStatus = orderBook.OrderStatus;
            viewModel.CancelQty   = orderBook.CancelQty;
            viewModel.BlankQty    = orderBook.BlankQty;
            viewModel.OrderSide   = orderBook.OrderSide;
            viewModel.OffsetType  = orderBook.OffsetType;
            viewModel.Memo        = orderBook.Memo;
            viewModel.OrderTime   = orderBook.OrderTime;
            viewModel.IsFinish    = orderBook.IsFinish;
            return(viewModel);
        }
        private void UpdateOrderBook(USeOrderBook orderBook)
        {
            OrderBookViewModel marketModel = m_order_data_source.FirstOrDefault(p => p.OrderNum.Equals(orderBook.OrderNum));

            if (marketModel != null)
            {
                if (Filter(orderBook.OrderStatus))
                {
                    marketModel.Update(orderBook);
                }
                else
                {
                    m_order_data_source.Remove(marketModel);
                }
            }
            else
            {
                if (Filter(orderBook.OrderStatus))
                {
                    OrderBookViewModel order_data_model = OrderBookViewModel.Creat(orderBook);
                    m_order_data_source.Insert(0, order_data_model);
                }
            }
        }
        /// <summary>
        /// CTP TradeField To USeTradeBook。
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        private USeTradeBook CtpTradeFieldToUSeTradeBook(TradeField field)
        {
            USeTradeBook tradeBook = new USeTradeBook();

            try
            {
                tradeBook.Instrument = new USeInstrument(field.InstrumentID.Trim(),
                                                         field.InstrumentID.Trim(),
                                                         CtpProtocol.FtdcExchangeToUSeMarket(field.ExchangeID));
                tradeBook.TradeNum = field.TradeID.Trim();
                USeOrderNum  orderNum  = new CtpOrderNum(field.ExchangeID, field.OrderSysID);
                USeOrderBook orderBook = m_dataBuffer.GetOrderBook(orderNum);
                if (orderBook != null)
                {
                    orderNum = orderBook.OrderNum.Clone();  // 去搜索对应委托单,若存在则将委托单号设置为完整委托单号
                }
                tradeBook.OrderNum  = orderNum;
                tradeBook.OrderSide = field.Direction == DirectionType.Buy ? USeOrderSide.Buy : USeOrderSide.Sell;
                switch (field.OffsetFlag)
                {
                case OffsetFlagType.Close:
                case OffsetFlagType.ForceClose:
                    tradeBook.OffsetType = USeOffsetType.Close; break;

                case OffsetFlagType.CloseToday:
                    tradeBook.OffsetType = USeOffsetType.CloseToday; break;

                case OffsetFlagType.CloseYesterday:
                    tradeBook.OffsetType = USeOffsetType.CloseHistory; break;

                case OffsetFlagType.Open:
                    tradeBook.OffsetType = USeOffsetType.Open; break;

                default:
                    Debug.Assert(false, string.Format("CtpTradeFieldToUSeTradeBook(),Invalid offsetfalg.", field.OffsetFlag));
                    break;
                }
                tradeBook.Price   = Convert.ToDecimal(field.Price);
                tradeBook.Qty     = field.Volume;
                tradeBook.Account = field.InvestorID;
                DateTime tradeTime = DateTime.Now;
                if (string.IsNullOrEmpty(field.TradeDate) && string.IsNullOrEmpty(field.TradeTime))
                {
                    Debug.Assert(false);
                    tradeTime = DateTime.Now;
                }
                else if (string.IsNullOrEmpty(field.TradeDate))
                {
                    if (DateTime.TryParseExact(DateTime.Today.ToString("yyyyMMdd") + field.TradeTime, "yyyyMMddHH:mm:ss", null, System.Globalization.DateTimeStyles.None, out tradeTime) == false)
                    {
                        Debug.Assert(false);
                        tradeTime = DateTime.Now;
                    }
                }
                else
                {
                    if (DateTime.TryParseExact(field.TradeDate + field.TradeTime, "yyyyMMddHH:mm:ss", null, System.Globalization.DateTimeStyles.None, out tradeTime) == false)
                    {
                        Debug.Assert(false);
                        tradeTime = DateTime.Now;
                    }
                }

                tradeBook.TradeTime = tradeTime;

                int volumeMultiple = m_dataBuffer.GetVolumeMultiple(tradeBook.Instrument);

                tradeBook.Amount = tradeBook.Price * tradeBook.Qty * volumeMultiple;
                // 手续费尝试计算
                tradeBook.Fee = m_dataBuffer.CalculateFee(tradeBook.Instrument, tradeBook.OffsetType, tradeBook.Qty, tradeBook.Price);
            }
            catch (Exception ex)
            {
                Debug.Assert(false, "CtpTradeFieldToUSeTradeBook() convet failed," + ex.Message);
            }
            return(tradeBook);
        }
        /// <summary>
        /// CTP OrderField To USeOrderBook。
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        private USeOrderBook CtpOrderFieldToUSeOrderBook(OrderField field)
        {
            USeOrderBook orderbook = new USeOrderBook();

            try
            {
                orderbook.OrderNum = new CtpOrderNum(field.FrontID,
                                                     field.SessionID,
                                                     field.OrderRef,
                                                     field.ExchangeID,
                                                     field.OrderSysID);
                orderbook.Account    = field.InvestorID.ToString().Trim();
                orderbook.Instrument = new USeInstrument(field.InstrumentID.Trim(),
                                                         field.InstrumentID.Trim(),
                                                         CtpProtocol.FtdcExchangeToUSeMarket(field.ExchangeID));
                orderbook.OrderQty    = field.VolumeTotalOriginal;
                orderbook.OrderPrice  = Convert.ToDecimal(field.LimitPrice);
                orderbook.TradeQty    = field.VolumeTraded;
                orderbook.TradeAmount = 0m; // 此处转换不处理,最终再计算
                switch (field.OrderStatus)
                {
                case OrderStatusType.AllTraded: orderbook.OrderStatus = USeOrderStatus.AllTraded; break;

                case OrderStatusType.PartTradedNotQueueing:
                case OrderStatusType.PartTradedQueueing: orderbook.OrderStatus = USeOrderStatus.PartTraded; break;

                case OrderStatusType.Canceled: orderbook.OrderStatus = field.VolumeTraded > 0 ? USeOrderStatus.PartCanceled : USeOrderStatus.AllCanceled; break;

                case OrderStatusType.NoTradeQueueing:
                case OrderStatusType.NoTradeNotQueueing: orderbook.OrderStatus = USeOrderStatus.NoTraded; break;

                case OrderStatusType.Unknown: orderbook.OrderStatus = USeOrderStatus.Unknown; break;

                default:
                {
                    Debug.Assert(false, string.Format("CtpOrderFieldToUSeOrderBook() unknown OrderStatus [{0}]", field.OrderStatus));
                    orderbook.OrderStatus = USeOrderStatus.Unknown;
                    break;
                }
                }
                orderbook.CancelQty = field.OrderStatus == OrderStatusType.Canceled ? field.VolumeTotalOriginal - field.VolumeTraded : 0;
                orderbook.OrderSide = field.Direction == DirectionType.Buy ? USeOrderSide.Buy : USeOrderSide.Sell;
                switch (field.CombOffsetFlag1)
                {
                case OffsetFlagType.Close:
                case OffsetFlagType.ForceClose: orderbook.OffsetType = USeOffsetType.Close; break;

                case OffsetFlagType.CloseToday: orderbook.OffsetType = USeOffsetType.CloseToday; break;

                case OffsetFlagType.CloseYesterday: orderbook.OffsetType = USeOffsetType.CloseHistory; break;

                case OffsetFlagType.Open:
                    orderbook.OffsetType = USeOffsetType.Open; break;

                default:
                    Debug.Assert(false, "Invalid comb offset Flag1.");
                    break;
                }
                orderbook.Memo = field.StatusMsg;


                DateTime orderTime = DateTime.Now;
                if (string.IsNullOrEmpty(field.InsertDate) && string.IsNullOrEmpty(field.InsertTime))
                {
                    Debug.Assert(false);
                    orderTime = DateTime.Now;
                }
                else if (string.IsNullOrEmpty(field.InsertDate))
                {
                    if (DateTime.TryParseExact(DateTime.Today.ToString("yyyyMMdd") + field.InsertTime, "yyyyMMddHH:mm:ss", null, System.Globalization.DateTimeStyles.None, out orderTime) == false)
                    {
                        Debug.Assert(false);
                        orderTime = DateTime.Now;
                    }
                }
                else
                {
                    if (DateTime.TryParseExact(field.InsertDate + field.InsertTime, "yyyyMMddHH:mm:ss", null, System.Globalization.DateTimeStyles.None, out orderTime) == false)
                    {
                        Debug.Assert(false);
                        orderTime = DateTime.Now;
                    }
                }
                orderbook.OrderTime = orderTime;
            }
            catch (Exception ex)
            {
                Debug.Assert(false, "CtpOrderFieldToUSeOrderBook() convet failed," + ex.Message);
            }
            return(orderbook);
        }