public void AddToOrderBook(OrderLeg orderLeg) { _exchangeBook.NotifyOrderAdded(orderLeg); WatchOrder watchOrder = new WatchOrder(orderLeg); AddOrderToWatchList(watchOrder); }
public void MarkOrderFilled(WatchOrder watchOrder) { // This will ultimately be replaced with logic calling into the repository // //Console.WriteLine("Order ID {0} Leg ID {1} is filled", watchOrder.orderLegRecord.OrderId, watchOrder.orderLegRecord.OrderLegId); watchOrder.orderLegRecord.OrderLegFillStatus = OrderLegFillStatusEnum.Full; _exchangeBook.NotifyOrderLegFilled(watchOrder.orderLegRecord); }
public void SaveFilledOrders(WatchOrder buyOrder, WatchOrder sellOrder, decimal AmountOffset, decimal OrderPrice) { OrderFilledData orderFilledDataRecord = new OrderFilledData(); orderFilledDataRecord.OrderLegId = buyOrder.orderLegRecord.OrderLegId; orderFilledDataRecord.Token1Amount = AmountOffset; orderFilledDataRecord.Token1Id = buyOrder.orderLegRecord.Token1Id; orderFilledDataRecord.Token2Id = buyOrder.orderLegRecord.Token2Id; orderFilledDataRecord.Token2Amount = AmountOffset * (buyOrder.orderLegRecord.OrderPriceTerms == OrderPriceTermsEnum.Token2PerToken1 ? OrderPrice : 1.0M / OrderPrice); orderFilledDataRecord.OrderPriceTerms = buyOrder.orderLegRecord.OrderPriceTerms; orderFilledDataRecord.FilledDateTime = System.DateTime.Now; _exchangeBook.NotifyOrderLegMatched(buyOrder.orderLegRecord, orderFilledDataRecord); _exchangeBook.NotifyOrderLegMatched(sellOrder.orderLegRecord, orderFilledDataRecord); //Console.WriteLine("Updating Order Book for Order {0}/{1} offset amount {2} at price {3}", buyOrder.orderLegRecord.OrderId, sellOrder.orderLegRecord.OrderId, AmountOffset, OrderPrice); }
public void AddOrderToWatchList(WatchOrder watchRecord) { // Buy Limit Orders Descending Order if (watchRecord.orderLegRecord.OrderLegType == OrderLegTypeEnum.Limit) { if (watchRecord.orderLegRecord.BuySellType == OrderLegBuySellEnum.Buy) { lock (LimitBuyLock) { LinkedListNode <WatchOrder> Node = LimitBuyOrders.First; if (Node == null) { LimitBuyOrders.AddFirst(watchRecord); UpdateOrderBookBid(watchRecord); } else { // Locate the Node with a price less than the current order for (; (Node != null) && (Node.Value.orderLegRecord.OrderPrice >= watchRecord.orderLegRecord.OrderPrice); Node = Node.Next) { ; } // End of the road, so add to the end of the list if (Node == null) { LimitBuyOrders.AddLast(watchRecord); } // Insertion Point else { if (Node.Previous == null) { UpdateOrderBookBid(watchRecord); } LimitBuyOrders.AddBefore(Node, watchRecord); } } } MatchLimitBuyWithSells(); } else // Sell Limit Order Ascending Order { lock (LimitSellLock) { LinkedListNode <WatchOrder> Node = LimitSellOrders.First; if (Node == null) { LimitSellOrders.AddFirst(watchRecord); UpdateOrderBookAsk(watchRecord); } else { // Locate the Node with a price less than the current order for (; (Node != null) && (Node.Value.orderLegRecord.OrderPrice <= watchRecord.orderLegRecord.OrderPrice); Node = Node.Next) { ; } // End of the road, so add to the end of the list if (Node == null) { LimitSellOrders.AddLast(watchRecord); } // Insertion Point else { if (Node.Previous == null) { UpdateOrderBookAsk(watchRecord); } LimitSellOrders.AddBefore(Node, watchRecord); } } } MatchLimitSellWithBuys(); } } else if (watchRecord.orderLegRecord.OrderLegType == OrderLegTypeEnum.Stop) { if (watchRecord.BuySellType == OrderLegBuySellEnum.Buy) { LinkedListNode <WatchOrder> Node = StopBuyOrders.First; if (Node == null) { StopBuyOrders.AddFirst(watchRecord); } else { for (; (Node != null) && (Node.Value.OrderPrice <= watchRecord.OrderPrice); Node = Node.Next) { ; } // End of the road, so add to the end of the list if (Node == null) { StopBuyOrders.AddLast(watchRecord); } // Insertion Point else { StopBuyOrders.AddBefore(Node, watchRecord); } } } else { LinkedListNode <WatchOrder> Node = StopSellOrders.First; if (Node == null) { StopSellOrders.AddFirst(watchRecord); } else { // Locate the Node with a price less than the current order for (; (Node != null) && (Node.Value.OrderPrice >= watchRecord.OrderPrice); Node = Node.Next) { ; } // End of the road, so add to the end of the list if (Node == null) { StopSellOrders.AddLast(watchRecord); } // Insertion Point else { StopSellOrders.AddBefore(Node, watchRecord); } } } } else if (watchRecord.orderLegRecord.OrderLegType == OrderLegTypeEnum.Market) { if (watchRecord.BuySellType == OrderLegBuySellEnum.Buy) { lock (MarketBuyLock) { MarketBuyOrders.AddLast(watchRecord); BuyMarketOrdersOutstanding += watchRecord.AmountOutstanding; } MatchMarketBuyWithSells(); } else { lock (MarketSellLock) { MarketSellOrders.AddLast(watchRecord); SellMarketOrdersOutstanding += watchRecord.AmountOutstanding; } MatchMarketSellWithBuys(); } } else { // Don't add it to the list } }
public void UpdateOrderBookAsk(WatchOrder OrderWatchRecord) { AskRate = OrderWatchRecord.OrderPrice; AskAmount = OrderWatchRecord.AmountOutstanding; }
public void UpdateOrderBookBid(WatchOrder OrderWatchRecord) { BidRate = OrderWatchRecord.OrderPrice; BidAmount = OrderWatchRecord.AmountOutstanding; }
public void OffsetBuySellOrders(WatchOrder buyOrder, WatchOrder sellOrder) { decimal AmountOffset = 0; decimal OrderPrice = 0; if (buyOrder.AmountOutstanding > sellOrder.AmountOutstanding) { AmountOffset = sellOrder.AmountOutstanding; } else { AmountOffset = buyOrder.AmountOutstanding; } buyOrder.orderLegRecord.Token1AmountFilled += AmountOffset; sellOrder.orderLegRecord.Token1AmountFilled += AmountOffset; if (buyOrder.orderLegRecord.OrderLegType == OrderLegTypeEnum.Market) { BuyMarketOrdersOutstanding -= AmountOffset; } if (sellOrder.orderLegRecord.OrderLegType == OrderLegTypeEnum.Market) { SellMarketOrdersOutstanding -= AmountOffset; } if (buyOrder.orderLegRecord.OrderLegType == OrderLegTypeEnum.Market) { if (sellOrder.orderLegRecord.OrderLegType == OrderLegTypeEnum.Market) { // Market Price was established when orders were paired OrderPrice = sellOrder.OrderPrice; } else { // Order Price is the Limit Price on the Sell Order; OrderPrice = sellOrder.OrderPrice; } } else if (buyOrder.orderLegRecord.OrderLegType == OrderLegTypeEnum.Limit) { if (sellOrder.orderLegRecord.OrderLegType == OrderLegTypeEnum.Market) { OrderPrice = buyOrder.OrderPrice; } else { OrderPrice = buyOrder.OrderPrice; } } else { throw new NotImplementedException("Order Type not Implemented"); } // Update the Database to reflect // the changes to the Order Legs and the amounts filled for each order leg SaveFilledOrders(buyOrder, sellOrder, AmountOffset, OrderPrice); //Console.WriteLine("Matched Asset Pair {0}-{1} Order {2}/{3} - {4}/{5} for {6}", buyOrder.AssetID1, buyOrder.AssetID2, buyOrder.OrderLegID, buyOrder.OrderType.ToString(), sellOrder.OrderLegID, sellOrder.OrderType.ToString(), AmountOffset); //Console.WriteLine("Asset Pair {0}-{1} Outstanding Mkt Buy {2} / Mkt Sell {3}", buyOrder.AssetID1, buyOrder.AssetID2, BuyMarketOrdersOutstanding, SellMarketOrdersOutstanding); //Console.WriteLine("Buy Order {0} Type {1} Outstanding {2}", buyOrder.orderLegRecord.OrderLegID, buyOrder.OrderType.ToString(), buyOrder.AmountOutstanding); //Console.WriteLine("Sell Order {0} Type {1} Outstanding {2}", sellOrder.orderLegRecord.OrderLegID, sellOrder.OrderType.ToString(), sellOrder.AmountOutstanding); }