2026-09-10 12:50:40 +08:00
|
|
|
|
"""先卖后买做 T;持仓存在 State,未买回数量从实际成交计算。"""
|
2026-08-31 13:00:22 +08:00
|
|
|
|
|
2026-09-06 11:53:28 +08:00
|
|
|
|
import logging as log
|
|
|
|
|
|
import math
|
2026-08-31 13:00:22 +08:00
|
|
|
|
|
|
|
|
|
|
from libs.grid_take_profit import GridState
|
2026-09-06 13:12:48 +08:00
|
|
|
|
from libs.order import PlaceOrderRequest
|
|
|
|
|
|
from libs.runtime import Runtime
|
2026-09-10 12:50:40 +08:00
|
|
|
|
from libs.state import State
|
|
|
|
|
|
from sdk import OP_BUY, OP_SELL, PositionItem
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def t_rounds(store: State) -> dict[str, dict]:
|
|
|
|
|
|
"""每只证券保留最近一轮 T,跨日未买回的数量继续保留。"""
|
|
|
|
|
|
rounds = {}
|
|
|
|
|
|
for deal in sorted(store.deals.values(), key=lambda d: (
|
|
|
|
|
|
d['trade_date'], d['trade_time'].replace(':', ''), d['id']
|
|
|
|
|
|
)):
|
|
|
|
|
|
order = deal['order_local_id']
|
|
|
|
|
|
code = deal['stock_code']
|
|
|
|
|
|
if order.startswith('zt-t-sell-') and deal['offset_flag'] in (24, 49):
|
|
|
|
|
|
item = rounds.get(code)
|
|
|
|
|
|
if item is None or item['bought'] >= item['sold']:
|
|
|
|
|
|
item = rounds[code] = dict(sold=0, bought=0, amount=0.0, date='')
|
|
|
|
|
|
item['sold'] += deal['volume']
|
|
|
|
|
|
item['amount'] += deal['trade_amount']
|
|
|
|
|
|
item['date'] = deal['trade_date']
|
|
|
|
|
|
elif order.startswith('zt-t-buy-') and deal['offset_flag'] in (23, 48) and code in rounds:
|
|
|
|
|
|
rounds[code]['bought'] += deal['volume']
|
|
|
|
|
|
rounds[code]['date'] = deal['trade_date']
|
|
|
|
|
|
return rounds
|
2026-08-31 13:00:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
2026-09-06 13:12:48 +08:00
|
|
|
|
def manage_positions(
|
2026-09-10 12:50:40 +08:00
|
|
|
|
run: Runtime, store: State, ticks, positions: list[PositionItem],
|
|
|
|
|
|
rounds: dict[str, dict], available: float, today: str, force_buy_back: bool = False,
|
2026-09-06 13:12:48 +08:00
|
|
|
|
) -> float:
|
2026-09-10 12:50:40 +08:00
|
|
|
|
"""先偿还买回欠仓;同一证券当天完成一轮后不再卖出。"""
|
|
|
|
|
|
by_code = {p.stock_code: p for p in positions}
|
|
|
|
|
|
codes = dict.fromkeys(list(rounds) + list(store.state))
|
|
|
|
|
|
for code in sorted(codes, key=lambda c: not (c in rounds and rounds[c]['sold'] > rounds[c]['bought'])):
|
2026-08-31 13:00:22 +08:00
|
|
|
|
try:
|
2026-09-07 00:27:33 +08:00
|
|
|
|
if code in run.account_cfg.excluded_codes:
|
2026-09-06 11:53:28 +08:00
|
|
|
|
continue
|
2026-09-10 12:50:40 +08:00
|
|
|
|
if run.orders.busy(code, 'BUY') or run.orders.busy(code, 'SELL'):
|
2026-09-06 11:53:28 +08:00
|
|
|
|
continue
|
|
|
|
|
|
tick = ticks.get(code)
|
|
|
|
|
|
price = tick.last_price if tick else 0.0
|
|
|
|
|
|
if not math.isfinite(price) or price <= 0:
|
|
|
|
|
|
continue
|
2026-09-10 12:50:40 +08:00
|
|
|
|
item = rounds.get(code)
|
|
|
|
|
|
row = store.state.get(code, {})
|
2026-09-06 11:53:28 +08:00
|
|
|
|
position = by_code.get(code)
|
2026-09-10 12:50:40 +08:00
|
|
|
|
recorded = row.get('base_qty', 0) + row.get('added_qty', 0)
|
|
|
|
|
|
if recorded != (position.volume if position else 0):
|
|
|
|
|
|
log.warning('[ZT] %s 持仓快照与成交未对齐,等待下一轮', code)
|
|
|
|
|
|
continue
|
|
|
|
|
|
if item and item['sold'] > item['bought']:
|
|
|
|
|
|
volume = item['sold'] - item['bought']
|
|
|
|
|
|
# 部分成交后的零股欠仓不能按普通买入申报,不扩大买回数量。
|
|
|
|
|
|
minimum = 200 if code.startswith('688') else 100
|
|
|
|
|
|
if not code.startswith('688'):
|
|
|
|
|
|
volume = volume // 100 * 100
|
|
|
|
|
|
if volume < minimum:
|
|
|
|
|
|
log.warning('[ZT] %s 剩余买回 %d 股不满足申报数量,保留欠仓', code, item['sold'] - item['bought'])
|
|
|
|
|
|
continue
|
|
|
|
|
|
target = item['amount'] / item['sold'] * (1 - run.account_cfg.zt_buy_fall_pct / 100)
|
|
|
|
|
|
if not force_buy_back and price > target:
|
|
|
|
|
|
continue
|
|
|
|
|
|
amount = price * volume * 1.01
|
|
|
|
|
|
if amount > available:
|
|
|
|
|
|
log.warning('[ZT] %s 买回资金不足,需要 %.2f,可用 %.2f', code, amount, available)
|
|
|
|
|
|
continue
|
|
|
|
|
|
if not force_buy_back and not run.add_watch.triggered('ZT 买回', code, price):
|
|
|
|
|
|
continue
|
|
|
|
|
|
available -= amount
|
2026-09-11 10:44:34 +08:00
|
|
|
|
request = PlaceOrderRequest(OP_BUY, code, volume, run.orders.new_order_id('zt', 't-buy'), 'zt')
|
2026-09-10 12:50:40 +08:00
|
|
|
|
if run.orders.place(run.client, request):
|
|
|
|
|
|
run.add_watch.forget(code)
|
|
|
|
|
|
log.info('[ZT 买回] %s %d 股%s', code, volume, ',尾盘买回' if force_buy_back else '')
|
|
|
|
|
|
continue
|
|
|
|
|
|
if force_buy_back or (item and item['date'] >= today) or not position or recorded <= 0:
|
|
|
|
|
|
continue
|
|
|
|
|
|
if price > run.account_cfg.zt_max_price:
|
|
|
|
|
|
continue
|
|
|
|
|
|
cost = (row['base_qty'] * row['base_price'] + row['added_qty'] * row['added_price']) / recorded
|
|
|
|
|
|
if cost <= 0:
|
2026-09-06 11:53:28 +08:00
|
|
|
|
continue
|
2026-09-10 12:50:40 +08:00
|
|
|
|
pnl = (price / cost - 1) * 100
|
|
|
|
|
|
observation = run.profit_tracker.observe(f'{run.account_cfg.account_id}:{code}:{today}', pnl)
|
|
|
|
|
|
if observation.state != GridState.RETREAT or pnl < run.account_cfg.min_profit_pct:
|
|
|
|
|
|
continue
|
|
|
|
|
|
volume = int(min(position.can_use_volume, recorded * run.account_cfg.zt_sell_ratio)) // 100 * 100
|
|
|
|
|
|
if volume < (200 if code.startswith('688') else 100):
|
|
|
|
|
|
continue
|
2026-09-11 10:44:34 +08:00
|
|
|
|
request = PlaceOrderRequest(OP_SELL, code, volume, run.orders.new_order_id('zt', 't-sell'), 'zt')
|
2026-09-10 12:50:40 +08:00
|
|
|
|
if run.orders.place(run.client, request):
|
|
|
|
|
|
log.info('[ZT 卖出] %s %d 股,按实际成交买回', code, volume)
|
2026-09-06 11:53:28 +08:00
|
|
|
|
except Exception:
|
2026-09-10 12:50:40 +08:00
|
|
|
|
log.exception('[ZT 持仓] %s 处理失败', code)
|
2026-09-06 11:53:28 +08:00
|
|
|
|
return available
|