fix bug
This commit is contained in:
@@ -7,7 +7,7 @@ from pathlib import Path
|
||||
from concurrent.futures import Future, ThreadPoolExecutor
|
||||
import config
|
||||
from libs.calc import trading_time
|
||||
from libs.grid_take_profit import GridTrailingTracker
|
||||
from .profit import ZTProfitTracker
|
||||
from libs.market import market_allow_open
|
||||
from libs.order import OrderBook
|
||||
from libs.overview import Overview
|
||||
@@ -33,8 +33,8 @@ def StartZT() -> None:
|
||||
executor = ThreadPoolExecutor(max_workers=2, thread_name_prefix="zt")
|
||||
run = Runtime(
|
||||
client=client, global_cfg=config.global_config, account_cfg=config.account_config,
|
||||
orders=OrderBook('zt'), open_watch=DipWatch(), add_watch=DipWatch(),
|
||||
profit_tracker=GridTrailingTracker(config.account_config.grid_step_pct),
|
||||
orders=OrderBook(), open_watch=DipWatch(), add_watch=DipWatch(),
|
||||
profit_tracker=ZTProfitTracker(config.account_config.grid_step_pct),
|
||||
executor=executor
|
||||
)
|
||||
|
||||
@@ -56,7 +56,8 @@ def StartZT() -> None:
|
||||
config.account_config.account_id, len(signals), len(positions))
|
||||
cache_portfolio(config.account_config.account_id, assets, positions, deals)
|
||||
state.sync_account(positions, deals, initialize=initialize)
|
||||
run.orders.refresh(client, portfolio.orders)
|
||||
run.profit_tracker.sync_positions(positions, state)
|
||||
run.orders.refresh(client, portfolio.orders, cancel_prefix='zt-')
|
||||
Overview(assets, positions, config.account_config)
|
||||
|
||||
DEFAULT_TICK_INTERVAL = 30
|
||||
@@ -111,9 +112,11 @@ def RunOnce(run: Runtime, state: State, signals: list[SignalItem]) -> None:
|
||||
assets = portfolio.assets
|
||||
positions = list(portfolio.positions.values())
|
||||
position_codes = list(portfolio.positions)
|
||||
cache_portfolio(run.account_cfg.account_id, assets, positions, deals)
|
||||
|
||||
state.sync_account(positions, deals)
|
||||
run.orders.refresh(run.client, portfolio.orders)
|
||||
run.profit_tracker.sync_positions(positions, state)
|
||||
run.orders.refresh(run.client, portfolio.orders, cancel_prefix='zt-')
|
||||
except Exception:
|
||||
log.exception("[Portfolio] 刷新账户快照失败")
|
||||
return
|
||||
|
||||
@@ -213,7 +213,8 @@ def handle_loss(
|
||||
|
||||
|
||||
def _position_key(runtime: Runtime, code: str) -> str:
|
||||
return f"{runtime.account_cfg.account_id}:{code}"
|
||||
# tracker 为该账户的 ZT Runtime 独享,与 sync_positions 使用同一个键。
|
||||
return code
|
||||
|
||||
|
||||
def get_add_num(hands: int, market_value: float) -> int:
|
||||
|
||||
31
py-client/strategy/zt/profit.py
Normal file
31
py-client/strategy/zt/profit.py
Normal file
@@ -0,0 +1,31 @@
|
||||
"""ZT 按已同步的仓位及实际成本管理止盈峰值。"""
|
||||
|
||||
from libs.grid_take_profit import GridTrailingTracker
|
||||
|
||||
|
||||
class ZTProfitTracker(GridTrailingTracker):
|
||||
def __init__(self, step: float = 1.0):
|
||||
super().__init__(step)
|
||||
self._bases: dict[str, tuple] = {}
|
||||
|
||||
def sync_positions(self, positions, state) -> None:
|
||||
# 与交易线程串行执行;提交委托本身不会改变这里的基准。
|
||||
current = {}
|
||||
for position in positions:
|
||||
code = position.stock_code
|
||||
row = state.get_by_code(code)
|
||||
if position.volume <= 0 or not row:
|
||||
continue
|
||||
bucket = 'added' if row.get('added_qty', 0) > 0 else 'base'
|
||||
cost = row.get('added_price', 0) if bucket == 'added' else position.open_price
|
||||
current[code] = (bucket, cost, row.get(f'{bucket}_order_local_id', ''),
|
||||
row.get(f'{bucket}_created_at', ''))
|
||||
for code in self._bases.keys() | current.keys():
|
||||
if code in state.blocked_codes:
|
||||
continue
|
||||
if self._bases.get(code) != current.get(code):
|
||||
self.clear(code)
|
||||
if code in current:
|
||||
self._bases[code] = current[code]
|
||||
else:
|
||||
self._bases.pop(code, None)
|
||||
Reference in New Issue
Block a user