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); }
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); } }
/// <summary> /// 获得订单 /// </summary> /// <param name="id"></param> /// <returns></returns> public static IList <OrderBook> GetDetailOrderById(int id) { return(OrderBookService.GetDetailOrderById(id)); }
/// <summary> /// 添加订单中的图书 /// </summary> /// <param name="order"></param> public static void AddOrderBook(OrderBook order) { OrderBookService.AddOrderBook(order); }
//通过Id或得订单 public static IList <OrderBook> GetOrderBookById(int Id) { return(OrderBookService.GetOrderBookById(Id)); }
public static OrderBook GetOrderBookById(int id) { return(OrderBookService.GetOrderBookById(id)); }
public static IList <OrderBook> GetAllOrderBooks() { return(OrderBookService.GetAllOrderBooks()); }
public static void ModifyOrderBook(OrderBook orderBook) { OrderBookService.ModifyOrderBook(orderBook); }
public static void DeleteOrderBookById(int id) { OrderBookService.DeleteOrderBookById(id); }
public static void DeleteOrderBook(OrderBook orderBook) { OrderBookService.DeleteOrderBook(orderBook); }
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); } }