Beispiel #1
0
 public void MakeTrade(TTrade Trd)
 {
     if (TradesList == null)
     {
         Init();
     }
     TradesList.Add(Trd);
 }
        /// <summary>
        /// Engine.Check Order for trade
        /// </summary>
        /// <param name="O">Check Order for trade.</param>
        public static bool CheckOrderForTrade(TOrder O)
        {
            bool res = false;

            try
            {
                List <TOrder> Orders4Del = new List <TOrder>();
                int           qty        = O.Quantity;
                var           OBook      = _OB.Orders.Where(w => (w.Side != O.Side && w.Symbol == O.Symbol && (O.Side == TOrderSide.Sell ? w.Price >= O.Price : w.Price <= O.Price)));
                if (OBook.Any())
                {
                    foreach (var OiB in OBook)
                    {
                        bool ONeMoreOrder = false;
                        while (qty > 0 && !ONeMoreOrder)
                        {
                            if (qty >= OiB.Quantity)
                            {
                                TTrade NewTrade = new TTrade();
                                NewTrade.BuyUserID  = O.UserID;
                                NewTrade.SellUserID = OiB.UserID;
                                NewTrade.Symbol     = O.Symbol;
                                NewTrade.Price      = OiB.Price;
                                NewTrade.Quantity   = OiB.Quantity;
                                _TRH.MakeTrade(NewTrade);
                                Console.ForegroundColor = ConsoleColor.Green;
                                Console.WriteLine($"{NewTrade.BuyUserID} buy lot from {NewTrade.SellUserID} - {OiB.Quantity}!!");
                                Console.ForegroundColor = ConsoleColor.White;
                                Logger.Log.Info($"{NewTrade.BuyUserID} buy lot from {NewTrade.SellUserID} - {OiB.Quantity}!!");
                                qty -= OiB.Quantity;
                                Orders4Del.Add(OiB);
                                res          = true;
                                ONeMoreOrder = true;
                            }
                            else
                            {
                                TTrade NewTrade = new TTrade();
                                NewTrade.BuyUserID  = O.UserID;
                                NewTrade.SellUserID = OiB.UserID;
                                NewTrade.Symbol     = O.Symbol;
                                NewTrade.Price      = OiB.Price;
                                NewTrade.Quantity   = qty;
                                _TRH.MakeTrade(NewTrade);
                                Console.ForegroundColor = ConsoleColor.Green;
                                Console.WriteLine($"{NewTrade.BuyUserID} buy lot from {NewTrade.SellUserID} - {qty} !!");
                                Logger.Log.Info($"{NewTrade.BuyUserID} buy lot from {NewTrade.SellUserID} - {qty} !!");
                                Console.ForegroundColor = ConsoleColor.White;
                                OiB.Quantity           -= qty;
                                qty = 0;
                                res = true;
                            }
                        }
                    }


                    if (qty > 0)
                    {
                        O.Quantity = qty;
                        _OB.AddNewOrder(O);
                    }
                    foreach (var OD in Orders4Del)
                    {
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.WriteLine($"Close Order from {OD.UserID}  side/symbol/qty/price  {OD.Side}/{OD.Symbol}/{OD.Quantity}/{O.Price}");
                        Logger.Log.Info($"Close Order from {OD.UserID}  side/symbol/qty/price  {OD.Side}/{OD.Symbol}/{OD.Quantity}/{O.Price}");
                        Console.ForegroundColor = ConsoleColor.White;
                        _OB.Orders.Remove(OD);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorCatcher(ex, "CheckOrderForTrade");
            }
            return(res);
        }