public static List <Share> getTodayInfo(ConcurrentDictionary <String, Data> symbols) { int nThreads = 0; ConcurrentBag <Share> lst = new ConcurrentBag <Share>(); DateTime today = new DateTime(TODAY.Year, TODAY.Month, TODAY.Day); foreach (string strCurrSymbol in symbols.Keys) { if (nThreads == Consts.NUM_OF_THREADS) { while (nThreads > 0) { Thread.Sleep(100); } } Interlocked.Increment(ref nThreads); ThreadPool.QueueUserWorkItem(async(stateInfo) => { //new Thread(() => { try { List <Share> towDays = await Share.getSharesInfo(strCurrSymbol, 2, today); Share s = towDays[0]; s.open = towDays[1].close; s.windowReturn = symbols[strCurrSymbol].windowReturn; s.direction = symbols[strCurrSymbol].direction; if (s.date.CompareTo(today) == 0) { lst.Add(s); } } catch (Exception e) { Console.WriteLine(strCurrSymbol + " - FuckingProblam! \n" + e); } finally { Interlocked.Decrement(ref nThreads); } });//.Start(); } while (nThreads > 0) { Thread.Sleep(1000); } return(lst.ToList <Share>()); }
public async Task <Object> predictTomorrowSymbols(DateTime TODAY) { int nThreads = 0; ConcurrentDictionary <string, int> lstForward = new ConcurrentDictionary <string, int>(); DateTime dtRef = new DateTime(1970, 1, 1, 0, 0, 0); DateTime dtTomorrow = new DateTime( TODAY.AddDays(1).Year, TODAY.AddDays(1).Month, TODAY.AddDays(1).Day, 0, 0, 0); try { string response = await MyRequersts.getHTTP(string.Format("https://www.zacks.com/includes/classes/z2_class_calendarfunctions_data.php?calltype=eventscal&date={0}&type=1", ((dtTomorrow.Ticks - dtRef.Ticks) / 10000000 + 22000))); //1509685200 1509598800 string[] lines = response.Split('['); //Console.WriteLine("EARNINGS IN ZACKS count " + (lines.Length - 2)); foreach (var currShareLine in lines) { int n = currShareLine.IndexOf("alt=\\\""); if (n != -1) { int nSymbolIndex = n + "alt=\\\"".Length; int nEndSymbolIndex = currShareLine.IndexOf(" ", nSymbolIndex); string symbol = currShareLine.Substring(nSymbolIndex, nEndSymbolIndex - nSymbolIndex); int nOffset = currShareLine.Contains("\"amc\"") ? 1 : (currShareLine.Contains("\"bmo\"") ? 0 : 0); int nStart = currShareLine.IndexOf("<div class"); #region estimate double dEstimate = -999; if (nStart != -1) { string[] split = currShareLine.Substring(0, nStart).Split(','); int i; int offset = 0; for (i = 0; i < split.Length - 2; i++) { offset += split[i].Replace(" ", string.Empty).StartsWith("\"") ? 0 : 1; } string strEstimate = split[4 + offset].Remove(0, 2); strEstimate = strEstimate.Remove(strEstimate.Length - 1, 1); double.TryParse(strEstimate, out dEstimate); } #endregion if (nThreads == Consts.NUM_OF_THREADS) { while (nThreads > 0) { Thread.Sleep(100); } } Interlocked.Increment(ref nThreads); ThreadPool.QueueUserWorkItem(async(stateInfo) => { //new Thread(() => { // Set prediction List <Share> info = null; List <Share> window = new List <Share>(); List <Share> volumeWindow = new List <Share>(); List <Share> pre = new List <Share>(); try { info = await Share.getSharesInfo(symbol, Consts.WITH_VOLUME_WINDOW_INC_CHECK ? Consts.PRE_PRE_WINDOW : Consts.WINDOW, new DateTime(TODAY.Year, TODAY.Month, TODAY.Day)); for (int nWindowIndex = 0; nWindowIndex < Consts.WINDOW; nWindowIndex++) { window.Add(info[nWindowIndex]); } if (Consts.WITH_VOLUME_WINDOW_INC_CHECK) { for (int nWindowIndex = 2; nWindowIndex < Consts.PRE_WINDOW; nWindowIndex++) { volumeWindow.Add(info[nWindowIndex]); } for (int nWindowIndex = Consts.PRE_WINDOW; nWindowIndex < Consts.PRE_PRE_WINDOW; nWindowIndex++) { pre.Add(info[nWindowIndex]); } } double windowReturn = window[0].close.price / window[window.Count - 1].open.price; int windowDirection = windowReturn >= 1 ? 1 : -1; double avgVolume = pre.Count == 0 ? 0 : pre.Average(s => s.volume); bool bAboveAVGVolume = !Consts.WITH_VOLUME_WINDOW_INC_CHECK || (volumeWindow.Average(S => S.volume) > avgVolume * Consts.K_VOLUME_INC); if (nOffset == 0) { if (window[0].volume >= Consts.MINIMUM_VOLUME && bAboveAVGVolume) { symbols.TryAdd(symbol, new Data(symbol, (-1) * windowDirection, windowReturn, window[0].close.price)); } } else { lstForward.TryAdd(symbol, 0); } } catch (Exception e) { if (e.Message.Contains("out of range")) { Console.WriteLine(symbol + " - " + (info != null ? info.Count : 0)); } else { Console.WriteLine(symbol + " - " + e); int ns = 3; } } finally { Interlocked.Decrement(ref nThreads); } });//.Start(); } } while (nThreads > 0) { Thread.Sleep(1000); } } catch (Exception e) { symbols.Clear(); } if (lstForward.Count != 0) { DateTime dtFarward = dtTomorrow.AddDays(1); if (dtFarward.DayOfWeek == DayOfWeek.Saturday) { dtFarward = dtFarward.AddDays(1); } if (dtFarward.DayOfWeek == DayOfWeek.Sunday) { dtFarward = dtFarward.AddDays(1); } forward.Add(dtTomorrow.AddDays(1), lstForward); } if (forward.ContainsKey(dtTomorrow)) { foreach (string symbol in forward[dtTomorrow].Keys) { if (nThreads == Consts.NUM_OF_THREADS) { while (nThreads > 0) { Thread.Sleep(100); } } Interlocked.Increment(ref nThreads); ThreadPool.QueueUserWorkItem(async(stateInfo) => { //new Thread(() => { List <Share> info = null; List <Share> window = new List <Share>(); List <Share> volumeWindow = new List <Share>(); List <Share> pre = new List <Share>(); try { info = await Share.getSharesInfo(symbol, Consts.WITH_VOLUME_WINDOW_INC_CHECK ? Consts.PRE_PRE_WINDOW : Consts.WINDOW, new DateTime(TODAY.Year, TODAY.Month, TODAY.Day)); for (int nWindowIndex = 0; nWindowIndex < Consts.WINDOW; nWindowIndex++) { window.Add(info[nWindowIndex]); } if (Consts.WITH_VOLUME_WINDOW_INC_CHECK) { for (int nWindowIndex = 2; nWindowIndex < Consts.PRE_WINDOW; nWindowIndex++) { volumeWindow.Add(info[nWindowIndex]); } for (int nWindowIndex = Consts.PRE_WINDOW; nWindowIndex < Consts.PRE_PRE_WINDOW; nWindowIndex++) { pre.Add(info[nWindowIndex]); } } double windowReturn = window[0].close.price / window[window.Count - 1].open.price; int windowDirection = windowReturn >= 1 ? 1 : -1; double avgVolume = pre.Count == 0 ? 0 : pre.Average(s => s.volume); bool bAboveAVGVolume = !Consts.WITH_VOLUME_WINDOW_INC_CHECK || (volumeWindow.Average(S => S.volume) > avgVolume * Consts.K_VOLUME_INC); if (window[0].volume >= Consts.MINIMUM_VOLUME && bAboveAVGVolume) { symbols.TryAdd(symbol, new Data(symbol, (-1) * windowDirection, windowReturn, window[0].close.price)); } } catch (Exception e) { if (e.Message.Contains("out of range")) { Console.WriteLine(symbol + " - " + (info != null ? info.Count : 0)); } else { Console.WriteLine(symbol + " - " + e); int ns = 3; } } finally { Interlocked.Decrement(ref nThreads); } });//.Start(); } while (nThreads > 0) { Thread.Sleep(1000); } } return(new Object()); }