Skip to content

Commit b2c417e

Browse files
committed
fix bug
1 parent 5cd9476 commit b2c417e

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

trader/strategy/brother2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from trader.utils.func_container import RegisterCallback
2929
from trader.utils.read_config import config, ctp_errors
3030
from trader.utils import ApiStruct, price_round, is_trading_day, update_from_shfe, update_from_dce, update_from_czce, update_from_cffex, \
31-
get_contracts_argument, calc_main_inst, str_to_number, get_next_id, ORDER_REF_SIGNAL_ID_START
31+
get_contracts_argument, calc_main_inst, str_to_number, get_next_id, ORDER_REF_SIGNAL_ID_START, update_from_gfex
3232
from panel.models import *
3333

3434
logger = logging.getLogger('CTPApi')
@@ -687,7 +687,7 @@ async def collect_quote(self, tasks=None):
687687
return
688688
logger.debug(f'{day}盘后计算,获取交易所日线数据..')
689689
if tasks is None:
690-
tasks = [update_from_shfe, update_from_dce, update_from_czce, update_from_cffex, get_contracts_argument]
690+
tasks = [update_from_shfe, update_from_dce, update_from_czce, update_from_cffex, update_from_gfex, get_contracts_argument]
691691
result = await asyncio.gather(*[func(day) for func in tasks], return_exceptions=True)
692692
if all(result):
693693
self.io_loop.call_soon(self.calculate, day)

trader/utils/__init__.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
max_conn_shfe = asyncio.Semaphore(15)
4343
max_conn_dce = asyncio.Semaphore(5)
44+
max_conn_gfex = asyncio.Semaphore(5)
4445
max_conn_czce = asyncio.Semaphore(15)
4546
max_conn_cffex = asyncio.Semaphore(15)
4647
max_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+
265294
async 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: 广期所
653683
async def get_contracts_argument(day: datetime.datetime = None) -> bool:
654684
try:
655685
if day is None:

0 commit comments

Comments
 (0)