Ejemplo n.º 1
0
 public void AddNewOrder(TOrder O)
 {
     if (Orders != null)
     {
         TickList.Add(new TTick(O.Price, O.Quantity));
         if (LastInsertedOrder == null)
         {
             LastInsertedOrder = Orders.AddFirst(O);
         }
         else
         {
             LastInsertedOrder = Orders.AddAfter(LastInsertedOrder, O);
         }
         SetTOBChangeState(true);
     }
 }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Read Command from Client
        /// </summary>
        /// <param name="client">TCPCLIENT</param>
        /// <param name="clientid">ClientID.</param>
        /// <param name="sCommand">received command text.</param>
        /// <param name="byte_count">received length of sCommand.</param>
        public static void readCommand(TcpClient client, string clientid, byte[] sCommand, int byte_count)
        {
            string exception4send = "";
            string ReadData       = Encoding.ASCII.GetString(sCommand, 0, byte_count);

            Console.WriteLine(client.Client.RemoteEndPoint + ":" + ReadData);
            switch (ReadData.Split(';').First())
            {
            case "/UPDATELASTGUIDORDERS":
                string guid4update = ReadData.Split(';')[1].ToString();
                _OB.UpdateLastGuid(guid4update, clientid);
                _TRH.UpdateLastGuid(guid4update, clientid);
                break;

            case "/GETORDERBOOK":
                SendOrderBooktoClients(_OB, client);
                SendTradeHistorytoClients(_TRH);
                break;

            case "/ADDNEWORDER":
                string OrderStr = ReadData.Split(';')[1].ToString();
                if (OrderStr.Length > 0)
                {
                    var  O         = TOrder.Deserialize(OrderStr);
                    bool TradeDone = CheckOrderForTrade(O);
                    if (!TradeDone)
                    {
                        _OB.AddNewOrder(O);
                    }
                    else
                    {
                        SendTradeHistorytoClients(_TRH);
                    }
                    SendOrderBooktoClients(_OB);
                }
                break;

            case "/REMOVEORDER":
                SendOrderBooktoClients(_OB);
                break;

            case "/CLIENTNUMBER":
                clientid = ReadData.Split(';')[1]?.ToString();
                SetCurrentUserID(clientid);
                if (_LastUserID.Length > 0)
                {
                    Sendbuf2Client(client, Encoding.ASCII.GetBytes("/UPDATELASTGUIDORDERS;" + _LastUserID));
                }
                File.WriteAllText("_lastguid", clientid);
                Sendbuf2Client(client, Encoding.ASCII.GetBytes("/GETORDERBOOK;"));
                break;

            case "/ORDERBOOK":
                string jsdataOB = ReadData.Split(';')[1]?.ToString();
                _OB = JsonConvert.DeserializeObject <TOrderBook>(jsdataOB);
                break;

            case "/TRADEHISTORYBOOK":
                string jsdataTH = ReadData.Split(';')[1]?.ToString();
                _TRH = JsonConvert.DeserializeObject <TTradeHistory>(jsdataTH);
                break;
            }
            if (exception4send.Length > 0)
            {
                Sendbuf2Client(client, Encoding.ASCII.GetBytes("/ERROR;" + exception4send));
            }
        }
Ejemplo n.º 4
0
 public void RemoveOrder(TOrder O)
 {
     Orders.Remove(O);
     SetTOBChangeState(true);
 }