public async Task RefreshCandleData(List <string> coinsToRefresh, Action <string> callback) { List <string> writtenFiles = new List <string>(); foreach (var coinToBuy in coinsToRefresh) { var startDate = DateTime.UtcNow.AddHours(-500); var jsonPath = GetJsonFilePath(coinToBuy); // Delete an existing file. if (File.Exists(jsonPath)) { File.Delete(jsonPath); } var i = 2; DateTime endDate; var totalCandles = new List <Candle>(); var candles = await _api.GetTickerHistory(coinToBuy, Period.Hour, startDate); totalCandles.AddRange(candles); // Get these in batches of 500 because they're limited in the API. while (candles.Count != 0) { endDate = startDate; startDate = DateTime.UtcNow.AddHours(-500 * i); candles = await _api.GetTickerHistory(coinToBuy, Period.Hour, startDate, endDate); i += 1; totalCandles.AddRange(candles); } // Add the last bit in... totalCandles.AddRange(candles); totalCandles = totalCandles.OrderBy(x => x.Timestamp).ToList(); // Write all the text. File.WriteAllText(jsonPath, JsonConvert.SerializeObject(totalCandles)); writtenFiles.Add(jsonPath); callback($"Refreshed data for {coinToBuy}..."); } // Delete everything that's not refreshed foreach (FileInfo fi in new DirectoryInfo(GetDataDirectory()).EnumerateFiles()) { if (!writtenFiles.Contains(fi.FullName)) { File.Delete(fi.FullName); } } }
public async Task RefreshCandleData(List <string> coinsToRefresh, Action <string> callback, Period period) { List <string> writtenFiles = new List <string>(); foreach (var coinToBuy in coinsToRefresh) { var jsonPath = GetJsonFilePath(coinToBuy); // Delete an existing file. if (File.Exists(jsonPath)) { File.Delete(jsonPath); } var endDate = DateTime.UtcNow; var startDate = DateTime.UtcNow.AddMinutes(-15); var totalCandles = new List <Candle>(); var candles = await _api.GetTickerHistory(coinToBuy, period, startDate, endDate); // Add the last bit in... totalCandles.AddRange(candles); totalCandles = totalCandles.OrderBy(x => x.Timestamp).ToList(); // Write all the text. File.WriteAllText(jsonPath, JsonConvert.SerializeObject(totalCandles)); writtenFiles.Add(jsonPath); callback($"{DateTime.UtcNow}: Refreshed data for {coinToBuy}..."); } // Delete everything that's not refreshed foreach (FileInfo fi in new DirectoryInfo(GetDataDirectory()).EnumerateFiles()) { if (!writtenFiles.Contains(fi.FullName)) { File.Delete(fi.FullName); } } }