Skip to content

Commit d5ef609

Browse files
committed
Address review: add example conversion of MyCustomDataType
* Adjust and properly import a few more namespaces
1 parent 4adeb3a commit d5ef609

4 files changed

Lines changed: 75 additions & 7 deletions

File tree

DataProcessing/VendorDataDownloaderConverter.cs

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515

1616
using QuantConnect.Logging;
1717
using System;
18+
using System.Collections.Generic;
1819
using System.IO;
20+
using System.Linq;
21+
using QuantConnect.DataLibrary;
1922

2023
namespace QuantConnect.DataProcessing
2124
{
@@ -76,7 +79,29 @@ public bool Run()
7679
{
7780
try
7881
{
79-
// Your data downloading/processing code goes here
82+
// Your data downloading/processing code goes here. The lines below
83+
// can be deleted since they are only meant to be an example.
84+
// ================================================================
85+
var underlying = Symbol.Create("SPY", SecurityType.Equity, Market.USA);
86+
var symbol = Symbol.CreateBase(
87+
typeof(MyCustomDataType),
88+
underlying,
89+
Market.USA);
90+
91+
var lines = new[]
92+
{
93+
"20131001,buy",
94+
"20131003,buy",
95+
"20131006,buy",
96+
"20131007,sell",
97+
"20131009,buy",
98+
"20131011,sell"
99+
};
100+
101+
var instances = lines.Select(x => ParseLine(symbol, x));
102+
var csvLines = instances.Select(x => ToCsvLine(x));
103+
104+
SaveContentsToFile(symbol, csvLines);
80105
}
81106
catch (Exception e)
82107
{
@@ -87,6 +112,52 @@ public bool Run()
87112
return true;
88113
}
89114

115+
/// <summary>
116+
/// Example method to parse and create an instance from a line of CSV
117+
/// </summary>
118+
/// <param name="symbol">Symbol</param>
119+
/// <param name="line">Line of raw data</param>
120+
/// <returns>Instance of <see cref="MyCustomDataType"/></returns>
121+
private MyCustomDataType ParseLine(Symbol symbol, string line)
122+
{
123+
var csv = line.Split(',');
124+
return new MyCustomDataType
125+
{
126+
Time = Parse.DateTimeExact(csv[0], "yyyyMMdd"),
127+
Symbol = symbol,
128+
129+
SomeCustomProperty = csv[1]
130+
};
131+
}
132+
133+
/// <summary>
134+
/// Example method to convert an instance to a CSV line
135+
/// </summary>
136+
/// <param name="instance">Custom Data instance</param>
137+
/// <returns>CSV line</returns>
138+
private string ToCsvLine(MyCustomDataType instance)
139+
{
140+
return string.Join(",",
141+
$"{instance.Time:yyyyMMdd}",
142+
instance.SomeCustomProperty);
143+
}
144+
145+
/// <summary>
146+
/// Example method to save CSV lines to disk
147+
/// </summary>
148+
/// <param name="symbol">Symbol of the data</param>
149+
/// <param name="csvLines">CSV lines to write</param>
150+
private void SaveContentsToFile(Symbol symbol, IEnumerable<string> csvLines)
151+
{
152+
var ticker = symbol.Value.ToLowerInvariant();
153+
154+
var tempPath = new FileInfo(Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}-{ticker}.csv"));
155+
var finalPath = Path.Combine(_destinationDirectory, $"{ticker}.csv");
156+
157+
File.WriteAllLines(tempPath.FullName, csvLines);
158+
tempPath.MoveTo(finalPath, true);
159+
}
160+
90161
/// <summary>
91162
/// If you need to shut down things like database connections, threads, or other
92163
/// resources that require manual shutdown, do it here. This will be called after

Tests/CustomDataAlgorithm.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@
1414
*
1515
*/
1616

17-
using DataLibrary;
18-
using QuantConnect;
1917
using QuantConnect.Data;
2018
using QuantConnect.Util;
2119
using QuantConnect.Orders;
2220
using QuantConnect.Algorithm;
2321

24-
namespace Tests
22+
namespace QuantConnect.DataLibrary.Tests
2523
{
2624
/// <summary>
2725
/// Example algorithm using the custom data type as a source of alpha

Tests/MyCustomDataTypeTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818
using ProtoBuf;
1919
using System.IO;
2020
using System.Linq;
21-
using DataLibrary;
22-
using QuantConnect;
2321
using ProtoBuf.Meta;
2422
using Newtonsoft.Json;
2523
using NUnit.Framework;
2624
using QuantConnect.Data;
2725

28-
namespace Tests
26+
namespace QuantConnect.DataLibrary.Tests
2927
{
3028
[TestFixture]
3129
public class MyCustomDataTypeTests

Tests/Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>net5.0</TargetFramework>
4+
<RootNamespace>QuantConnect.DataLibrary.Tests</RootNamespace>
45
</PropertyGroup>
56
<ItemGroup>
67
<PackageReference Include="protobuf-net" Version="3.0.29" />

0 commit comments

Comments
 (0)