Skip to content

Instantly share code, notes, and snippets.

@arakis
Created May 2, 2021 15:13
Show Gist options
  • Save arakis/2120bd826066a440142a3a20649ff4f9 to your computer and use it in GitHub Desktop.
Save arakis/2120bd826066a440142a3a20649ff4f9 to your computer and use it in GitHub Desktop.
Market Data Reader for QuantConnect.Lean
// It seems there's no easy way to extract the Stock Data form the Data Directory. Here's a small helper (only tested with SPY).
// The Zip-Files have already to be existent!
public class LeanReaderFactory
{
private string DataDir;
public LeanReaderFactory(string dataDir)
{
DataDir = dataDir;
}
private IEnumerable<DateTime> TradeableDays(DateTime start, DateTime end)
{
var d = start;
while (d <= end)
{
yield return d;
d = d.AddDays(1);
}
}
public SubscriptionDataReader CreateReader(DateTime start, DateTime end, string symbol, string market, Resolution resolution)
{
QuantConnect.Configuration.Config.Set("data-folder", DataDir);
Globals.Reset();
var mapFileProvider = new LocalDiskMapFileProvider();
var mapFileResolver = new MapFileResolver(mapFileProvider.Get(market));
var fileProvider = new LocalDiskFactorFileProvider(mapFileProvider);
var securityIdentifier = SecurityIdentifier.GenerateEquity(new DateTime(1999, 01, 01), symbol, market);
var subscriptionDataConfig = new SubscriptionDataConfig(typeof(TradeBar), new Symbol(securityIdentifier, symbol), resolution, DateTimeZone.Utc, DateTimeZone.Utc, false, false, false);
return new SubscriptionDataReader(subscriptionDataConfig, start, end, mapFileResolver, fileProvider, TradeableDays(start, end), false, new ZipDataCacheProvider(new DefaultDataProvider()));
}
}
var readerFactory = new LeanReaderFactory(@"Path\To\Data\...");
var reader = readerFactory.CreateReader(start, end, "SPY", "usa", Resolution.Minute);
while (reader.MoveNext())
{
var bar = (TradeBar)reader.Current;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment