Beispiel #1
0
        public void TestAddOrder()
        {
            Trader    trader    = new TraderService().FindTraderById(1);
            Strategy  strategy  = new StrategyService().FindStrategyById(1);
            OrderBook orderBook = new OrderBookService().FindOrderBookById(1);

            Order order = new Order {
                DateTime = DateTime.Now, Execution = null, IsBuy = true, Price = 12.3, Quantity = 12, Trader = trader
            };

            new OrderService().AddOrder(order);
        }
Beispiel #2
0
        public Execution buyorsell(Order order)
        {
            OrderBookService orderBookService = new OrderBookService();
            OrderBook        orderBook        = orderBookService.FindOrderBookById(order.OrderBookId);
            List <OrderBook> orderBookList    = orderBookService.FindOrderBookBySymbol(orderBook.Symbol);

            if (order.IsBuy)
            {
                var total = 0;
                foreach (OrderBook Q in orderBookList)
                {
                    if (order.Price == Q.Price)
                    {
                        total += Q.Quantity;
                    }
                }
                Execution exection = new Execution();
                exection.Trades  = new List <Trade>();
                exection.OrderId = order.Id;
                exection.Order   = order;

                if (order.Quantity > total)
                {
                    exection.Trades.Add(new Trade {
                        Id = order.Id, Price = order.Price, Quantity = order.Quantity, IsSuccess = false
                    });
                    order.Status = "Pending";
                }
                else
                {
                    int i = 0;
                    exection.Trades.Add(new Trade {
                        Id = i, Price = order.Price, Quantity = order.Quantity, IsSuccess = true
                    });
                    i++;
                    order.Status = "Completed";
                }

                exection.DateTime = DateTime.Now;
                return(exection);
            }

            //卖
            else
            {
                var total = 0;
                foreach (OrderBook Q in orderBookList)
                {
                    if (order.Price == Q.Price)
                    {
                        total += Q.Quantity;
                    }
                }
                Execution exection = new Execution();
                exection.Trades  = new List <Trade>();
                exection.OrderId = order.Id;
                exection.Order   = order;

                if (order.Quantity > total)
                {
                    exection.Trades.Add(new Trade {
                        Id = order.Id, Price = order.Price, Quantity = order.Quantity, IsSuccess = false
                    });
                    order.Status = "Pending";
                }
                else
                {
                    int i = 0;
                    exection.Trades.Add(new Trade {
                        Id = i, Price = order.Price, Quantity = order.Quantity, IsSuccess = true
                    });
                    i++;
                    order.Status = "Completed";
                }

                exection.DateTime = DateTime.Now;

                return(exection);
            }
        }
Beispiel #3
0
 /// <summary>
 /// 获得订单
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public static IList <OrderBook> GetDetailOrderById(int id)
 {
     return(OrderBookService.GetDetailOrderById(id));
 }
Beispiel #4
0
 /// <summary>
 /// 添加订单中的图书
 /// </summary>
 /// <param name="order"></param>
 public static void AddOrderBook(OrderBook order)
 {
     OrderBookService.AddOrderBook(order);
 }
Beispiel #5
0
 //通过Id或得订单
 public static IList <OrderBook> GetOrderBookById(int Id)
 {
     return(OrderBookService.GetOrderBookById(Id));
 }
Beispiel #6
0
 public static OrderBook GetOrderBookById(int id)
 {
     return(OrderBookService.GetOrderBookById(id));
 }
Beispiel #7
0
 public static IList <OrderBook> GetAllOrderBooks()
 {
     return(OrderBookService.GetAllOrderBooks());
 }
Beispiel #8
0
 public static void ModifyOrderBook(OrderBook orderBook)
 {
     OrderBookService.ModifyOrderBook(orderBook);
 }
Beispiel #9
0
 public static void DeleteOrderBookById(int id)
 {
     OrderBookService.DeleteOrderBookById(id);
 }
Beispiel #10
0
 public static void DeleteOrderBook(OrderBook orderBook)
 {
     OrderBookService.DeleteOrderBook(orderBook);
 }
Beispiel #11
0
 public static OrderBook AddOrderBook(OrderBook orderBook)
 {
     return(OrderBookService.AddOrderBook(orderBook));
 }
        public Execution buyorsell(Order order)
        {
            OrderBookService orderBookService = new OrderBookService();
            OrderBook        orderBook        = orderBookService.FindOrderBookById(order.OrderBookId);
            List <OrderBook> orderBookList    = orderBookService.FindOrderBookBySymbol(orderBook.Symbol);

            int orginQuantity = order.Quantity;

            if (order.IsBuy)
            {
                orderBookList.Sort(delegate(OrderBook x, OrderBook y)
                {
                    return(x.Price.CompareTo(y.Price));
                });
                var total = 0;
                foreach (OrderBook Q in orderBookList)
                {
                    total += Q.Quantity;
                }
                Execution exection = new Execution();
                exection.Trades = new List <Trade>();

                if (order.Quantity > total)
                {
                    exection.Trades.Add(new Trade {
                        Id = order.Id, Price = order.Price, Quantity = order.Quantity, IsSuccess = false
                    });
                    order.Status = "Rejected";
                }
                else
                {
                    int i = 0;
                    while (order.Quantity > 0)
                    {
                        if (order.Quantity < orderBookList[i].Quantity)
                        {
                            exection.Trades.Add(new Trade {
                                Id = i, Price = orderBookList[i].Price, Quantity = order.Quantity, IsSuccess = true
                            });
                        }
                        else
                        {
                            exection.Trades.Add(new Trade {
                                Id = i, Price = orderBookList[i].Price, Quantity = orderBookList[i].Quantity, IsSuccess = true
                            });
                        }
                        order.Quantity = order.Quantity - orderBookList[i].Quantity;
                        i++;
                    }

                    order.Status   = "Completed";
                    order.Quantity = orginQuantity;
                }

                exection.DateTime = DateTime.Now;
                return(exection);
            }
            //卖
            else
            {
                orderBookList.Sort(delegate(OrderBook x, OrderBook y)
                {
                    return(y.Price.CompareTo(x.Price));
                });
                var total = 0;
                foreach (OrderBook Q in orderBookList)
                {
                    total += Q.Quantity;
                }
                Execution exection = new Execution();
                exection.Trades = new List <Trade>();
                if (order.Quantity > total)
                {
                    exection.Trades.Add(new Trade {
                        Id = order.Id, Price = order.Price, Quantity = order.Quantity, IsSuccess = false
                    });
                    order.Status = "Rejected";
                }
                else
                {
                    int i = 0;
                    while (order.Quantity > 0)
                    {
                        if (order.Quantity < orderBookList[i].Quantity)
                        {
                            exection.Trades.Add(new Trade {
                                Price = orderBookList[i].Price, Quantity = order.Quantity, IsSuccess = true
                            });
                        }
                        else
                        {
                            exection.Trades.Add(new Trade {
                                Price = orderBookList[i].Price, Quantity = orderBookList[i].Quantity, IsSuccess = true
                            });
                        }
                        order.Quantity = order.Quantity - orderBookList[i].Quantity;
                        i++;
                    }

                    order.Status   = "Completed";
                    order.Quantity = orginQuantity;
                }

                exection.DateTime = DateTime.Now;
                return(exection);
            }
        }