Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
uranio-235 opened this issue May 5, 2019 · 1 comment
Open

Comments

@uranio-235
Copy link

uranio-235 commented May 5, 2019

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 ","

@Shuffzord
Copy link

Shuffzord commented Feb 4, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants