1414*/
1515
1616using System ;
17+ using QuantConnect . Configuration ;
1718using QuantConnect . Util ;
1819using QuantConnect . Logging ;
20+ using System . Globalization ;
1921using System . Threading . Tasks ;
2022using QuantConnect . Interfaces ;
2123using System . Collections . Generic ;
@@ -27,6 +29,7 @@ namespace QuantConnect.Data.Auxiliary
2729 /// </summary>
2830 public class LocalZipMapFileProvider : IMapFileProvider
2931 {
32+ private readonly DateTime ? _lookupDate ;
3033 private Dictionary < AuxiliaryDataKey , MapFileResolver > _cache ;
3134 private IDataProvider _dataProvider ;
3235 private object _lock ;
@@ -50,12 +53,18 @@ protected virtual TimeSpan CacheRefreshPeriod
5053 }
5154
5255 /// <summary>
53- /// Creates a new instance of the <see cref="LocalDiskFactorFileProvider "/>
56+ /// Creates a new instance of the <see cref="LocalZipMapFileProvider "/>
5457 /// </summary>
5558 public LocalZipMapFileProvider ( )
5659 {
5760 _lock = new object ( ) ;
5861 _cache = new Dictionary < AuxiliaryDataKey , MapFileResolver > ( ) ;
62+
63+ var lookupDateConfig = Config . Get ( "map-file-provider-lookup-date" ) ;
64+ if ( DateTime . TryParseExact ( lookupDateConfig , "yyyyMMdd" , CultureInfo . InvariantCulture , DateTimeStyles . None , out var lookupDate ) )
65+ {
66+ _lookupDate = lookupDate ;
67+ }
5968 }
6069
6170 /// <summary>
@@ -111,36 +120,29 @@ private MapFileResolver GetMapFileResolver(AuxiliaryDataKey auxiliaryDataKey)
111120 var market = auxiliaryDataKey . Market ;
112121 var timestamp = DateTime . UtcNow . ConvertFromUtc ( TimeZones . NewYork ) ;
113122 var todayNewYork = timestamp . Date ;
114- var yesterdayNewYork = todayNewYork . AddDays ( - 1 ) ;
115-
123+ // If a lookup date was provided, use it, otherwise use yesterday as the starting point for our search
124+ var yesterdayNewYork = _lookupDate ?? todayNewYork . AddDays ( - 1 ) ;
125+ // To prevent infinite recursion if something is wrong with the zip files, we look back a maximum of 30 days
126+ var endDate = yesterdayNewYork . AddDays ( _lookupDate . HasValue ? 0 : - 30 ) ;
127+
116128 // start the search with yesterday, today's file will be available tomorrow
117- var count = 0 ;
118- var date = yesterdayNewYork ;
119- do
129+ for ( var date = yesterdayNewYork ; date >= endDate ; date = date . AddDays ( - 1 ) )
120130 {
121131 var zipFileName = MapFileZipHelper . GetMapFileZipFileName ( market , date , auxiliaryDataKey . SecurityType ) ;
122132
123133 // Fetch a stream for our zip from our data provider
124134 var stream = _dataProvider . Fetch ( zipFileName ) ;
125135
126- // If we found a file we can read it
127- if ( stream != null )
128- {
129- Log . Trace ( "LocalZipMapFileProvider.Get({0}): Fetched map files for: {1} NY" , market , date . ToShortDateString ( ) ) ;
130- var result = new MapFileResolver ( MapFileZipHelper . ReadMapFileZip ( stream , market , auxiliaryDataKey . SecurityType ) ) ;
131- stream . DisposeSafely ( ) ;
132- return result ;
133- }
136+ // If we didn't find a file, continue to the next date
137+ if ( stream == null ) continue ;
134138
135- // prevent infinite recursion if something is wrong
136- if ( count ++ > 30 )
137- {
138- throw new InvalidOperationException ( $ "LocalZipMapFileProvider couldn't find any map files going all the way back to { date } for { market } ") ;
139- }
140-
141- date = date . AddDays ( - 1 ) ;
139+ Log . Trace ( $ "LocalZipMapFileProvider.Get({ market } ): Fetched map files for: { date . ToShortDateString ( ) } NY ({ ( date - todayNewYork ) . Days } days ago).") ;
140+ var result = new MapFileResolver ( MapFileZipHelper . ReadMapFileZip ( stream , market , auxiliaryDataKey . SecurityType ) ) ;
141+ stream . DisposeSafely ( ) ;
142+ return result ;
142143 }
143- while ( true ) ;
144+
145+ throw new InvalidOperationException ( $ "LocalZipMapFileProvider couldn't find any map files going all the way back to { endDate . ToShortDateString ( ) } for { market } ") ;
144146 }
145147 }
146148}
0 commit comments