대시보드 수정및 전략수정중
This commit is contained in:
@@ -62,6 +62,51 @@ class PortfolioCache:
|
||||
|
||||
def mark_changed(self):
|
||||
self.change_flag = True
|
||||
|
||||
def apply_buy(self, stock_code: str, stock_name: str, quantity: int, price: float):
|
||||
"""매수 체결 즉시 캐시 반영 — KIS 재동기화 전까지 summary 수치 정확하게 유지"""
|
||||
if self.summary is None:
|
||||
self.mark_changed()
|
||||
return
|
||||
cost = quantity * price
|
||||
# 예수금 차감
|
||||
self.summary["available_cash"] = max(0.0, float(self.summary.get("available_cash", 0)) - cost)
|
||||
# 주식 평가금액 증가
|
||||
self.summary["total_value"] = float(self.summary.get("total_value", 0)) + cost
|
||||
# 매입금액 증가
|
||||
self.summary["total_investment"] = float(self.summary.get("total_investment", 0)) + cost
|
||||
# 총 자산 = 예수금 + 주식평가 (재계산)
|
||||
self.summary["total_assets"] = self.summary["available_cash"] + self.summary["total_value"]
|
||||
# 평가손익 재계산
|
||||
inv = self.summary["total_investment"]
|
||||
val = self.summary["total_value"]
|
||||
self.summary["total_profit_loss"] = val - inv
|
||||
self.summary["total_profit_loss_rate"] = ((val - inv) / inv * 100) if inv > 0 else 0
|
||||
self.summary["total_stocks"] = len([h for h in self.holdings if h.quantity > 0])
|
||||
self.mark_changed()
|
||||
|
||||
def apply_sell(self, stock_code: str, quantity: int, price: float, avg_price: float):
|
||||
"""매도 체결 즉시 캐시 반영"""
|
||||
if self.summary is None:
|
||||
self.mark_changed()
|
||||
return
|
||||
proceeds = quantity * price
|
||||
cost = quantity * avg_price
|
||||
# 예수금 증가
|
||||
self.summary["available_cash"] = float(self.summary.get("available_cash", 0)) + proceeds
|
||||
# 주식 평가금액 감소
|
||||
self.summary["total_value"] = max(0.0, float(self.summary.get("total_value", 0)) - proceeds)
|
||||
# 매입금액 감소
|
||||
self.summary["total_investment"] = max(0.0, float(self.summary.get("total_investment", 0)) - cost)
|
||||
# 총 자산 = 예수금 + 주식평가
|
||||
self.summary["total_assets"] = self.summary["available_cash"] + self.summary["total_value"]
|
||||
# 평가손익 재계산
|
||||
inv = self.summary["total_investment"]
|
||||
val = self.summary["total_value"]
|
||||
self.summary["total_profit_loss"] = val - inv
|
||||
self.summary["total_profit_loss_rate"] = ((val - inv) / inv * 100) if inv > 0 else 0
|
||||
self.summary["total_stocks"] = len([h for h in self.holdings if h.quantity > 0])
|
||||
self.mark_changed()
|
||||
|
||||
def should_sync(self) -> bool:
|
||||
now = datetime.now()
|
||||
@@ -104,7 +149,8 @@ class PortfolioCache:
|
||||
total_stocks = len(holdings)
|
||||
total_value = sum(float(h.current_price or 0) * h.quantity for h in holdings)
|
||||
total_investment = sum(float(h.average_price or 0) * h.quantity for h in holdings)
|
||||
total_profit_loss = sum(float(h.profit_loss or 0) for h in holdings)
|
||||
# 평가손익은 현재가 기준으로 직접 계산 (KIS evlu_pfls_amt는 지연될 수 있음)
|
||||
total_profit_loss = total_value - total_investment
|
||||
total_profit_loss_rate = (total_profit_loss / total_investment * 100) if total_investment > 0 else 0
|
||||
|
||||
# KIS API output2에서 예수금 및 총자산 정보 추출
|
||||
|
||||
Reference in New Issue
Block a user