3therk1ll
3therk1ll

Reputation: 2421

C# Characters changed when returning zip folder and file names

I am writing a script to loop over a range of dates, check whether each date is a valid trading day and check there is a corresponding folder and file name for each date.

Quantconnect/Lean Githib repo for classes

The issue I am having is that my File.Exists(dataFile) method is coming up false each time. Reason it is seems to be that the folder names that are being stored in formattedFolderName are having their characters changed.

The target folders themselves are zipped, so I wonder if that is the issue maybe?

Intended file path: C:\Users\richa_000\Desktop\exporter\forex\fxcm\minute\audjpy\20070401_quote\20070401_audjpy_minute_quote

Actual file path: C:\Users\richa_000\Desktop\exporter\forex\fxcm\minute\audjpy\20070401_quoAe\20070401_au1jp7_0inuAe_quoAe

I think it could be zip folder, 20070401_quote messing with this, but I'm not sure.

Program.cs

using System;
using System.IO;
using QuantConnect;
using QuantConnect.Securities;
using QuantConnect.Securities.Forex;

namespace TradingDaysFileChecker
{
    class Program
    {
        static void Main(string[] args)
        {
            var startDate = new DateTime(2007, 04, 01);
            var endDate = new DateTime(2016, 07, 25);
            var dataFilePath = @"C:\Users\richa_000\Desktop\exporter\forex\fxcm\minute\";

            var securityType = SecurityType.Forex;
            var ticker = TickType.Trade;
            var marketHoursDatabase = MarketHoursDatabase.FromDataFolder();
            var market = Market.FXCM;
            var symbol = Symbol.Create(ticker.ToString(), securityType, market);
            var marketHoursDbEntry = marketHoursDatabase.GetEntry(symbol.ID.Market, symbol.Value, symbol.ID.SecurityType);

            var exchange = new ForexExchange(marketHoursDbEntry.ExchangeHours);
            var validTradingDays = new DateHandler(startDate, endDate, dataFilePath).IterateOverDateRange(exchange);

            var forexSecuritiesFolders = Directory.GetDirectories(dataFilePath);

                foreach (var validDay in validTradingDays)
                {
                    foreach (var forexSecurity in forexSecuritiesFolders)
                    {
                        var securityName = new DirectoryInfo(forexSecurity).Name.ToString();
                        var formattedFolderName = validDay.ToString("yyyyMMdd_quote");
                        var formattedFileName = validDay.ToString($"yyyyMMdd_{securityName}_minute_quote");

                        var dataFile = dataFilePath + securityName + @"\" + formattedFolderName + @"\" + formattedFileName;

                        if (!File.Exists(dataFile))
                        {
                            Console.WriteLine(dataFile);
                            Console.ReadLine(); // Stopping after the first run while troubleshooting
                        }
                    }
                }
                Console.ReadLine();
        }
    }
}

DateHandler.cs

using System;
using System.Collections.Generic;
using QuantConnect.Securities.Forex;

namespace TradingDaysFileChecker
{
    public class DateHandler
    {
        private DateTime _startDate;
        private DateTime _endDate;

        public DateHandler(DateTime startDate, DateTime endDate, string filePath)
        {
            _startDate = startDate;
            _endDate = endDate;
        }

        public IEnumerable<DateTime> IterateOverDateRange(ForexExchange exchange)
        {
            for (var day = _startDate.Date; day.Date <= _endDate.Date; day = day.AddDays(1))
                if (exchange.IsOpenDuringBar(day.Date, day.Date.AddDays(1), false))
                {
                    yield return day;
                }
        }
    }
}

Upvotes: 0

Views: 95

Answers (1)

Eric Walker
Eric Walker

Reputation: 1052

Ok, it's how you're building your formatted names from the DateTime objects. When you call .ToString() it looks for special formatting characters, so you can't include any in any literal text. Your 't' is getting converted to A because 't' is the formatting character for meridian (AM/PM, but a single 't' just gives you A or P) and same thing with 'm', it's being replaced with a single digit of the minutes of that DateTime object.

Try this:

                var formattedFolderName = validDay.ToString("yyyyMMdd") + "_quote";
                var formattedFileName = validDay.ToString($"yyyyMMdd") + securityName + "_minute_quote";

Upvotes: 1

Related Questions