Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            try
            {
                Console.Write("Enter order type (SELL, BUY): ");
                string orderTypeString = Console.ReadLine();

                if (!Enum.TryParse(orderTypeString, true, out TypeEnum orderType))
                {
                    throw new ArgumentException("Invalid order type");
                }

                Console.Write("Enter BTC order amount: ");
                string orderAmountString = Console.ReadLine();

                if (!decimal.TryParse(orderAmountString, out decimal orderAmount))
                {
                    throw new ArgumentException("Invalid order amount");
                }

                if (Math.Truncate(100000000 * orderAmount) / 100000000 != orderAmount)
                {
                    throw new ArgumentException("Invalid order amount precision");
                }

                Console.Write("Enter book file path: ");
                string orderBookFilePath = Console.ReadLine();

                if (string.IsNullOrEmpty(orderBookFilePath) || !File.Exists(orderBookFilePath))
                {
                    throw new ArgumentException("Order book not found");
                }

                Console.WriteLine();

                Console.WriteLine("**** Processing started ****");

                MetaExchangeLogic metaExchangeLogic = MetaExchangeLogic.ReadData(orderBookFilePath);
                ICollection <(BookOrder BookOrder, decimal OrderAmount)> orders = metaExchangeLogic.Process(orderType, orderAmount);

                if (orders.Count > 0)
                {
                    foreach ((BookOrder bookOrder, decimal tradeOrderAmount) in orders)
                    {
                        Console.WriteLine($"Exchange '{bookOrder.BookId}' order for '{bookOrder.Id}' at amount '{tradeOrderAmount:0.00000000} BTC' and price '{bookOrder.Price:0.00} EUR'");
                    }
                }
                else
                {
                    Console.WriteLine($"Order of '{orderAmount:0.00000000} BTC' not possible'");
                }

                Console.WriteLine("**** Processing finished ****");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 2
0
        public static void Process(string file, TypeEnum type, decimal amount, decimal orderCost, decimal remainingAmount)
        {
            string assemblyPath = Assembly.GetExecutingAssembly().Location.Replace($"{nameof(MetaExchangeTest)}.dll", "");
            string filePath     = Path.Combine(assemblyPath, file);

            MetaExchangeLogic metaExchangeLogic = MetaExchangeLogic.ReadData(filePath);

            ICollection <(BookOrder BookOrder, decimal TradeOrderAmount)> exchangeOrders = metaExchangeLogic.Process(type, amount);

            Assert.Equal(orderCost, Math.Round(exchangeOrders.Sum(i => i.TradeOrderAmount * i.BookOrder.Price), 2));
            Assert.Equal(amount - remainingAmount, exchangeOrders.Sum(i => i.TradeOrderAmount));
        }