refactor QMT client and optimize API
This commit is contained in:
@@ -12,6 +12,7 @@ from datetime import datetime
|
||||
import config
|
||||
from libs import init_signals, market_allow_open, trading_time
|
||||
from sdk import Client
|
||||
from libs.grid_take_profit import GridTrailingTracker
|
||||
from .state import State
|
||||
from .order import OrderBook
|
||||
from .watch import DipWatch
|
||||
@@ -78,7 +79,9 @@ def StartTrend() -> None:
|
||||
config.account_config.strategy,
|
||||
config.account_config.account_id,
|
||||
)
|
||||
storeState.sync_positions(positions)
|
||||
orders = client.trade_detail_data("order")
|
||||
deals = client.deals()
|
||||
storeState.reconcile(positions, orders, deals)
|
||||
|
||||
# 获取本策略的信号开仓数据
|
||||
signals = init_signals(config.global_config,["morning","tail","arbitrage"])
|
||||
@@ -90,6 +93,7 @@ def StartTrend() -> None:
|
||||
orders=OrderBook(),
|
||||
open_watch=DipWatch(),
|
||||
add_watch=DipWatch(),
|
||||
profit_tracker=GridTrailingTracker(config.account_config.grid_step_pct),
|
||||
)
|
||||
|
||||
logging.info(
|
||||
@@ -129,9 +133,9 @@ def RunOnce(run: Runtime, signals) -> None:
|
||||
except Exception:
|
||||
logging.exception("获取资产失败")
|
||||
return
|
||||
if assets.available < assets.total * run.account_cfg.min_cash_ratio:
|
||||
allow_open_by_cash = assets.available >= assets.total * run.account_cfg.min_cash_ratio
|
||||
if not allow_open_by_cash:
|
||||
logging.info("资金总闸:可用金额太少,禁止开新仓")
|
||||
return
|
||||
|
||||
# 3. 获取大盘状态,只有大盘信号允许时才执行开仓。
|
||||
market_ok = market_allow_open(run.global_cfg.api_host)
|
||||
@@ -143,11 +147,23 @@ def RunOnce(run: Runtime, signals) -> None:
|
||||
logging.exception("获取持仓失败")
|
||||
return
|
||||
|
||||
active_codes = set(position_codes)
|
||||
removed_codes = set(run.state.codes) - active_codes
|
||||
for code in removed_codes:
|
||||
run.state.delete(code)
|
||||
run.open_watch.forget(code)
|
||||
run.add_watch.forget(code)
|
||||
if removed_codes:
|
||||
run.state.save()
|
||||
|
||||
# 5. 验证有效开仓信号:排除已有持仓,并按 signal_allow 过滤。
|
||||
position_code_set = set(position_codes)
|
||||
allow_open = [
|
||||
signal for signal in signals if signal.code not in position_code_set
|
||||
]
|
||||
allow_open = []
|
||||
seen_codes = set(position_code_set)
|
||||
for signal in signals:
|
||||
if signal.code not in seen_codes:
|
||||
allow_open.append(signal)
|
||||
seen_codes.add(signal.code)
|
||||
|
||||
# 6. 获取持仓和待开仓证券的实时行情 tick。
|
||||
all_codes = list(position_codes)
|
||||
@@ -161,7 +177,7 @@ def RunOnce(run: Runtime, signals) -> None:
|
||||
return
|
||||
|
||||
# 7. 执行开仓:必须同时存在有效信号且大盘允许开仓。
|
||||
if allow_open and market_ok:
|
||||
if allow_open and market_ok and allow_open_by_cash:
|
||||
open_signal(run, ticks, allow_open)
|
||||
|
||||
# 8. 持仓计算。当前 Go 版本的 managePositions 为空,保留扩展入口。
|
||||
|
||||
Reference in New Issue
Block a user