Files
big-qmt/py-client/strategy/zt/profit.py
2026-09-12 16:25:34 +08:00

32 lines
1.3 KiB
Python

"""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)