Ejemplo n.º 1
0
        public static void RunGetHistoricalTrades(Dictionary <string, string> dict)
        {
            RequireArgs(dict, "exchangeName", "symbol");

            string       exchangeName = dict["exchangeName"];
            IExchangeAPI api          = ExchangeAPI.GetExchangeAPI(exchangeName);
            string       symbol       = dict["symbol"];

            Console.WriteLine("Showing historical trades for exchange {0}...", exchangeName);
            DateTime?startDate = null;
            DateTime?endDate   = null;

            if (dict.ContainsKey("startDate"))
            {
                startDate = DateTime.Parse(dict["startDate"]).ToUniversalTime();
            }
            if (dict.ContainsKey("endDate"))
            {
                endDate = DateTime.Parse(dict["endDate"]).ToUniversalTime();
            }
            api.GetHistoricalTrades((IEnumerable <ExchangeTrade> trades) =>
            {
                foreach (ExchangeTrade trade in trades)
                {
                    Console.WriteLine("Trade at timestamp {0}: {1}/{2}/{3}", trade.Timestamp.ToLocalTime(), trade.Id, trade.Price, trade.Amount);
                }
                return(true);
            }, symbol, startDate, endDate);
        }
 /// <summary>
 /// Export exchange data to csv and then to optimized bin files
 /// </summary>
 /// <param name="api">Exchange api, null to just convert existing csv files</param>
 /// <param name="symbol">Symbol to export</param>
 /// <param name="basePath">Base path to export to, should not contain symbol, symbol will be appended</param>
 /// <param name="sinceDateTime">Start date to begin export at</param>
 /// <param name="callback">Callback if api is not null to notify of progress</param>
 public static void ExportExchangeTrades(IExchangeAPI api, string symbol, string basePath, DateTime sinceDateTime, System.Action <long> callback = null)
 {
     basePath = Path.Combine(basePath, symbol);
     Directory.CreateDirectory(basePath);
     sinceDateTime = sinceDateTime.ToUniversalTime();
     if (api != null)
     {
         long         count     = 0;
         int          lastYear  = -1;
         int          lastMonth = -1;
         StreamWriter writer    = null;
         foreach (ExchangeTrade trade in api.GetHistoricalTrades(symbol, sinceDateTime))
         {
             if (trade.Timestamp.Year != lastYear || trade.Timestamp.Month != lastMonth)
             {
                 if (writer != null)
                 {
                     writer.Close();
                 }
                 lastYear  = trade.Timestamp.Year;
                 lastMonth = trade.Timestamp.Month;
                 writer    = new StreamWriter(basePath + trade.Timestamp.Year + "-" + trade.Timestamp.Month.ToString("00") + ".csv");
             }
             writer.WriteLine("{0},{1},{2}", CryptoUtility.UnixTimestampFromDateTimeSeconds(trade.Timestamp), trade.Price, trade.Amount);
             if (++count % 100 == 0)
             {
                 callback?.Invoke(count);
             }
         }
         writer.Close();
         callback?.Invoke(count);
     }
     TraderFileReader.ConvertCSVFilesToBinFiles(basePath);
 }