/// <summary> /// Gathers the last 10 days of price history for a symbol and saves it to a json file /// </summary> /// <param name="client">HttpClient with OAuth Header</param> /// <param name="apiKey">TDAmeritrade API Application Key</param> /// <param name="symbol">Market (Stock) Symbol Case Sensitive</param> /// <param name="historyPath">Program Data Path (Configurable)</param> /// <returns>The new combined file path</returns> public static string Gather10daysByTheMinute(HttpClient client, string apiKey, string symbol, string historyPath) { //Get Data var results = TD_API_Interface.API_Calls.PriceHistory.getPriceHistory(client, apiKey, symbol, "day", "10", "minute", "1"); var contents = results.Content.ReadAsStringAsync().Result; dynamic data = JsonConvert.DeserializeObject(contents); dynamic candlesData = data.candles; if (candlesData.Count > 0) { //Check save location string storageFolderPath = getSymbolsPriceHistoryPath(historyPath, symbol, "ByMinute"); ReadWriteJSONToDisk.testCreateDirectory(storageFolderPath); //Gather Start and End Date string quoteFileName = getFileNameForPriceHistory(candlesData, true); string saveFilePath = $"{storageFolderPath}\\{quoteFileName}"; //save File ReadWriteJSONToDisk.writeDataAsJSON(saveFilePath, candlesData); return(saveFilePath); } else { return("No data returned"); } }
/// <summary> /// This gathers the last Ten Days of a Symbol Price History by minute and combines it with previously stored data. /// It rights the combined results to the TradeData folder /// </summary> /// <param name="client">HttpClient with OAuth Header</param> /// <param name="apiKey">TDAmeritrade API Application Key</param> /// <param name="symbol">Market (Stock) Symbol Case Sensitive</param> /// <param name="historyPath">Program Data Path (Configurable)</param> /// <param name="deleteAllFilesOtherThenNewAndSourceofTruth">If true after combining all the data files /// attempt to delete all the files except for the previously source of true and the newly combined price history. /// Unfortunately if there are not new updates to the price history it deletes all previous sources of truth</param> /// <returns>The new combined file path</returns> public static string UpdateStockByMinuteHistoryFile(HttpClient client, string apiKey, string symbol, string historyPath, bool deleteAllFilesOtherThenNewAndSourceofTruth = false) { //Get Latest by Minute data string newStorageFile = Gather10daysByTheMinute(client, apiKey, symbol, historyPath); //Get Files to Combine string storageFolderPath = getSymbolsPriceHistoryPath(historyPath, symbol, "ByMinute"); List <QuoteFile> priceByMinuteFiles = ReadWriteJSONToDisk.getQuotesFileListFromDirectory(storageFolderPath); //Combine Files List <Quote> NewQuoteList = new List <Quote>(); foreach (QuoteFile file in priceByMinuteFiles) { List <Quote> filesQuotes = ReadWriteJSONToDisk.getQuotesFromJSON(file.path); NewQuoteList = CombineQuoteLists(NewQuoteList, filesQuotes); } //write quoteList string quoteFileName = getFileNameForPriceHistory(NewQuoteList, true); string saveFilePath = $"{storageFolderPath}\\{quoteFileName}"; //save File ReadWriteJSONToDisk.writeDataAsJSON(saveFilePath, NewQuoteList); //delete old files if (deleteAllFilesOtherThenNewAndSourceofTruth) { QuoteFile oldSourceOfTruth = ChooseSourceOfTruthFile(priceByMinuteFiles); List <QuoteFile> filesToDelete = priceByMinuteFiles.Where(q => q.info.Name != oldSourceOfTruth.info.Name).ToList(); int filesDeleted = ReadWriteJSONToDisk.deleteFiles(filesToDelete); } //return new file path return(saveFilePath); }