/*public void GainersTask()
         * {
         *  var myExchanges = new List<string>() { "Cryptopia", "YoBit", "Livecoin", "HitBTC", "Poloniex", "Bleutrade", "Kucoin", "Bittrex", "Huobi", "Binance", "Bithumb", "Kraken", "Bitfinex", "Okex", "Tux Exchange", "Nanex", "EtherDelta (ForkDelta)", "Waves Decentralized Exchange" };
         *
         *  while (true)
         *  {
         *      Console.WriteLine("\n---Biggest Gainers 1h---");
         *      var gainers1h = BiggestGainers1h();
         *      foreach (var t in gainers1h)
         *      {
         *          var mr = GetMarkets(t.name);
         *          var exchanges = mr.Select(m => m.exchange).Distinct();
         *          if (exchanges.Intersect(myExchanges).Count() > 0)
         *          {
         *              Console.WriteLine(t.ToString());
         *          }
         *      }
         *
         *      Console.WriteLine("\n---Biggest Gainers 24h---");
         *      var gainers24h = BiggestGainers24h();
         *      foreach (var t in gainers24h)
         *      {
         *          var mr = GetMarkets(t.name);
         *          var exchanges = mr.Select(m => m.exchange).Distinct();
         *          if (exchanges.Intersect(myExchanges).Count() > 0)
         *          {
         *              Console.WriteLine(t.ToString());
         *          }
         *      }
         *
         *      Thread.Sleep(60000);
         *  }
         * }*/

        private void DisplayExchangesForGainers()
        {
            var uniqueExchanges = new HashSet <string>();

            // Biggest 1-HOUR gainers from CoinMarketCap
            var gainers1h = m_cmc.GetBiggestGainers1h();

            uniqueExchanges.Clear();
            foreach (var t in gainers1h)
            {
                var mr = CoinMarketCapApi.GetMarkets(t.name);
                mr.Select(m => m.exchange).ToList().ForEach(exch => uniqueExchanges.Add(exch));
            }
            Console.WriteLine("\n---Exchanges for Biggest Gainers 1h---");
            uniqueExchanges.ToList().ForEach(exch => Console.WriteLine(exch));

            // Biggest 24-HOUR gainers from CoinMarketCap
            var gainers24h = m_cmc.GetBiggestGainers24h();

            uniqueExchanges.Clear();
            foreach (var t in gainers24h)
            {
                var mr = CoinMarketCapApi.GetMarkets(t.name);
                mr.Select(m => m.exchange).ToList().ForEach(exch => uniqueExchanges.Add(exch));
            }
            Console.WriteLine("\n---Exchanges for Biggest Gainers 24h---");
            uniqueExchanges.ToList().ForEach(exch => Console.WriteLine(exch));
        }
        // Display biggest gainers from CoinMarketCap
        private void DisplayGainers(int limit = 50)
        {
            var g24 = m_cmc.GetBiggestGainers24h(limit);

            foreach (var t in g24)
            {
                var mr = CoinMarketCapApi.GetMarkets(t.name);
                var marketExchanges = mr.Select(m => m.exchange).Distinct();
                Console.WriteLine("{0,4} {1,-7}  {2,10:0}  1h:{3,6:0.00}%  24h:{4,6:0.00}%  7d:{5,6:0.00}%  {6}", t.rank, t.symbol, t.market_cap_usd, t.percent_change_1h, t.percent_change_24h, t.percent_change_7d, string.Join('|', marketExchanges));
            }
        }
        public void GainersTask(bool display = false)
        {
            var headers = CoinMarketCapTicker.CsvHeaders + ",Exchanges";
            var writer  = GFile.CreateOutputFile("sploders_raw", headers);

            if (display)
            {
                Console.WriteLine(headers);
            }

            while (true)
            {
                var g24 = m_cmc.GetBiggestGainers24h();

                var now = DateTime.Now;
                m_rawGainers24[now] = new Dictionary <string, CoinMarketCapTicker>();
                int count = 0;
                foreach (var t in g24)
                {
                    m_rawGainers24[now][t.symbol] = t;

                    var mr = CoinMarketCapApi.GetMarkets(t.name);
                    var marketExchanges = mr.Select(m => m.exchange).Distinct();
                    //var mr = CoinMarketCapApi.GetMarkets(t.name, removeDuplicates: true);
                    //var marketExchanges = mr.Select(x => x.pair);
                    var exchanges = marketExchanges.Intersect(m_workingExchanges);
                    if (exchanges.Count() > 0)
                    {
                        var exchangesString = string.Join('|', exchanges);
                        var output          = string.Format("{0},{1}", t.ToCsv(), exchangesString);
                        if (display)
                        {
                            Console.WriteLine(output);
                        }
                        writer.WriteLine(output);
                        ++count;
                    }
                }
                writer.FlushAsync();
                var timeString = now.ToString("yyyy-MM-dd HH:mm:ss");
                Console.WriteLine("     {0}  {1} symbols output", timeString, count);

                Thread.Sleep(40000);
            }
        }