stock_system/src/trading/portfolio.py

65 lines
2.1 KiB
Python

from typing import List, Dict
from dataclasses import dataclass
from datetime import datetime
@dataclass
class Position:
symbol: str
entry_date: datetime
entry_price: float
shares: int
stop_loss: float
target_price: float
@property
def current_value(self) -> float:
# TODO: Implement real-time price fetching
return self.shares * self.entry_price
@property
def potential_profit(self) -> float:
"""Calculate potential profit at target price"""
return (self.target_price - self.entry_price) * self.shares
@property
def potential_loss(self) -> float:
"""Calculate potential loss at stop loss"""
return (self.stop_loss - self.entry_price) * self.shares
@property
def risk_reward_ratio(self) -> float:
"""Calculate risk/reward ratio"""
potential_gain = self.target_price - self.entry_price
potential_risk = self.entry_price - self.stop_loss
return abs(potential_gain / potential_risk) if potential_risk != 0 else 0
class Portfolio:
def __init__(self):
self.positions: List[Position] = []
def add_position(self, position: Position):
"""Add a new position to the portfolio"""
self.positions.append(position)
def remove_position(self, symbol: str):
"""Remove a position from the portfolio by symbol"""
self.positions = [p for p in self.positions if p.symbol != symbol]
def get_position_summary(self) -> List[Dict]:
"""Get summary of all positions"""
return [
{
"symbol": p.symbol,
"entry_date": p.entry_date,
"entry_price": p.entry_price,
"shares": p.shares,
"current_value": p.current_value,
"stop_loss": p.stop_loss,
"target_price": p.target_price,
"potential_profit": p.potential_profit,
"potential_loss": p.potential_loss,
"risk_reward_ratio": p.risk_reward_ratio
}
for p in self.positions
]