Импорт данных из csv файла
C#
Несколько вариантов импорта внешних данных из файла csv на примере сценария
1)
Код
using System;
using System.IO;
using OpenQuant.API;
public class MyScript : Script
{
public override void Run()
{
const string dataDir = @"D:\Data\CSV\";
long dailyBarSize = 86400;
string[] filenames = new string[]
{
"AAPL.csv",
"CSCO.csv",
"MSFT.csv"
};
// CSV data files have invariant date/number format
.Globalization.CultureInfo culture = System.Globalization.CultureInfo.InvariantCulture;
System
foreach (string filename in filenames)
{
.WriteLine();
Console
string path = dataDir + filename;
// check file exists
if (!File.Exists(path))
{
.WriteLine(string.Format("File {0} does not exists.", path));
Console
continue;
}
.WriteLine(string.Format("Processing file {0} ...", path));
Console
// get instrument
string symbol = filename.Substring(0, filename.IndexOf('.'));
= InstrumentManager.Instruments[symbol];
Instrument instrument
if (instrument == null)
{
.WriteLine(string.Format("Instrument {0} does not exist.", symbol));
Console
continue;
}
// read file and parse data
= new StreamReader(path);
StreamReader reader
.ReadLine(); // skip CSV header
reader
string line = null;
while ((line = reader.ReadLine()) != null)
{
string[] items = line.Split(',');
// parse data
= DateTime.ParseExact(items[0], "yyyy-M-d", culture);
DateTime date
double high = double.Parse(items[1], culture);
double low = double.Parse(items[2], culture);
double open = double.Parse(items[3], culture);
double close = double.Parse(items[4], culture);
long volume = long.Parse(items[5], culture);
// add daily bar
.Add(
DataManager,
instrument,
date,
open,
high,
low,
close,
volume);
dailyBarSize}
.Close();
reader
//
.WriteLine(string.Format("CSV data for {0} was successfully imported.", instrument.Symbol));
Console}
}
}
2)
Код
using System;
using System.IO;
using OpenQuant.API;
public class MyScript : Script
{
public override void Run()
{
const string dataDir = @"C:\ua\Files\OQ_Import\";
long dailyBarSize = 86400;
//Lookup Directory & File Info
= new DirectoryInfo(@"C:\ua\Files\OQ_Import\");
DirectoryInfo directory_Info [] file_Info = directory_Info.GetFiles("*.csv");
FileInfo
// CSV data files have invariant date/number format
.Globalization.CultureInfo culture = System.Globalization.CultureInfo.InvariantCulture;
System
foreach (FileInfo file_info in file_Info)
{
.WriteLine(file_info.Name.Substring(0, file_info.Name.IndexOf('.')));
Console
.WriteLine();
Console
string filename = file_info.Name.Substring(0, file_info.Name.IndexOf('.'));
string path = dataDir + filename + ".csv";
// check if file exists
if (!File.Exists(path))
{
.WriteLine(string.Format("File {0} does not exists.", path));
Console
continue;
}
.WriteLine(string.Format("Processing file {0} ...", path));
Console
// get instrument
string symbol = filename; //.Substring(0, filename.IndexOf('.'));
= InstrumentManager.Instruments[symbol];
Instrument instrument
if (instrument == null)
{
.WriteLine(string.Format("Instrument {0} does not exist.", symbol));
Console
continue;
}
// read file and parse data
= new StreamReader(path);
StreamReader reader
.ReadLine(); // skip CSV header
reader
string line = null;
while ((line = reader.ReadLine()) != null)
{
string[] items = line.Split(',');
// parse data
= DateTime.ParseExact(items[0], "yyyyMMdd", culture);
DateTime date
double open = double.Parse(items[3], culture);
double high = double.Parse(items[4], culture);
double low = double.Parse(items[5], culture);
double close = double.Parse(items[6], culture);
long volume = long.Parse(items[7], culture);
// add daily bar
.Add(
DataManager,
instrument,
date,
open,
high,
low,
close,
volume);
dailyBarSize}
.Close();
reader
//
.WriteLine(string.Format("CSV data for {0} was successfully imported.", instrument.Symbol));
Console}
}
}