Example #1
0
        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());
        }
Example #2
0
        public static async Task <List <Share> > getSharesInfo(string strSymbol, int nNumOfDays, DateTime dtFirstDate)
        {
            List <Share> ret   = new List <Share>();
            int          nTrys = 0;

            string[] days;

            while (ret.Count == 0 && nTrys < 3)
            {
                dtFirstDate = new DateTime(dtFirstDate.Year, dtFirstDate.Month, dtFirstDate.Day);

                DateTime date = new DateTime(2100, 1, 1);
                string   html = await MyRequersts.getHTTP(string.Format("https://finance.yahoo.com/quote/{0}/history?", strSymbol));

                DateTime start     = new DateTime(1970, 1, 1, 0, 0, 0);
                string   strToFind = "\"HistoricalPriceStore\":{\"prices\":";
                string   a         = "{\"date\":";
                string   b         = "\"open\":";
                string   c         = "\"close\":";
                string   d         = "\"volume\":";
                string   e         = "\"high\":";
                string   f         = "\"low\":";

                int nStartIndex = html.IndexOf(strToFind);
                html = html.Substring(strToFind.Length + nStartIndex, html.Length - nStartIndex - strToFind.Length);
                int nEndIndex = html.IndexOf(']');
                html = html.Substring(0, nEndIndex);
                days = html.Split('}');

                for (int nDayIndex = 0; nDayIndex < days.Length; nDayIndex++)
                {
                    string[] values = null;

                    try
                    {
                        values = days[nDayIndex].Remove(0, 1).Split(',');
                        long lDate = long.Parse(values[0].Remove(0, a.Length));
                        date = start.AddMilliseconds(lDate * 1000);
                        date = new DateTime(date.Year, date.Month, date.Day);
                    }
                    catch { };

                    if (date <= dtFirstDate)
                    {
                        for (int nIndex = 0; (nIndex < nNumOfDays) && (nIndex + nDayIndex < days.Length - 1); nIndex++)
                        {
                            try
                            {
                                values = days[nIndex + nDayIndex].Remove(0, 1).Split(',');

                                long lDate = long.Parse(values[0].Remove(0, a.Length));
                                date = start.AddMilliseconds(lDate * 1000);
                                date = new DateTime(date.Year, date.Month, date.Day);
                                double dOpen   = double.Parse(values[1].Remove(0, b.Length));
                                double dHigh   = double.Parse(values[2].Remove(0, e.Length));
                                double dLow    = double.Parse(values[3].Remove(0, f.Length));
                                double dClose  = double.Parse(values[4].Remove(0, c.Length));
                                long   lVolume = long.Parse(values[5].Remove(0, d.Length));


                                Share s = new Share();
                                s.date   = date;
                                s.close  = new Price(dClose, -1, -1);
                                s.high   = new Price(dHigh, -1, -1);
                                s.low    = new Price(dLow, -1, -1);
                                s.open   = new Price(dOpen, -1, -1);
                                s.volume = lVolume;
                                s.symbol = strSymbol;
                                ret.Add(s);
                            }
                            catch (Exception err)
                            {
                                if (err.Message.Contains("format"))
                                {
                                    nNumOfDays++;
                                }
                                else
                                {
                                    int sn = 3;
                                }
                            };
                        }

                        break;
                    }
                }

                nTrys++;
                if (nTrys == 3 && ret.Count == 0)
                {
                    Console.Write(days.Length + " Lines for :    ");
                }
            }

            return(ret);
        }