Created
May 2, 2021 15:13
-
-
Save arakis/2120bd826066a440142a3a20649ff4f9 to your computer and use it in GitHub Desktop.
Market Data Reader for QuantConnect.Lean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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