Coverage for utils / transaction_cost_utils.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-04 15:08 +0000

1class TransactionCostUtils: 

2 """거래 비용(수수료 및 세금) 관리 클래스""" 

3 # 수수료율 (KRX/NXT 구분 없이 기본값 적용: 0.0140527%) 

4 FEE_RATE = 0.000140527 

5 # 증권거래세 + 농어촌특별세 (0.20%) 

6 TAX_RATE = 0.002 

7 

8 @classmethod 

9 def calculate_cost(cls, price: float, qty: float, is_sell: bool = False) -> float: 

10 """거래 비용 계산 (매수: 수수료, 매도: 수수료 + 세금)""" 

11 amount = price * qty 

12 fee = amount * cls.FEE_RATE 

13 tax = amount * cls.TAX_RATE if is_sell else 0.0 

14 return fee + tax 

15 

16 @classmethod 

17 def get_return_rate(cls, buy_price: float, sell_price: float, qty: float = 1, apply_cost: bool = False) -> float: 

18 """수익률 계산 (비용 적용 옵션)""" 

19 if buy_price == 0: 

20 return 0.0 

21 

22 if not apply_cost: 

23 return ((sell_price - buy_price) / buy_price) * 100 

24 

25 buy_cost = cls.calculate_cost(buy_price, qty, is_sell=False) 

26 sell_cost = cls.calculate_cost(sell_price, qty, is_sell=True) 

27 

28 total_invest = (buy_price * qty) + buy_cost 

29 total_retrieve = (sell_price * qty) - sell_cost 

30 

31 return ((total_retrieve - total_invest) / total_invest) * 100