Files
AssetPilot/asset_pilot_docker/app/calculator.py

60 lines
1.9 KiB
Python

from typing import Dict, Optional
class Calculator:
"""손익 계산 클래스"""
@staticmethod
def calc_pnl(
gold_buy_price: float,
gold_quantity: float,
btc_buy_price: float,
btc_quantity: float,
current_gold: Optional[float],
current_btc: Optional[float]
) -> Dict:
"""
금과 BTC의 손익 계산
Returns:
{
"금손익": float,
"금손익%": float,
"BTC손익": float,
"BTC손익%": float,
"총손익": float,
"총손익%": float
}
"""
result = {
"금손익": 0.0,
"금손익%": 0.0,
"BTC손익": 0.0,
"BTC손익%": 0.0,
"총손익": 0.0,
"총손익%": 0.0
}
# 금 손익 계산
if current_gold:
cost_gold = gold_buy_price * gold_quantity
pnl_gold = gold_quantity * (float(current_gold) - gold_buy_price)
result["금손익"] = round(pnl_gold, 0)
if cost_gold > 0:
result["금손익%"] = round((pnl_gold / cost_gold * 100), 2)
# BTC 손익 계산
if current_btc:
cost_btc = btc_buy_price * btc_quantity
pnl_btc = btc_quantity * (float(current_btc) - btc_buy_price)
result["BTC손익"] = round(pnl_btc, 0)
if cost_btc > 0:
result["BTC손익%"] = round((pnl_btc / cost_btc * 100), 2)
# 총 손익 계산
result["총손익"] = result["금손익"] + result["BTC손익"]
total_cost = (gold_buy_price * gold_quantity) + (btc_buy_price * btc_quantity)
if total_cost > 0:
result["총손익%"] = round((result["총손익"] / total_cost * 100), 2)
return result