Skip to content

Commit

Permalink
0.9.47 update tqsdk
Browse files Browse the repository at this point in the history
  • Loading branch information
zengbin93 committed Apr 9, 2024
1 parent 1d6d78e commit c599d21
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions czsc/connectors/tq_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,26 +183,6 @@ def format_kline(df, freq=Freq.F1):
}


def is_trade_time(trade_time: Optional[str] = None):
"""判断当前是否是交易时间"""
if trade_time is None:
trade_time = datetime.now().strftime("%H:%M:%S")

if trade_time >= "09:00:00" and trade_time <= "11:30:00":
return True

if trade_time >= "13:00:00" and trade_time <= "15:00:00":
return True

if trade_time >= "21:00:00" and trade_time <= "23:59:59":
return True

if trade_time >= "00:00:00" and trade_time <= "02:30:00":
return True

return False


def get_daily_backup(api: TqApi, **kwargs):
"""获取每日账户中需要备份的信息
Expand Down Expand Up @@ -238,6 +218,18 @@ def get_daily_backup(api: TqApi, **kwargs):
return backup


def is_trade_time(quote):
"""判断当前是否是交易时间"""
trade_time = pd.Timestamp.now().strftime("%H:%M:%S")
times = quote["trading_time"]['day'] + quote["trading_time"]['night']

for sdt, edt in times:
if trade_time >= sdt and trade_time <= edt:
logger.info(f"当前时间:{trade_time},交易时间:{sdt} - {edt}")
return True
return False


def adjust_portfolio(api: TqApi, portfolio, account=None, **kwargs):
"""调整账户组合
Expand All @@ -255,11 +247,21 @@ def adjust_portfolio(api: TqApi, portfolio, account=None, **kwargs):
:param kwargs: dict, 其他参数
"""
timeout = kwargs.get("timeout", 600)
start_time = datetime.now()

symbol_infos = {}
for symbol, conf in portfolio.items():
quote = api.get_quote(symbol)
if not is_trade_time(quote):
logger.warning(f"{symbol} 当前时间不是交易时间,跳过调仓")
continue

lots = conf.get("target_volume", None)
if lots is None:
logger.warning(f"{symbol} 目标手数为 None,跳过调仓")
continue

lots = conf.get("target_volume", 0)
price = conf.get("price", "PASSIVE")
offset_priority = conf.get("offset_priority", "今昨,开")

Expand Down Expand Up @@ -290,7 +292,8 @@ def adjust_portfolio(api: TqApi, portfolio, account=None, **kwargs):
if all(completed):
break

if kwargs.get("close_api", True):
api.close()
if (datetime.now() - start_time).seconds > timeout:
logger.error(f"调仓超时,已运行 {timeout} 秒")
break

return api

0 comments on commit c599d21

Please sign in to comment.