//Load all stock history from Tiingo API public static async void LoadAllData() { // Console.Write("Enter API Key: "); var key = "";// Console.ReadLine(); using (var reader = new StreamReader($@"C:\temp\key.txt")) { while (!reader.EndOfStream) { key = reader.ReadLine(); } } Console.Write("Enter year to load:"); var years = new List <int> { 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 }; var c = 0; List <string> log = new List <string>(); foreach (var year in years) { foreach (var s in symb) { HttpClient _httpClient = new HttpClient(); c++; Console.Write("\r" + c + "/" + symb.Count()); _httpClient.BaseAddress = new Uri("https://api.tiingo.com/tiingo/daily/" + s + "/prices?startDate=" + year + "-1-1&endDate=" + (year + 1) + "-1-1&token=" + key);// (Int32.Parse(year) + 1) + "-1-1&token=" + key); _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var tmp = _httpClient.GetAsync(_httpClient.BaseAddress).Result; string ret = ""; if (tmp.IsSuccessStatusCode) { ret = await tmp.Content.ReadAsStringAsync(); var stk = StockHist.FromJson(ret); foreach (var st in stk) { log.Add( $"{st.Date.DateTime.ToString()}," + $"{s}," + $"{st.Open}," + $"{st.High}," + $"{st.Low}," + $"{st.Close}" ); } } } File.WriteAllLines(Path.Combine("c:\\temp", year + ".csv"), log); } }
public void Run(DateTime startDate, DateTime endDate, double change, double high, double drop, bool pctOnly = false) { _pctOnly = pctOnly; _high = high; _drop = drop; _change = change; try { // set years to sample var years = new List <string> { startDate.Year.ToString() }; if (startDate.Year != endDate.Year) { var end = endDate.Year; while (end != startDate.Year) { years.Add(end.ToString()); end--; } } var count = 0; var added = new List <string>(); foreach (var year in years) { using (var reader = new StreamReader($@"C:\temp\{year}.csv")) { while (!reader.EndOfStream) { StockHist st = new StockHist(); var line = reader.ReadLine(); var values = line.Split(','); st.Date = DateTime.Parse(values[0]); st.Symbol = values[1]; st.Open = Double.Parse(values[2]); st.High = Double.Parse(values[3]); st.Low = Double.Parse(values[4]); st.Close = Double.Parse(values[5]); if (st.Date.Date >= startDate && st.Date.Date <= endDate) { AllStockHistory.Add(st); } } } } var stocksInRange = AllStockHistory.Where(a => a.Open <= _high); foreach (var s in stocksInRange) { if (!added.Contains(s.Symbol)) { Lookup(s.Symbol, count++, stocksInRange.Count()); } added.Add(s.Symbol); } if (_pctOnly) { File.WriteAllLines(Path.Combine("c:\\temp", "pct.csv"), log); } else { Buy(startBal, Buys.OrderBy(b => b.Date).First().Date); foreach (var s in Sells.OrderBy(s => s.SellDate)) { Buy(Sell(s), s.SellDate); } var rep = new List <string>(); foreach (var b in Balances) { rep.Add( $"{b.Date}," + $"{b.Inv}," + $"{b.Int}," + $"{b.Inv + b.Int}" ); } File.WriteAllLines(Path.Combine("c:\\temp", startDate.ToString("ddMMyyyy") + "-" + endDate.ToString("ddMMyyyy") + "-1000-shigh-" + _high + "-Change" + _change + "drop-" + _drop + ".csv"), rep); } } catch (Exception e) { Console.WriteLine(e.Message); } }
public List <double> GetData(StockHist sh, double drp, double chnge) { var otherDrops = new List <double>(); var pchange = (sh.Close - sh.Open) / sh.Open; if (pchange <= drp && Buys.SingleOrDefault(s => s.Date == sh.Date && s.StockAndPrices.Select(x => x.Stock).Contains(sh.Symbol)) == null) { var next = AllStockHistory.Where(s => s.Symbol == sh.Symbol).Where(s => s.Date > sh.Date).OrderBy(s => s.Date).FirstOrDefault(); if (next != null) { var dt = new DateTime(); var cng = (next.High - sh.Close) / next.Close; if (_pctOnly) { log.Add( $"{sh.Date.Date}," + $"{cng}" ); } else { var sell = 0.0; if (cng >= chnge) { dt = next.Date.DateTime; sell = sh.Close + ((sh.Close / 100) * (chnge * 100)); } else { dt = next.Date.DateTime; sell = next.Close; } if (Buys.SingleOrDefault(b => b.Date == sh.Date) == null) { Buys.Add(new DateStructure() { Date = sh.Date, SellDate = dt, StockAndPrices = new List <StockAndPrice>() { new StockAndPrice() { Stock = sh.Symbol, Price = sh.Close } } }); } else { Buys.SingleOrDefault(b => b.Date == sh.Date).StockAndPrices.Add(new StockAndPrice() { Stock = sh.Symbol, Price = sh.Close }); } if (Sells.SingleOrDefault(b => b.BoughtDate == sh.Date) == null) { Sells.Add(new DateStructure() { SellDate = dt, BoughtDate = sh.Date, StockAndPrices = new List <StockAndPrice>() { new StockAndPrice() { Stock = sh.Symbol, Price = sell } } }); } else { Sells.SingleOrDefault(b => b.BoughtDate == sh.Date).StockAndPrices.Add(new StockAndPrice() { Stock = sh.Symbol, Price = sell }); } } } } else { drp += 0.1; while (drp <= _minDrop) { if (pchange <= drp) { otherDrops.Add(drp); } drp += 0.1; } } return(otherDrops); }