@@ -119,7 +119,7 @@ def _get_split(self):
119119 return rqdatac .get_split (stocks )
120120
121121 def _write (self , data_iter : Iterable [tuple [str , np .ndarray ]]):
122- with h5py .File (os .path .join (self .d , 'split_factors .h5' ), "w" ) as h5 :
122+ with h5py .File (os .path .join (self .d , 'split_factor .h5' ), "w" ) as h5 :
123123 for order_book_id , data in data_iter :
124124 h5 .create_dataset (order_book_id , data = data )
125125
@@ -130,32 +130,45 @@ def __call__(self):
130130 split ['split_factor' ] = split ['split_coefficient_to' ] / split ['split_coefficient_from' ]
131131 split = split [['split_factor' ]]
132132 split .reset_index (inplace = True )
133- split .rename (columns = {'ex_dividend_date' : 'ex_date' }, inplace = True )
133+ split .rename (columns = {'ex_dividend_date' : 'ex_date' }, inplace = True ) # type: ignore
134134 split ['ex_date' ] = [convert_date_to_int (d ) for d in split ['ex_date' ]]
135135 split .set_index (['order_book_id' , 'ex_date' ], inplace = True )
136136 self ._write ([(
137137 order_book_id , split .loc [order_book_id ].to_records ()
138138 ) for order_book_id in split .index .levels [0 ]]) # type: ignore
139139
140140
141+ class GenerateExFactorBundle :
142+ def __init__ (self , d : str ):
143+ self .d = d
144+
145+ def _get_ex_factor (self ):
146+ stocks = rqdatac .all_instruments ().order_book_id .tolist ()
147+ return rqdatac .get_ex_factor (stocks )
141148
142- def gen_ex_factor (d ):
143- stocks = rqdatac .all_instruments ().order_book_id .tolist ()
144- ex_factor = rqdatac .get_ex_factor (stocks )
145- ex_factor .reset_index (inplace = True )
146- ex_factor ['ex_date' ] = [convert_date_to_int (d ) for d in ex_factor ['ex_date' ]]
147- ex_factor .rename (columns = {'ex_date' : 'start_date' }, inplace = True )
148- ex_factor .set_index (['order_book_id' , 'start_date' ], inplace = True )
149- ex_factor = ex_factor [['ex_cum_factor' ]]
149+ def _write (self , data_iter : Iterable [tuple [str , np .ndarray ]]):
150+ with h5py .File (os .path .join (self .d , 'ex_cum_factor.h5' ), "w" ) as h5 :
151+ for order_book_id , data in data_iter :
152+ h5 .create_dataset (order_book_id , data = data )
153+
154+ def __call__ (self ):
155+ ex_factor = self ._get_ex_factor ()
156+ if ex_factor is None :
157+ raise RuntimeError ("Got no ex factor data" )
158+ ex_factor .reset_index (inplace = True )
159+ ex_factor ['ex_date' ] = [convert_date_to_int (d ) for d in ex_factor ['ex_date' ]]
160+ ex_factor .rename (columns = {'ex_date' : 'start_date' }, inplace = True )
161+ ex_factor .set_index (['order_book_id' , 'start_date' ], inplace = True )
162+ ex_factor = ex_factor [['ex_cum_factor' ]]
150163
151- dtype = ex_factor .loc [ex_factor .index .levels [0 ][0 ]].to_records ().dtype
152- initial = np .empty ((1 ,), dtype = dtype )
153- initial ['start_date' ] = 0
154- initial ['ex_cum_factor' ] = 1.0
164+ dtype = ex_factor .loc [ex_factor .index .levels [0 ][0 ]].to_records ().dtype # type: ignore
165+ initial = np .empty ((1 ,), dtype = dtype )
166+ initial ['start_date' ] = 0
167+ initial ['ex_cum_factor' ] = 1.0
155168
156- with h5py . File ( os . path . join ( d , 'ex_cum_factor.h5' ), 'w' ) as h5 :
157- for order_book_id in ex_factor .index . levels [ 0 ]:
158- h5 [ order_book_id ] = np . concatenate ([ initial , ex_factor .loc [ order_book_id ]. to_records ()])
169+ self . _write (((
170+ order_book_id , np . concatenate ([ initial , ex_factor .loc [ order_book_id ]. to_records ()])
171+ ) for order_book_id in ex_factor .index . levels [ 0 ])) # type: ignore
159172
160173
161174def gen_share_transformation (d ):
@@ -484,7 +497,7 @@ def gather_tasks(path: str, create: bool, enable_compression: bool, **h5_kwargs)
484497 )
485498
486499 gen_file_funcs = (
487- gen_instruments , gen_trading_dates , gen_ex_factor , gen_st_days ,
500+ gen_instruments , gen_trading_dates , gen_st_days ,
488501 gen_suspended_days , gen_yield_curve , gen_share_transformation , gen_future_info
489502 )
490503 kwargs = {}
@@ -497,6 +510,7 @@ def gather_tasks(path: str, create: bool, enable_compression: bool, **h5_kwargs)
497510 tasks .append (GenerateFileTask (func , path ))
498511 tasks .append (GenerateFileTask (GenerateDividendBundle (path )))
499512 tasks .append (GenerateFileTask (GenerateSplitBundle (path )))
513+ tasks .append (GenerateFileTask (GenerateExFactorBundle (path )))
500514 return tasks
501515
502516
0 commit comments