public static void GetStockPriceForTable(string table) { // key: bo5suuvrh5rbvm1sl1t0 https://finnhub.io/dashboard // https://finnhub.io/api/v1/quote?symbol=AAPL&token=bo5suuvrh5rbvm1sl1t0 string mysqlcmnd = "SELECT * FROM money." + table + ";"; string apiKey = "bo5suuvrh5rbvm1sl1t0"; DataTable dt = new DataTable(); var client = new System.Net.WebClient(); try { using (MySqlConnection connection = new MySqlConnection(GlobalVars.conString)) { connection.Open(); using (MySqlCommand myCommand = new MySqlCommand(mysqlcmnd, connection)) { using (MySqlDataAdapter mysqlDa = new MySqlDataAdapter(myCommand)) mysqlDa.Fill(dt); foreach (DataRow row in dt.Rows) { string symbol = row[9].ToString(); //Logger("DEBUG", "Symbol: " + symbol); try { string url = $"https://finnhub.io/api/v1/quote?symbol={symbol}&token={apiKey}"; string responseBody = client.DownloadString(url); FinnhubData StockData = JsonConvert.DeserializeObject <FinnhubData>(responseBody); decimal CurrentOpenPrice = StockData.C; System.Threading.Thread.Sleep(GlobalVars.Delay); UpdateStock(table, symbol, CurrentOpenPrice); Logger("INFO", table + "." + symbol + " " + CurrentOpenPrice); } catch (Exception ex) { Logger("ERROR", symbol + " " + ex.Message); } } } } } catch (Exception ex) { Logger("ERROR", ex.Message); } }
public async void UpdatePrice(string symbol, string valuta) { // key: bo5suuvrh5rbvm1sl1t0 https://finnhub.io/dashboard // https://finnhub.io/api/v1/quote?symbol=AAPL&token=bo5suuvrh5rbvm1sl1t0 string apiKey = "bo5suuvrh5rbvm1sl1t0"; HttpClient client = new HttpClient(); decimal rate = 1; try { string url = $"https://finnhub.io/api/v1/quote?symbol={symbol}&token={apiKey}"; string responseBody = await client.GetStringAsync(url); FinnhubData StockData = JsonConvert.DeserializeObject <FinnhubData>(responseBody); decimal CurrentOpenPrice = StockData.C; if (string.Compare(valuta, "SEK") != 0) { rate = ConvertExchangeRates(valuta); } UpdateInvestment(symbol, CurrentOpenPrice, rate); rate = 1; Logger("DEBUG", symbol + " " + CurrentOpenPrice); } catch (ArgumentNullException e) { Logger("ERROR7", "UpdatePrice function (argumentnullexception). Symbol: " + symbol + " " + e.Message); } catch (Exception ex) { Logger("ERROR8", "UpdatePrice function. Symbol: " + symbol + " " + ex.Message); } }
public static void GetStockPrices(string table) { // key: bo5suuvrh5rbvm1sl1t0 https://finnhub.io/dashboard // https://finnhub.io/api/v1/quote?symbol=AAPL&token=bo5suuvrh5rbvm1sl1t0 string sqlcmnd = "SELECT * FROM money." + table + ";"; string apiKey = "bo5suuvrh5rbvm1sl1t0"; decimal rate = 1; DataTable dt = new DataTable(); var client = new System.Net.WebClient(); //Logger("INFO", "Uppdatera priser för " + table); try { using (SqlConnection connection = new SqlConnection(GlobalVars.SQLconString)) { connection.Open(); using (SqlCommand myCommand = new SqlCommand(sqlcmnd, connection)) { using (SqlDataAdapter mysqlDa = new SqlDataAdapter(myCommand)) mysqlDa.Fill(dt); foreach (DataRow row in dt.Rows) { string symbol = row[10].ToString(); string valuta = row[9].ToString(); // Lite fullösning, men här behöver vi inte kolla upp aktier som har en symbol som slutar på .STO. // Detta tas hand om av GetStockPriceSpecial // Vi passar på att även hoppa över kontanter. if (!symbol.Contains(".STO") && !symbol.Contains("Kontanter")) { try { string url = $"https://finnhub.io/api/v1/quote?symbol={symbol}&token={apiKey}"; string responseBody = client.DownloadString(url); FinnhubData StockData = JsonConvert.DeserializeObject <FinnhubData>(responseBody); decimal CurrentOpenPrice = StockData.C; if (string.Compare(valuta, "SEK") != 0) { rate = ConvertExchangeRates(valuta); } System.Threading.Thread.Sleep(GlobalVars.Delay); UpdateInvestment(table, symbol, CurrentOpenPrice, rate); rate = 1; //Logger("INFO", table + "." + symbol + " " + CurrentOpenPrice); } catch (Exception ex) { Logger("ERROR3", symbol + " " + ex.Message); } } } } } } catch (Exception ex) { Logger("ERROR4", ex.Message); } }