feat libs,sdk,trend
This commit is contained in:
@@ -8,15 +8,16 @@ from __future__ import annotations
|
||||
import time
|
||||
import logging as log
|
||||
from concurrent.futures import Future, ThreadPoolExecutor
|
||||
from copy import deepcopy
|
||||
from datetime import datetime
|
||||
from threading import Lock
|
||||
|
||||
import config
|
||||
from libs.calc import trading_time
|
||||
from libs.market import market_allow_open
|
||||
from libs.overview import Overview
|
||||
from libs.signal import init_signals, SignalItem
|
||||
from libs.collector import collector_push
|
||||
from sdk import Client
|
||||
from sdk import Assets, Client, PositionItem
|
||||
from libs.grid_take_profit import GridTrailingTracker
|
||||
from libs.order import OrderBook
|
||||
from libs.watch import DipWatch
|
||||
@@ -24,6 +25,23 @@ from libs.runtime import Runtime
|
||||
from .open import open_signal
|
||||
from .positions import manage_positions
|
||||
|
||||
_collector_lock = Lock()
|
||||
_collector_snapshot: tuple[str, Assets, list[PositionItem]] | None = None
|
||||
|
||||
|
||||
def _cache_portfolio(account_id: str, assets: Assets, positions: list[PositionItem]) -> None:
|
||||
"""整体替换最新快照,策略线程不执行序列化和网络上报。"""
|
||||
global _collector_snapshot
|
||||
with _collector_lock:
|
||||
_collector_snapshot = (account_id, assets, positions)
|
||||
|
||||
|
||||
def get_collector_snapshot() -> tuple[str, Assets, list[PositionItem]] | None:
|
||||
"""供 scheduler 读取;复制在锁外执行,不阻塞下一轮缓存更新。"""
|
||||
with _collector_lock:
|
||||
snapshot = _collector_snapshot
|
||||
return deepcopy(snapshot)
|
||||
|
||||
|
||||
def StartTrend() -> None:
|
||||
"""初始化趋势策略,并以 30 秒间隔持续执行。"""
|
||||
@@ -37,6 +55,7 @@ def StartTrend() -> None:
|
||||
portfolio = client.portfolio()
|
||||
assets = portfolio.assets
|
||||
positions = list(portfolio.positions.values())
|
||||
_cache_portfolio(config.account_config.account_id, assets, positions)
|
||||
order_book = OrderBook("trend")
|
||||
order_book.refresh(client, portfolio.orders)
|
||||
|
||||
@@ -51,7 +70,7 @@ def StartTrend() -> None:
|
||||
len(signals),
|
||||
len(positions),
|
||||
)
|
||||
executor = ThreadPoolExecutor(max_workers=3, thread_name_prefix="trend")
|
||||
executor = ThreadPoolExecutor(max_workers=2, thread_name_prefix="trend")
|
||||
run = Runtime(
|
||||
client=client,
|
||||
global_cfg=config.global_config,
|
||||
@@ -116,22 +135,13 @@ def RunOnce(run: Runtime, signals: list[SignalItem]) -> None:
|
||||
assets = portfolio.assets
|
||||
position_codes = list(portfolio.positions)
|
||||
positions = list(portfolio.positions.values())
|
||||
_cache_portfolio(run.account_cfg.account_id, assets, positions)
|
||||
run.orders.refresh(run.client, portfolio.orders)
|
||||
except Exception:
|
||||
log.exception("[Portfolio] 刷新账户快照失败")
|
||||
return
|
||||
|
||||
futures: list[tuple[str, Future]] = [
|
||||
(
|
||||
"数据提交",
|
||||
run.executor.submit(
|
||||
collector_push,
|
||||
run.account_cfg.account_id,
|
||||
assets,
|
||||
positions,
|
||||
),
|
||||
)
|
||||
]
|
||||
futures: list[tuple[str, Future]] = []
|
||||
|
||||
# 2. 验证可用资金;低于资金安全线时禁止开新仓。
|
||||
allow_open_by_cash = (
|
||||
|
||||
Reference in New Issue
Block a user