Пример #1
0
        public int Run(string[] args)
        {
            Stopwatch w = Stopwatch.StartNew();

            // bear market
            //startDate = new DateTime(2017, 6, 11, 17, 0, 0, DateTimeKind.Utc);
            //endDate = new DateTime(2017, 7, 18, 0, 0, 0, DateTimeKind.Utc);
            startDate = new DateTime(2017, 1, 3, 0, 0, 0, DateTimeKind.Utc);
            endDate   = new DateTime(2017, 1, 7, 0, 0, 0, DateTimeKind.Utc);

            byte[] tradeData = TraderFileReader.GetBytesFromBinFiles(@"../../data/btcusd", startDate, endDate);
            tradeReader = new TradeReaderMemory(tradeData);

            if (csv.Count != 0)
            {
                using (StreamWriter csvWriter = new StreamWriter(@"../../data.csv"))
                {
                    csvWriter.WriteLine("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14}",
                                        "StartCashFlow", "UnitsToBuy", "Interval", "BuyThresholdPercent", "SellThresholdPercent",
                                        "BuyReverseThresholdPercent", "BuyFalseReverseThresholdPercent", "SellReverseThresholdPercent",
                                        "Spend", "Profit", "SpendProfitDiff", "ItemCount", "Buys", "Sells", "CashFlow");
                    csv.Sort((k1, k2) => k2.Key.CompareTo(k1.Key));
                    Console.WriteLine("Max: {0}", csv[0].Value);
                    foreach (var kv in csv)
                    {
                        csvWriter.WriteLine(kv.Value);
                    }
                }
            }

            w.Stop();
            Console.WriteLine("Total time: {0}", w.Elapsed);
            return(0);
        }
        /// <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, 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;
                bool innerCallback(IEnumerable <ExchangeTrade> trades)
                {
                    foreach (ExchangeTrade trade in trades)
                    {
                        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);
                        }
                    }
                    return(true);
                }

                api.GetHistoricalTrades(innerCallback, symbol, sinceDateTime);
                writer.Close();
                callback?.Invoke(count);
            }
            TraderFileReader.ConvertCSVFilesToBinFiles(basePath);
        }