public async Task HandleOrders() { var stocks = new List <StockData>(); var temp = new decimal(0); var index = 0; var order = await _tradingClient.LastOrderStatusOrDefault(_stock); if (order != null && order.Buy) { _buyingPrice = order.Price; } var position = await _tradingClient.GetCurrentPositionOrDefault(_stock); if (position != null) { _quantity = position.Quantity; _buyingPrice = position.BuyingPrice; } while (true) { var stockData = _stockDataReader.ReadStockValue(); var value = Convert.ToDecimal(stockData.Value); if (index > 0) { var prevValue = Convert.ToDecimal(stocks[index - 1].Value); var trend = value > prevValue; if (trend) { temp = 0; if (_buyingPrice != 0 && value / _buyingPrice >= _sellingValue) { _log.Information($"Selling {_quantity} {_stock} share(s). Price: {value}..."); var results = await _tradingClient.Sell(_stock, _quantity, value); if (results) { _log.Information($"SOLD {_quantity} {_stock} share(s). Price: {value}"); } else { _log.Information( $"FAILED TO SELL {_quantity} {_stock} share(s). Price: {value}"); } _profit = _profit * (value / _buyingPrice); try { System.IO.File.WriteAllText($"{AppConstants.FilePath}profit.txt", _profit.ToString(CultureInfo.InvariantCulture)); } catch (Exception e) { Console.WriteLine(e.Message); } _buyingPrice = 0; } } else { temp = new[] { temp, value, prevValue }.Max(); if (_buyingPrice == 0 && value / temp <= _buyingValue) { _log.Information($"Buying {_quantity} {_stock} share(s). Price: {_buyingPrice}."); _buyingPrice = value; var results = await _tradingClient.Buy(_stock, _quantity, _buyingPrice); if (results) { _log.Information( $"BOUGHT {_quantity} {_stock} share(s). Price: {_buyingPrice}. results is {results}."); } else { _log.Information( $"FAILED TO BUY {_quantity} {_stock} share(s). Price: {_buyingPrice}."); } } } } stocks.Add(stockData); index++; } _stockDataReader.Dispose(); }