feat: 优化界面并补充网格成交分析
This commit is contained in:
@@ -177,9 +177,10 @@ def calculate_account_summary(
|
||||
sum((position.floating_pnl or Decimal("0") for position in positions), Decimal("0"))
|
||||
)
|
||||
total_assets = money(cash + market_value)
|
||||
deployable_assets = market_value + max(cash, Decimal("0"))
|
||||
capital_usage_rate = (
|
||||
(market_value / total_assets).quantize(RATE_PLACES, rounding=ROUND_HALF_UP)
|
||||
if total_assets > 0
|
||||
(market_value / deployable_assets).quantize(RATE_PLACES, rounding=ROUND_HALF_UP)
|
||||
if deployable_assets > 0
|
||||
else Decimal("0")
|
||||
)
|
||||
return AccountSummary(
|
||||
|
||||
@@ -138,6 +138,20 @@ class OpenGridLot:
|
||||
status: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class GridTradeMatch:
|
||||
sell_trade_id: int | None
|
||||
sell_date: date
|
||||
sell_price: Decimal
|
||||
buy_trade_id: int | None
|
||||
buy_date: date
|
||||
buy_price: Decimal
|
||||
matched_quantity: int
|
||||
buy_amount: Decimal
|
||||
sell_amount: Decimal
|
||||
gross_profit: Decimal
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class QuoteSnapshot:
|
||||
symbol: str
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import replace
|
||||
from dataclasses import dataclass, replace
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
|
||||
from grid_trading.domain.calculations import money, price
|
||||
from grid_trading.domain.models import OpenGridLot, Trade, TradeGroup, TradeSide
|
||||
from grid_trading.domain.models import GridTradeMatch, OpenGridLot, Trade, TradeGroup, TradeSide
|
||||
|
||||
|
||||
@dataclass
|
||||
class _OpenBuyLot:
|
||||
trade_id: int | None
|
||||
buy_date: date
|
||||
buy_price: Decimal
|
||||
remaining_quantity: int
|
||||
|
||||
|
||||
def calculate_open_grid_lots(
|
||||
@@ -45,6 +53,52 @@ def calculate_open_grid_lots(
|
||||
return lots
|
||||
|
||||
|
||||
def calculate_grid_trade_matches(
|
||||
trades: list[Trade],
|
||||
*,
|
||||
as_of: date,
|
||||
) -> list[GridTradeMatch]:
|
||||
open_buys: list[_OpenBuyLot] = []
|
||||
matches: list[GridTradeMatch] = []
|
||||
for trade in sorted(trades, key=lambda item: (item.trade_date, item.id or 0)):
|
||||
if trade.trade_date > as_of or trade.trade_group is not TradeGroup.GRID:
|
||||
continue
|
||||
if trade.side is TradeSide.BUY:
|
||||
open_buys.append(
|
||||
_OpenBuyLot(
|
||||
trade_id=trade.id,
|
||||
buy_date=trade.trade_date,
|
||||
buy_price=price(trade.price),
|
||||
remaining_quantity=trade.quantity,
|
||||
)
|
||||
)
|
||||
continue
|
||||
sell_price = price(trade.price)
|
||||
remaining_sell_quantity = trade.quantity
|
||||
while remaining_sell_quantity > 0 and open_buys:
|
||||
buy_lot = open_buys[0]
|
||||
matched_quantity = min(buy_lot.remaining_quantity, remaining_sell_quantity)
|
||||
matches.append(
|
||||
GridTradeMatch(
|
||||
sell_trade_id=trade.id,
|
||||
sell_date=trade.trade_date,
|
||||
sell_price=sell_price,
|
||||
buy_trade_id=buy_lot.trade_id,
|
||||
buy_date=buy_lot.buy_date,
|
||||
buy_price=buy_lot.buy_price,
|
||||
matched_quantity=matched_quantity,
|
||||
buy_amount=money(buy_lot.buy_price * Decimal(matched_quantity)),
|
||||
sell_amount=money(sell_price * Decimal(matched_quantity)),
|
||||
gross_profit=money((sell_price - buy_lot.buy_price) * Decimal(matched_quantity)),
|
||||
)
|
||||
)
|
||||
buy_lot.remaining_quantity -= matched_quantity
|
||||
remaining_sell_quantity -= matched_quantity
|
||||
if buy_lot.remaining_quantity == 0:
|
||||
open_buys.pop(0)
|
||||
return matches
|
||||
|
||||
|
||||
def _match_sell_against_lots(lots: list[OpenGridLot], quantity: int) -> list[OpenGridLot]:
|
||||
remaining_sell_quantity = quantity
|
||||
updated_lots: list[OpenGridLot] = []
|
||||
|
||||
@@ -19,6 +19,7 @@ from grid_trading.domain.models import (
|
||||
FeeEstimate,
|
||||
FeeRules,
|
||||
GridLevelSuggestion,
|
||||
GridTradeMatch,
|
||||
Instrument,
|
||||
OpenGridLot,
|
||||
PositionSummary,
|
||||
@@ -28,7 +29,7 @@ from grid_trading.domain.models import (
|
||||
Trade,
|
||||
TradeSide,
|
||||
)
|
||||
from grid_trading.domain.open_grid_lots import calculate_open_grid_lots
|
||||
from grid_trading.domain.open_grid_lots import calculate_grid_trade_matches, calculate_open_grid_lots
|
||||
from grid_trading.market.tencent import TencentQuoteProvider
|
||||
from grid_trading.storage.repositories import Repository
|
||||
|
||||
@@ -262,6 +263,18 @@ class TradingService:
|
||||
as_of=as_of_date,
|
||||
)
|
||||
|
||||
def get_grid_trade_matches(
|
||||
self,
|
||||
instrument_id: int,
|
||||
*,
|
||||
as_of: date | None = None,
|
||||
) -> list[GridTradeMatch]:
|
||||
as_of_date = as_of or date.today()
|
||||
self._require_instrument(instrument_id)
|
||||
account = self.get_active_account()
|
||||
trades = self.repository.list_trades(account_id=account.id, instrument_id=instrument_id)
|
||||
return calculate_grid_trade_matches(trades, as_of=as_of_date)
|
||||
|
||||
def _require_account(self, account_id: int) -> Account:
|
||||
account = self.repository.get_account(account_id)
|
||||
if account is None:
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
|
||||
from PySide6.QtCore import QObject, Qt, QThread, Signal
|
||||
from PySide6.QtGui import QBrush, QColor, QFont
|
||||
from PySide6.QtWidgets import (
|
||||
QAbstractItemView,
|
||||
QApplication,
|
||||
QDialog,
|
||||
QFrame,
|
||||
@@ -26,7 +29,14 @@ from PySide6.QtWidgets import (
|
||||
)
|
||||
|
||||
from grid_trading.config import DEFAULT_DB_PATH
|
||||
from grid_trading.domain.models import GridLevelSuggestion, OpenGridLot, PositionSummary, Trade, TradeSide
|
||||
from grid_trading.domain.models import (
|
||||
GridLevelSuggestion,
|
||||
GridTradeMatch,
|
||||
OpenGridLot,
|
||||
PositionSummary,
|
||||
Trade,
|
||||
TradeSide,
|
||||
)
|
||||
from grid_trading.services.trading_service import TradingService
|
||||
from grid_trading.ui.dialogs import AccountDialog, InstrumentDialog, StrategyTemplateDialog, TradeDialog
|
||||
from grid_trading.ui.formatters import format_money, format_percent, format_price, format_quantity
|
||||
@@ -51,6 +61,10 @@ class QuoteRefreshWorker(QObject):
|
||||
|
||||
|
||||
class MainWindow(QMainWindow):
|
||||
PROFIT_COLOR = QColor("#c62828")
|
||||
LOSS_COLOR = QColor("#2e7d32")
|
||||
NEUTRAL_COLOR = QColor("#334155")
|
||||
|
||||
HOLDING_COLUMNS = [
|
||||
"代码",
|
||||
"名称",
|
||||
@@ -86,6 +100,17 @@ class MainWindow(QMainWindow):
|
||||
"当前价",
|
||||
"状态",
|
||||
]
|
||||
GRID_MATCH_COLUMNS = [
|
||||
"卖出日期",
|
||||
"卖出价",
|
||||
"匹配买入日期",
|
||||
"买入价",
|
||||
"匹配股数",
|
||||
"买入金额",
|
||||
"卖出金额",
|
||||
"毛利润",
|
||||
"对应成交",
|
||||
]
|
||||
|
||||
def __init__(self, service: TradingService):
|
||||
super().__init__()
|
||||
@@ -104,16 +129,26 @@ class MainWindow(QMainWindow):
|
||||
self.refresh_all()
|
||||
|
||||
def _build_ui(self) -> None:
|
||||
self._apply_light_theme()
|
||||
|
||||
root = QWidget()
|
||||
root.setObjectName("appRoot")
|
||||
root_layout = QHBoxLayout(root)
|
||||
root_layout.setContentsMargins(14, 14, 14, 14)
|
||||
root_layout.setSpacing(14)
|
||||
|
||||
nav = QListWidget()
|
||||
nav.setObjectName("sideNav")
|
||||
nav.addItems(["账户总览", "持仓管理", "成交记录", "网格策略", "设置"])
|
||||
nav.setFixedWidth(150)
|
||||
nav.setCurrentRow(1)
|
||||
root_layout.addWidget(nav)
|
||||
|
||||
content = QWidget()
|
||||
content.setObjectName("contentPanel")
|
||||
content_layout = QVBoxLayout(content)
|
||||
content_layout.setContentsMargins(0, 0, 0, 0)
|
||||
content_layout.setSpacing(12)
|
||||
self.summary_labels = self._create_summary_cards()
|
||||
content_layout.addLayout(self.summary_cards_layout)
|
||||
content_layout.addLayout(self._create_toolbar())
|
||||
@@ -121,17 +156,17 @@ class MainWindow(QMainWindow):
|
||||
splitter = QSplitter(Qt.Orientation.Vertical)
|
||||
self.holdings_table = QTableWidget(0, len(self.HOLDING_COLUMNS))
|
||||
self.holdings_table.setHorizontalHeaderLabels(self.HOLDING_COLUMNS)
|
||||
self.holdings_table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents)
|
||||
self.holdings_table.horizontalHeader().setStretchLastSection(True)
|
||||
self.holdings_table.setSelectionBehavior(QTableWidget.SelectionBehavior.SelectRows)
|
||||
self.holdings_table.setEditTriggers(QTableWidget.EditTrigger.NoEditTriggers)
|
||||
self._configure_table(self.holdings_table)
|
||||
self.holdings_table.itemSelectionChanged.connect(self._on_holding_selected)
|
||||
splitter.addWidget(self.holdings_table)
|
||||
|
||||
self.details_tabs = QTabWidget()
|
||||
self.details_tabs.setObjectName("detailsTabs")
|
||||
|
||||
grid_tab = QWidget()
|
||||
grid_layout = QVBoxLayout(grid_tab)
|
||||
grid_layout.setContentsMargins(10, 10, 10, 10)
|
||||
grid_layout.setSpacing(8)
|
||||
grid_controls = QHBoxLayout()
|
||||
self.grid_levels_count_edit = QSpinBox()
|
||||
self.grid_levels_count_edit.setRange(1, 100)
|
||||
@@ -144,41 +179,51 @@ class MainWindow(QMainWindow):
|
||||
grid_controls.addStretch()
|
||||
self.grid_levels_table = QTableWidget(0, len(self.GRID_LEVEL_COLUMNS))
|
||||
self.grid_levels_table.setHorizontalHeaderLabels(self.GRID_LEVEL_COLUMNS)
|
||||
self.grid_levels_table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents)
|
||||
self.grid_levels_table.horizontalHeader().setStretchLastSection(True)
|
||||
self.grid_levels_table.setEditTriggers(QTableWidget.EditTrigger.NoEditTriggers)
|
||||
self._configure_table(self.grid_levels_table)
|
||||
grid_layout.addLayout(grid_controls)
|
||||
grid_layout.addWidget(self.grid_levels_table)
|
||||
|
||||
open_grid_tab = QWidget()
|
||||
open_grid_layout = QVBoxLayout(open_grid_tab)
|
||||
open_grid_layout.setContentsMargins(10, 10, 10, 10)
|
||||
open_grid_layout.setSpacing(8)
|
||||
self.open_grid_lots_table = QTableWidget(0, len(self.OPEN_GRID_LOT_COLUMNS))
|
||||
self.open_grid_lots_table.setHorizontalHeaderLabels(self.OPEN_GRID_LOT_COLUMNS)
|
||||
self.open_grid_lots_table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents)
|
||||
self.open_grid_lots_table.horizontalHeader().setStretchLastSection(True)
|
||||
self.open_grid_lots_table.setEditTriggers(QTableWidget.EditTrigger.NoEditTriggers)
|
||||
self._configure_table(self.open_grid_lots_table)
|
||||
open_grid_layout.addWidget(self.open_grid_lots_table)
|
||||
|
||||
grid_match_tab = QWidget()
|
||||
grid_match_layout = QVBoxLayout(grid_match_tab)
|
||||
grid_match_layout.setContentsMargins(10, 10, 10, 10)
|
||||
grid_match_layout.setSpacing(8)
|
||||
self.grid_matches_table = QTableWidget(0, len(self.GRID_MATCH_COLUMNS))
|
||||
self.grid_matches_table.setHorizontalHeaderLabels(self.GRID_MATCH_COLUMNS)
|
||||
self._configure_table(self.grid_matches_table)
|
||||
grid_match_layout.addWidget(self.grid_matches_table)
|
||||
|
||||
trade_tab = QWidget()
|
||||
trade_layout = QVBoxLayout(trade_tab)
|
||||
trade_layout.setContentsMargins(10, 10, 10, 10)
|
||||
trade_layout.setSpacing(8)
|
||||
trade_buttons = QHBoxLayout()
|
||||
edit_trade_button = QPushButton("编辑成交")
|
||||
edit_trade_button.setObjectName("toolbarButton")
|
||||
edit_trade_button.clicked.connect(self._edit_selected_trade)
|
||||
delete_trade_button = QPushButton("删除成交")
|
||||
delete_trade_button.setObjectName("toolbarButton")
|
||||
delete_trade_button.clicked.connect(self._delete_selected_trade)
|
||||
trade_buttons.addWidget(edit_trade_button)
|
||||
trade_buttons.addWidget(delete_trade_button)
|
||||
trade_buttons.addStretch()
|
||||
self.trades_table = QTableWidget(0, len(self.TRADE_COLUMNS))
|
||||
self.trades_table.setHorizontalHeaderLabels(self.TRADE_COLUMNS)
|
||||
self.trades_table.horizontalHeader().setStretchLastSection(True)
|
||||
self.trades_table.setSelectionBehavior(QTableWidget.SelectionBehavior.SelectRows)
|
||||
self.trades_table.setEditTriggers(QTableWidget.EditTrigger.NoEditTriggers)
|
||||
self._configure_table(self.trades_table)
|
||||
trade_layout.addLayout(trade_buttons)
|
||||
trade_layout.addWidget(self.trades_table)
|
||||
|
||||
self.details_tabs.addTab(grid_tab, "网格档位")
|
||||
self.details_tabs.addTab(open_grid_tab, "待卖网格")
|
||||
self.details_tabs.addTab(grid_match_tab, "配对明细")
|
||||
self.details_tabs.addTab(trade_tab, "最近成交")
|
||||
self.details_tabs.setCurrentIndex(1)
|
||||
splitter.addWidget(self.details_tabs)
|
||||
@@ -188,12 +233,171 @@ class MainWindow(QMainWindow):
|
||||
root_layout.addWidget(content)
|
||||
self.setCentralWidget(root)
|
||||
|
||||
def _apply_light_theme(self) -> None:
|
||||
self.setStyleSheet(
|
||||
"""
|
||||
QMainWindow {
|
||||
background: #f4f7fb;
|
||||
color: #334155;
|
||||
font-family: "Microsoft YaHei", "Segoe UI", Arial, sans-serif;
|
||||
font-size: 13px;
|
||||
}
|
||||
QWidget#appRoot {
|
||||
background: #f4f7fb;
|
||||
}
|
||||
QListWidget#sideNav {
|
||||
background: #ffffff;
|
||||
border: 1px solid #dbe4ef;
|
||||
border-radius: 8px;
|
||||
padding: 8px;
|
||||
outline: 0;
|
||||
}
|
||||
QListWidget#sideNav::item {
|
||||
border-radius: 6px;
|
||||
color: #475569;
|
||||
margin: 2px 0;
|
||||
padding: 10px 12px;
|
||||
}
|
||||
QListWidget#sideNav::item:selected {
|
||||
background: #e8f1ff;
|
||||
color: #1d4ed8;
|
||||
font-weight: 700;
|
||||
}
|
||||
QFrame#summaryCard {
|
||||
background: #ffffff;
|
||||
border: 1px solid #dbe4ef;
|
||||
border-radius: 8px;
|
||||
}
|
||||
QLabel#summaryTitle {
|
||||
color: #64748b;
|
||||
font-size: 12px;
|
||||
}
|
||||
QLabel#summaryValue {
|
||||
color: #0f172a;
|
||||
font-size: 20px;
|
||||
font-weight: 700;
|
||||
}
|
||||
QPushButton#toolbarButton {
|
||||
background: #2563eb;
|
||||
border: 1px solid #1d4ed8;
|
||||
border-radius: 6px;
|
||||
color: #ffffff;
|
||||
font-weight: 700;
|
||||
min-height: 30px;
|
||||
padding: 6px 12px;
|
||||
}
|
||||
QPushButton#toolbarButton:hover {
|
||||
background: #1d4ed8;
|
||||
}
|
||||
QPushButton#toolbarButton:pressed {
|
||||
background: #1e40af;
|
||||
}
|
||||
QPushButton#toolbarButton:disabled {
|
||||
background: #cbd5e1;
|
||||
border-color: #cbd5e1;
|
||||
color: #f8fafc;
|
||||
}
|
||||
QTabWidget#detailsTabs::pane {
|
||||
background: #ffffff;
|
||||
border: 1px solid #dbe4ef;
|
||||
border-radius: 8px;
|
||||
top: -1px;
|
||||
}
|
||||
QTabBar::tab {
|
||||
background: #eef2f7;
|
||||
border: 1px solid #dbe4ef;
|
||||
border-bottom: 0;
|
||||
border-top-left-radius: 6px;
|
||||
border-top-right-radius: 6px;
|
||||
color: #475569;
|
||||
margin-right: 4px;
|
||||
padding: 8px 14px;
|
||||
}
|
||||
QTabBar::tab:selected {
|
||||
background: #ffffff;
|
||||
color: #1d4ed8;
|
||||
font-weight: 700;
|
||||
}
|
||||
QTableWidget {
|
||||
background: #ffffff;
|
||||
alternate-background-color: #f8fafc;
|
||||
border: 1px solid #dbe4ef;
|
||||
border-radius: 8px;
|
||||
gridline-color: #e2e8f0;
|
||||
selection-background-color: #dbeafe;
|
||||
selection-color: #0f172a;
|
||||
}
|
||||
QHeaderView::section {
|
||||
background: #f1f5f9;
|
||||
border: 0;
|
||||
border-right: 1px solid #dbe4ef;
|
||||
border-bottom: 1px solid #dbe4ef;
|
||||
color: #475569;
|
||||
font-weight: 700;
|
||||
padding: 8px 10px;
|
||||
}
|
||||
QSpinBox {
|
||||
background: #ffffff;
|
||||
border: 1px solid #cbd5e1;
|
||||
border-radius: 6px;
|
||||
min-height: 28px;
|
||||
padding: 2px 8px;
|
||||
}
|
||||
QLabel {
|
||||
color: #334155;
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
def _configure_table(self, table: QTableWidget) -> None:
|
||||
table.setAlternatingRowColors(True)
|
||||
table.setEditTriggers(QTableWidget.EditTrigger.NoEditTriggers)
|
||||
table.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)
|
||||
table.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
|
||||
table.setShowGrid(True)
|
||||
table.setWordWrap(False)
|
||||
table.verticalHeader().setVisible(False)
|
||||
table.verticalHeader().setDefaultSectionSize(34)
|
||||
table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents)
|
||||
table.horizontalHeader().setStretchLastSection(True)
|
||||
|
||||
def _make_item(self, value: str, pnl_value: Decimal | None = None) -> QTableWidgetItem:
|
||||
item = QTableWidgetItem(value)
|
||||
if pnl_value is not None:
|
||||
self._style_pnl_item(item, pnl_value)
|
||||
return item
|
||||
|
||||
def _style_pnl_item(self, item: QTableWidgetItem, value: Decimal | None) -> None:
|
||||
if value is None:
|
||||
return
|
||||
|
||||
font = QFont(item.font())
|
||||
if value > 0:
|
||||
item.setForeground(QBrush(self.PROFIT_COLOR))
|
||||
font.setBold(True)
|
||||
elif value < 0:
|
||||
item.setForeground(QBrush(self.LOSS_COLOR))
|
||||
font.setBold(True)
|
||||
else:
|
||||
font.setBold(False)
|
||||
item.setFont(font)
|
||||
|
||||
def _style_pnl_label(self, label: QLabel, value: Decimal | None) -> None:
|
||||
if value is None or value == 0:
|
||||
label.setStyleSheet(f"color: {self.NEUTRAL_COLOR.name()}; font-weight: 400;")
|
||||
elif value > 0:
|
||||
label.setStyleSheet(f"color: {self.PROFIT_COLOR.name()}; font-weight: 700;")
|
||||
else:
|
||||
label.setStyleSheet(f"color: {self.LOSS_COLOR.name()}; font-weight: 700;")
|
||||
|
||||
def _create_summary_cards(self) -> dict[str, QLabel]:
|
||||
self.summary_cards_layout = QGridLayout()
|
||||
self.summary_cards_layout.setHorizontalSpacing(10)
|
||||
self.summary_cards_layout.setVerticalSpacing(10)
|
||||
labels: dict[str, QLabel] = {}
|
||||
for column, (key, title) in enumerate(
|
||||
[
|
||||
("total_assets", "总资产"),
|
||||
("total_assets", "账户权益"),
|
||||
("cash", "现金"),
|
||||
("market_value", "持仓市值"),
|
||||
("floating_pnl", "浮动盈亏"),
|
||||
@@ -201,11 +405,15 @@ class MainWindow(QMainWindow):
|
||||
]
|
||||
):
|
||||
frame = QFrame()
|
||||
frame.setObjectName("summaryCard")
|
||||
frame.setFrameShape(QFrame.Shape.StyledPanel)
|
||||
layout = QVBoxLayout(frame)
|
||||
layout.setContentsMargins(14, 12, 14, 12)
|
||||
layout.setSpacing(6)
|
||||
title_label = QLabel(title)
|
||||
title_label.setObjectName("summaryTitle")
|
||||
value_label = QLabel("-")
|
||||
value_label.setStyleSheet("font-size: 20px; font-weight: 700;")
|
||||
value_label.setObjectName("summaryValue")
|
||||
layout.addWidget(title_label)
|
||||
layout.addWidget(value_label)
|
||||
labels[key] = value_label
|
||||
@@ -223,6 +431,7 @@ class MainWindow(QMainWindow):
|
||||
]
|
||||
for text, handler in buttons:
|
||||
button = QPushButton(text)
|
||||
button.setObjectName("toolbarButton")
|
||||
button.clicked.connect(handler)
|
||||
if text == "刷新行情":
|
||||
self.refresh_quotes_button = button
|
||||
@@ -241,6 +450,7 @@ class MainWindow(QMainWindow):
|
||||
self.summary_labels["cash"].setText(format_money(account_summary.cash))
|
||||
self.summary_labels["market_value"].setText(format_money(account_summary.market_value))
|
||||
self.summary_labels["floating_pnl"].setText(format_money(account_summary.floating_pnl))
|
||||
self._style_pnl_label(self.summary_labels["floating_pnl"], account_summary.floating_pnl)
|
||||
self.summary_labels["usage"].setText(format_percent(account_summary.capital_usage_rate))
|
||||
self._fill_holdings_table()
|
||||
self._refresh_details()
|
||||
@@ -309,8 +519,13 @@ class MainWindow(QMainWindow):
|
||||
format_money(position.grid_profit),
|
||||
format_money(position.floating_pnl),
|
||||
]
|
||||
pnl_values = {
|
||||
10: position.realized_pnl,
|
||||
11: position.grid_profit,
|
||||
12: position.floating_pnl,
|
||||
}
|
||||
for column, value in enumerate(values):
|
||||
item = QTableWidgetItem(value)
|
||||
item = self._make_item(value, pnl_values.get(column))
|
||||
item.setData(Qt.ItemDataRole.UserRole, position.instrument_id)
|
||||
self.holdings_table.setItem(row, column, item)
|
||||
if self._positions and self.holdings_table.currentRow() < 0:
|
||||
@@ -327,10 +542,12 @@ class MainWindow(QMainWindow):
|
||||
if position is None:
|
||||
self._refresh_grid_levels(None)
|
||||
self._refresh_open_grid_lots(None)
|
||||
self._refresh_grid_matches(None)
|
||||
self._fill_trades_table([])
|
||||
return
|
||||
self._refresh_grid_levels(position)
|
||||
self._refresh_open_grid_lots(position)
|
||||
self._refresh_grid_matches(position)
|
||||
self._fill_trades_table(self.service.list_trades(instrument_id=position.instrument_id))
|
||||
|
||||
def _refresh_grid_levels(self, position: PositionSummary | None) -> None:
|
||||
@@ -368,7 +585,11 @@ class MainWindow(QMainWindow):
|
||||
format_money(level.estimated_gross_profit),
|
||||
]
|
||||
for column, value in enumerate(values):
|
||||
self.grid_levels_table.setItem(row, column, QTableWidgetItem(value))
|
||||
item = self._make_item(
|
||||
value,
|
||||
level.estimated_gross_profit if column == 6 else None,
|
||||
)
|
||||
self.grid_levels_table.setItem(row, column, item)
|
||||
|
||||
def _refresh_open_grid_lots(self, position: PositionSummary | None) -> None:
|
||||
if position is None:
|
||||
@@ -396,7 +617,41 @@ class MainWindow(QMainWindow):
|
||||
lot.status,
|
||||
]
|
||||
for column, value in enumerate(values):
|
||||
self.open_grid_lots_table.setItem(row, column, QTableWidgetItem(value))
|
||||
item = self._make_item(
|
||||
value,
|
||||
lot.estimated_gross_profit if column == 5 else None,
|
||||
)
|
||||
self.open_grid_lots_table.setItem(row, column, item)
|
||||
|
||||
def _refresh_grid_matches(self, position: PositionSummary | None) -> None:
|
||||
if position is None:
|
||||
self._fill_grid_matches_table([])
|
||||
return
|
||||
try:
|
||||
matches = self.service.get_grid_trade_matches(position.instrument_id)
|
||||
except Exception as exc:
|
||||
QMessageBox.warning(self, "配对明细失败", str(exc))
|
||||
self._fill_grid_matches_table([])
|
||||
return
|
||||
self._fill_grid_matches_table(matches)
|
||||
|
||||
def _fill_grid_matches_table(self, matches: list[GridTradeMatch]) -> None:
|
||||
self.grid_matches_table.setRowCount(len(matches))
|
||||
for row, match in enumerate(matches):
|
||||
values = [
|
||||
match.sell_date.isoformat(),
|
||||
format_price(match.sell_price),
|
||||
match.buy_date.isoformat(),
|
||||
format_price(match.buy_price),
|
||||
format_quantity(match.matched_quantity),
|
||||
format_money(match.buy_amount),
|
||||
format_money(match.sell_amount),
|
||||
format_money(match.gross_profit),
|
||||
f"买{match.buy_trade_id or '-'} -> 卖{match.sell_trade_id or '-'}",
|
||||
]
|
||||
for column, value in enumerate(values):
|
||||
item = self._make_item(value, match.gross_profit if column == 7 else None)
|
||||
self.grid_matches_table.setItem(row, column, item)
|
||||
|
||||
def _fill_trades_table(self, trades: list[Trade]) -> None:
|
||||
self._trade_ids_by_row = {}
|
||||
|
||||
Reference in New Issue
Block a user