summaryrefslogtreecommitdiff
path: root/trade.py
blob: 3ceaeb6bb52cd0ab56b49b25b46f93c1425a91cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Trade:
    def __init__(self, amount: float, total_cost: float, date: str) -> None:
        self.__amount: float = amount
        self.__total_cost: float = total_cost
        self.__date: str = date

    def remove_coins(self, amount: float) -> None:
        if amount > self.__amount:
            raise ValueError(f"Can't remove more than {self.__amount}")
        
        self.__total_cost -= amount * self.price_per_coin
        self.__amount -= amount

    @property
    def amount(self) -> float:
        return self.__amount

    @property
    def total_cost(self) -> float:
        return self.__total_cost

    @property
    def date(self) -> str:
        return self.__date

    @property
    def price_per_coin(self) -> float:
        """
        Calculate the price per coin based on the total cost and the current amount
        """
        return self.total_cost / self.amount

    def __repr__(self) -> str:
        return f"Trade(amount={self.amount}, price_per_coin={self.price_per_coin:.2f}, total_cost={self.total_cost:.2f}, date={self.date})"