4141
4242max_conn_shfe = asyncio .Semaphore (15 )
4343max_conn_dce = asyncio .Semaphore (5 )
44+ max_conn_gfex = asyncio .Semaphore (5 )
4445max_conn_czce = asyncio .Semaphore (15 )
4546max_conn_cffex = asyncio .Semaphore (15 )
4647max_conn_sina = asyncio .Semaphore (15 )
@@ -241,10 +242,11 @@ async def update_from_dce(day: datetime.datetime) -> bool:
241242 continue
242243 if DCE_NAME_CODE [inst_data [0 ]] in IGNORE_INST_LIST :
243244 continue
245+ expire_date = inst_data [1 ].removeprefix (DCE_NAME_CODE [inst_data [0 ]])
244246 DailyBar .objects .update_or_create (
245- code = DCE_NAME_CODE [ inst_data [ 0 ]] + inst_data [1 ],
247+ code = inst_data [1 ],
246248 exchange = ExchangeType .DCE , time = day , defaults = {
247- 'expire_date' : inst_data [ 1 ] ,
249+ 'expire_date' : expire_date ,
248250 'open' : inst_data [2 ].replace (',' , '' ) if inst_data [2 ] != '-' else
249251 inst_data [5 ].replace (',' , '' ),
250252 'high' : inst_data [3 ].replace (',' , '' ) if inst_data [3 ] != '-' else
@@ -262,6 +264,33 @@ async def update_from_dce(day: datetime.datetime) -> bool:
262264 return False
263265
264266
267+ async def update_from_gfex (day : datetime .datetime ) -> bool :
268+ try :
269+ async with aiohttp .ClientSession () as session :
270+ await max_conn_gfex .acquire ()
271+ async with session .post ('http://www.gfex.com.cn/gfexweb/Quote/getQuote_ftr' , data = {'varietyid' : 'si' }) as response :
272+ rst = await response .text ()
273+ rst = json .loads (rst )
274+ max_conn_gfex .release ()
275+ for inst_code , inst_data in rst ['contractQuote' ].items ():
276+ expire_date = inst_code .removeprefix ('si' )
277+ DailyBar .objects .update_or_create (
278+ code = inst_code ,
279+ exchange = ExchangeType .GFEX , time = day , defaults = {
280+ 'expire_date' : expire_date ,
281+ 'open' : inst_data ['openPrice' ] if inst_data ['openPrice' ] != "--" else inst_data ['closePrice' ],
282+ 'high' : inst_data ['highPrice' ] if inst_data ['highPrice' ] != "--" else inst_data ['closePrice' ],
283+ 'low' : inst_data ['lowPrice' ] if inst_data ['lowPrice' ] != "--" else inst_data ['closePrice' ],
284+ 'close' : inst_data ['closePrice' ],
285+ 'settlement' : inst_data ['clearPrice' ],
286+ 'volume' : inst_data ['matchTotQty' ] if inst_data ['matchTotQty' ] != "--" else 0 ,
287+ 'open_interest' : inst_data ['openInterest' ] if inst_data ['openInterest' ] != "--" else 0 })
288+ return True
289+ except Exception as e :
290+ logger .warning (f'update_from_gfex failed: { repr (e )} ' , exc_info = True )
291+ return False
292+
293+
265294async def update_from_cffex (day : datetime .datetime ) -> bool :
266295 try :
267296 async with aiohttp .ClientSession () as session :
@@ -368,7 +397,8 @@ def calc_main_inst(inst: Instrument, day: datetime.datetime):
368397 exchange = inst .exchange , code__regex = f"^{ inst .product_code } [0-9]+" ,
369398 expire_date__gte = expire_date , time = day .date ()).order_by ('-volume' , '-open_interest' , 'code' ).first ()
370399 if check_bar is None :
371- logger .error (f"calc_main_inst 未找到主力合约:{ inst } " )
400+ check_bar = DailyBar .objects .filter (code = inst .main_code ).last ()
401+ logger .error (f"calc_main_inst 未找到主力合约:{ inst } 使用上一个主力合约" )
372402 if inst .main_code is None : # 之前没有主力合约
373403 inst .main_code = check_bar .code
374404 inst .change_time = day
@@ -649,7 +679,7 @@ def load_kt_data(directory: str = r'D:\test'):
649679 return False
650680
651681
652- # 从交易所获取合约当日的涨跌停幅度
682+ # 从交易所获取合约当日的涨跌停幅度 TODO: 广期所
653683async def get_contracts_argument (day : datetime .datetime = None ) -> bool :
654684 try :
655685 if day is None :
0 commit comments