@@ -28,6 +28,7 @@ public class OptionAdditionalFieldGenerator : DerivativeUniverseGenerator.Additi
2828 private const string _deltaHeader = "delta" ;
2929 private const string _sidHeader = "#symbol_id" ;
3030 private const string _tickerHeader = "symbol_value" ;
31+ private Dictionary < string , Dictionary < DateTime , decimal > > _iv30s = new ( ) ;
3132
3233 public OptionAdditionalFieldGenerator ( DateTime processingDate , string rootPath )
3334 : base ( processingDate , rootPath )
@@ -43,6 +44,7 @@ public override bool Run()
4344 {
4445 foreach ( var subFolder in Directory . GetDirectories ( _rootPath ) )
4546 {
47+ _iv30s [ subFolder ] = new ( ) ;
4648 var dateFile = Path . Combine ( subFolder , $ "{ _processingDate : yyyyMMdd} .csv") ;
4749 var symbol = subFolder . Split ( Path . DirectorySeparatorChar ) [ ^ 1 ] . ToUpper ( ) ;
4850 if ( ! File . Exists ( dateFile ) )
@@ -72,14 +74,22 @@ private List<decimal> GetIvs(DateTime currentDateTime, string path)
7274 // get i-year ATM IVs to calculate IV rank and percentile
7375 var lastYearFiles = Directory . EnumerateFiles ( path , "*.csv" )
7476 . AsParallel ( )
75- . Where ( file => DateTime . TryParseExact ( Path . GetFileNameWithoutExtension ( file ) , "yyyyMMdd" ,
77+ . Where ( file => DateTime . TryParseExact ( Path . GetFileNameWithoutExtension ( file ) , "yyyyMMdd" ,
7678 CultureInfo . InvariantCulture , DateTimeStyles . None , out var fileDate )
7779 && fileDate > currentDateTime . AddYears ( - 1 )
78- && fileDate <= currentDateTime )
79- . OrderBy ( file => file )
80- . ToList ( ) ;
80+ && fileDate <= currentDateTime
81+ && ! _iv30s [ path ] . ContainsKey ( fileDate ) )
82+ . ToDictionary (
83+ file => DateTime . ParseExact ( Path . GetFileNameWithoutExtension ( file ) , "yyyyMMdd" ,
84+ CultureInfo . InvariantCulture , DateTimeStyles . None ) ,
85+ file => GetAtmIv ( file )
86+ ) ;
87+ _iv30s [ path ] = _iv30s [ path ] . Concat ( lastYearFiles )
88+ . ToDictionary ( x => x . Key , x => x . Value ) ;
8189
82- return lastYearFiles . Select ( csvFile => GetAtmIv ( csvFile ) )
90+ return _iv30s [ path ] . Where ( x => x . Key > currentDateTime . AddYears ( - 1 ) && x . Key <= currentDateTime )
91+ . OrderBy ( x => x . Key )
92+ . Select ( x => x . Value )
8393 . ToList ( ) ;
8494 }
8595
0 commit comments