67 lines
3.3 KiB
Python
67 lines
3.3 KiB
Python
from PySide6.QtWidgets import QWidget, QLabel, QVBoxLayout, QTableWidget, QTableWidgetItem, QHeaderView
|
|
from PySide6.QtCore import Qt
|
|
from storage.models import Position
|
|
|
|
class PositionsPanel(QWidget):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.init_ui()
|
|
self.position_rows = {} # To keep track of rows by symbol
|
|
|
|
def init_ui(self):
|
|
layout = QVBoxLayout(self)
|
|
|
|
# Title
|
|
title_label = QLabel("Positions")
|
|
title_label.setAlignment(Qt.AlignCenter)
|
|
title_label.setStyleSheet("font-size: 24px; font-weight: bold; margin-bottom: 20px;")
|
|
layout.addWidget(title_label)
|
|
|
|
self.positions_table = QTableWidget()
|
|
self.positions_table.setColumnCount(6)
|
|
self.positions_table.setHorizontalHeaderLabels(["Symbol", "Quantity", "Average Price", "Current Price", "Market Value", "Unrealized P&L"])
|
|
self.positions_table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
|
|
layout.addWidget(self.positions_table)
|
|
|
|
layout.addStretch(1) # Pushes content to the top
|
|
self.setLayout(layout)
|
|
|
|
def add_or_update_position(self, position: Position):
|
|
if not position.is_open: # Remove closed positions from the UI
|
|
if position.symbol in self.position_rows:
|
|
row_to_remove = self.position_rows.pop(position.symbol)
|
|
self.positions_table.removeRow(row_to_remove)
|
|
# Update row indices after removal
|
|
for symbol, row_idx in self.position_rows.items():
|
|
if row_idx > row_to_remove:
|
|
self.position_rows[symbol] = row_idx - 1
|
|
return
|
|
|
|
if position.symbol in self.position_rows:
|
|
row_position = self.position_rows[position.symbol]
|
|
else:
|
|
row_position = self.positions_table.rowCount()
|
|
self.positions_table.insertRow(row_position)
|
|
self.position_rows[position.symbol] = row_position
|
|
|
|
self.positions_table.setItem(row_position, 0, QTableWidgetItem(position.symbol))
|
|
self.positions_table.setItem(row_position, 1, QTableWidgetItem(str(position.quantity)))
|
|
self.positions_table.setItem(row_position, 2, QTableWidgetItem(str(position.average_price)))
|
|
self.positions_table.setItem(row_position, 3, QTableWidgetItem(str(position.current_price)))
|
|
self.positions_table.setItem(row_position, 4, QTableWidgetItem(str(position.market_value)))
|
|
self.positions_table.setItem(row_position, 5, QTableWidgetItem(str(position.unrealized_pnl)))
|
|
|
|
def update_position(self, symbol, quantity, avg_price, current_price, market_value, unrealized_pnl):
|
|
# This method is now a compatibility wrapper or can be removed if not used elsewhere
|
|
# For now, create a dummy Position object and call add_or_update_position
|
|
from storage.models import Position as DummyPosition # Use alias to avoid re-importing
|
|
dummy_position = DummyPosition(
|
|
symbol=symbol,
|
|
quantity=quantity,
|
|
average_price=avg_price,
|
|
current_price=current_price,
|
|
market_value=market_value,
|
|
unrealized_pnl=unrealized_pnl,
|
|
is_open=True # Assuming update_position is for open positions
|
|
)
|
|
self.add_or_update_position(dummy_position) |