Trady icon indicating copy to clipboard operation
Trady copied to clipboard

Exception: Could not convert (the whole CSV line) to DateTime.

Open uranio-235 opened this issue 5 years ago • 1 comments

Readme example does not work

System.AggregateException
  HResult=0x80131500
  Mensaje = One or more errors occurred.
  Origen = System.Private.CoreLib
  Seguimiento de la pila:
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at playground.Program.Main(String[] args) in C:\Users\lazaro\source\repos\trading\playground\Program.cs:line 21

Excepción interna 1:
Exception: Could not convert '2019-03-06,172.899994,173.570007,171.270004,172.509995,172.509995,21531700' to DateTime.

Simple line of code

var grafico = new YahooFinanceImporter()
    .ImportAsync("FB", DateTime.Now - TimeSpan.FromDays(60)).Result;

Seem it is not spliting by ","

uranio-235 avatar May 05 '19 18:05 uranio-235

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CsvHelper;
using CsvHelper.Configuration;
using Trady.Core.Infrastructure;
using Trady.Core.Period;

namespace StockApp
{
    public class LocalCsvImporter : IImporter
    {
        public class Candle : IOhlcv, ITick
        {
            public Candle()
            {
            }

            public Candle(DateTimeOffset dateTime, decimal open, decimal high, decimal low, decimal close, decimal volume)
            {
                DateTime = dateTime;
                Open = open;
                High = high;
                Low = low;
                Close = close;
                Volume = volume;
            }

            public DateTimeOffset DateTime { get; set; }

            public decimal Open { get; set; }

            public decimal High { get; set; }

            public decimal Low { get; set; }

            public decimal Close { get; set; }

            public decimal Volume { get; set; }
        }

        public sealed class CandleMap : ClassMap<Candle>
        {
            public CandleMap()
            {
                Map(m => m.DateTime);
                Map(m => m.Open);
                Map(m => m.High);
                Map(m => m.Low);
                Map(m => m.Volume);
                Map(m => m.Close);
            }
        }

        public Task<IReadOnlyList<IOhlcv>> ImportAsync(string symbol, DateTime? startTime = null, DateTime? endTime = null, PeriodOption period = PeriodOption.Daily,
            CancellationToken token = new CancellationToken())
        {
            var files = Directory.GetFiles(Defaults.DownloadDataFolderPath);
            var tickerFile = files.Single(x => x.Contains(symbol));
            {
                using (var reader = new StreamReader(tickerFile))
                using (var csv = new CsvReader(reader))
                {
                    csv.Configuration.RegisterClassMap<CandleMap>();
                    var candles = csv.GetRecords<Candle>().ToList();
                    return Task.FromResult<IReadOnlyList<IOhlcv>>(candles);
                }
            }

        }
    }
}

Check this.

Shuffzord avatar Feb 04 '20 12:02 Shuffzord