public void GetRecentInfo()
        {
            //command to get all recent info from server, notify causes this to run
            this.ListCompanies();
            var     returnVal  = this.GetBuyOrders();
            var     stringjson = parse_server_response(returnVal);
            JObject jsonData   = JObject.Parse(stringjson);

            //this is for all buy orders
            foreach (var company in this.Subject.getCompanies())
            {
                JArray  ja = (JArray)jsonData[company.Name];
                Company cmp;

                cmp = company;
                cmp.clearBuyOrders();
                foreach (JObject jo in ja)
                {
                    cmp.addBuyOrder(Double.Parse(jo["price"].ToString()), Int32.Parse(jo["volume"].ToString()));
                }
            }

            var     returnedSellVal = this.GetSellOrders();
            var     sellStringjson  = parse_server_response(returnedSellVal);
            JObject jsonSellData    = JObject.Parse(sellStringjson);

            //this is for all sell orders
            foreach (var c in this.Subject.getCompanies())
            {
                JArray jsa = (JArray)jsonSellData[c.Name];
                c.clearSellOrders();
                foreach (JObject jo in jsa)
                {
                    c.addSellOrder(Double.Parse(jo["price"].ToString()), Int32.Parse(jo["volume"].ToString()));
                }
            }

            Console.WriteLine("\n ######################### Recent Info has been ran\n");


            Subject.Notify();
        }
Exemplo n.º 2
0
        public void addBuyOrder(Double buy_price, int buy_size)
        {
            if (SellOrders.Count > 0)
            {
                //              var SortedSellOrders = SellOrders.OrderBy(SellOrder => SellOrder.Price).ThenBy(SellOrder => SellOrder.TimeStamp).ToList();
                //              SellOrders = SortedSellOrders;



                BuyOrder sale;
                foreach (SellOrder sell_order in SellOrders)
                {
                    // if we have sell order with the same price do the trasaction and add it into Transaction list
                    if (buy_price >= sell_order.Price)
                    {
                        if (sell_order.Size == buy_size)
                        {
                            // do full transacqion
                            // do not add to buyOrders list
                            // remove sellOrder from the sellOrders list
                            sale = new BuyOrder(buy_price, buy_size);
                            Transactions.Add(sale);
                            SellOrders.Remove(sell_order);
                            lastSale = buy_price;
                            break;
                            //                        market.Notify();
                        }
                        else if (sell_order.Size > buy_size)
                        {
                            // do partial transacqion
                            // do not add to buyOrders list
                            // update sellOrder size to the remaining size in sellOrders list
                            int remainingSize = sell_order.Size - buy_size;
                            sale = new BuyOrder(buy_price, buy_size);
                            Transactions.Add(sale);
                            sell_order.Size = remainingSize;
                            lastSale        = buy_price;
                            break;
                            //                       market.Notify();
                        }
                        else if (sell_order.Size < buy_size)
                        {
                            // do partial transacqion
                            // remove sellOrder from the sellOrders list
                            // add to buyOrders for the remaining size in buyOrders list
                            int remainingSize = buy_size - sell_order.Size;
                            sale = new BuyOrder(buy_price, sell_order.Size);
                            Transactions.Add(sale);
                            SellOrders.Remove(sell_order);

                            //BuyOrder buyOrder = new BuyOrder(buy_price, remainingSize);
                            //BuyOrders.Add(buyOrder);
                            //var SortedBuyOrders = BuyOrders.OrderByDescending(BuyOrder => BuyOrder.Price).ThenBy(BuyOrder => BuyOrder.TimeStamp).ToList();
                            //BuyOrders = SortedBuyOrders;

                            lastSale = buy_price;
                            addBuyOrder(buy_price, remainingSize);


                            break;
                            //                        market.Notify();
                        }
                    }
                    else
                    {
                        BuyOrder buyOrder = new BuyOrder(buy_price, buy_size);
                        BuyOrders.Add(buyOrder);
                        var SortedBuyOrders = BuyOrders.OrderByDescending(BuyOrder => BuyOrder.Price).ThenBy(BuyOrder => BuyOrder.TimeStamp).ToList();
                        BuyOrders = SortedBuyOrders;
                        break;
                    }
                }
            }
            else
            {
                BuyOrder buyOrder = new BuyOrder(buy_price, buy_size);
                BuyOrders.Add(buyOrder);
                var SortedBuyOrders = BuyOrders.OrderByDescending(BuyOrder => BuyOrder.Price).ThenBy(BuyOrder => BuyOrder.TimeStamp).ToList();
                BuyOrders = SortedBuyOrders;
            }
            market.Notify();
        }