返回 zip 文件夹和文件名时更改 C# 字符
C# Characters changed when returning zip folder and file names
我正在编写一个脚本来循环一个日期范围,检查每个日期是否是有效的交易日,并检查每个日期是否有相应的文件夹和文件名。
Quantconnect/Lean Githib repo for classes
我遇到的问题是我的 File.Exists(dataFile)
方法每次都出错。
原因似乎是存储在 formattedFolderName
中的文件夹名称正在更改其字符。
目标文件夹本身是压缩的,所以我想知道这是否是问题所在?
预期文件路径:C:\Users\richa_000\Desktop\exporter\forex\fxcm\minute\audjpy070401_quote070401_audjpy_minute_quote
实际文件路径:C:\Users\richa_000\Desktop\exporter\forex\fxcm\minute\audjpy070401_quoAe070401_au1jp7_0inuAe_quoAe
我认为它可能是 zip 文件夹,20070401_quote
搞砸了,但我不确定。
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;
}
}
}
}
好的,这就是您从 DateTime 对象构建格式化名称的方式。当您调用 .ToString() 时,它会查找特殊格式字符,因此您不能在任何文字文本中包含任何字符。您的 't' 正在转换为 A,因为 't' 是子午线的格式字符(AM/PM,但单个 't' 只会为您提供 A 或 P),与 'm',它被替换为该 DateTime 对象的分钟数。
试试这个:
var formattedFolderName = validDay.ToString("yyyyMMdd") + "_quote";
var formattedFileName = validDay.ToString($"yyyyMMdd") + securityName + "_minute_quote";
我正在编写一个脚本来循环一个日期范围,检查每个日期是否是有效的交易日,并检查每个日期是否有相应的文件夹和文件名。
Quantconnect/Lean Githib repo for classes
我遇到的问题是我的 File.Exists(dataFile)
方法每次都出错。
原因似乎是存储在 formattedFolderName
中的文件夹名称正在更改其字符。
目标文件夹本身是压缩的,所以我想知道这是否是问题所在?
预期文件路径:C:\Users\richa_000\Desktop\exporter\forex\fxcm\minute\audjpy070401_quote070401_audjpy_minute_quote
实际文件路径:C:\Users\richa_000\Desktop\exporter\forex\fxcm\minute\audjpy070401_quoAe070401_au1jp7_0inuAe_quoAe
我认为它可能是 zip 文件夹,20070401_quote
搞砸了,但我不确定。
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;
}
}
}
}
好的,这就是您从 DateTime 对象构建格式化名称的方式。当您调用 .ToString() 时,它会查找特殊格式字符,因此您不能在任何文字文本中包含任何字符。您的 't' 正在转换为 A,因为 't' 是子午线的格式字符(AM/PM,但单个 't' 只会为您提供 A 或 P),与 'm',它被替换为该 DateTime 对象的分钟数。
试试这个:
var formattedFolderName = validDay.ToString("yyyyMMdd") + "_quote";
var formattedFileName = validDay.ToString($"yyyyMMdd") + securityName + "_minute_quote";