diff options
author | uvok | 2025-04-14 20:42:05 +0200 |
---|---|---|
committer | uvok | 2025-04-14 20:42:05 +0200 |
commit | 5e99acb7c2b0c3b7106a2ed0ac39ce00d8b335eb (patch) | |
tree | 3bac9e0865f9b101825e251bf5ec183ecfb5bc76 /trade.py | |
parent | bc3400ee34ac945f708e483980348ba95b5a8c9a (diff) |
Use decimal for Trades
Diffstat (limited to 'trade.py')
-rw-r--r-- | trade.py | 28 |
1 files changed, 15 insertions, 13 deletions
@@ -1,31 +1,33 @@ +from decimal import Decimal + class Trade: """ Represents a cryptocurrency trade, including the amount traded, total cost, and the date of trade. Provides methods to modify the trade and access various attributes. """ - def __init__(self, amount: float, total_cost: float, date: str) -> None: + def __init__(self, amount: Decimal, total_cost: Decimal, date: str) -> None: """ Initialize a new Trade instance. Args: - amount (float): The amount of cryptocurrency traded. - total_cost (float): The total cost of the trade. + amount (Decimal): The amount of cryptocurrency traded. + total_cost (Decimal): The total cost of the trade. date (str): The date of the trade, formatted as a string. """ if amount <= 0 or total_cost <= 0: raise ValueError("Amount and total cost must be > 0") - self.__amount: float = amount - self.__total_cost: float = total_cost + self.__amount: Decimal = amount + self.__total_cost: Decimal = total_cost self.__date: str = date - def remove_coins(self, amount: float) -> None: + def remove_coins(self, amount: Decimal) -> None: """ Reduce the amount of cryptocurrency in the trade by a specified amount. Args: - amount (float): The amount of cryptocurrency to remove. + amount (Decimal): The amount of cryptocurrency to remove. Raises: ValueError: If the amount to remove exceeds the current amount in the trade. @@ -37,22 +39,22 @@ class Trade: self.__amount -= amount @property - def amount(self) -> float: + def amount(self) -> Decimal: """ Get the current amount of cryptocurrency in the trade. Returns: - float: The amount of cryptocurrency. + Decimal: The amount of cryptocurrency. """ return self.__amount @property - def total_cost(self) -> float: + def total_cost(self) -> Decimal: """ Get the total cost of the trade. Returns: - float: The total cost of the trade. + Decimal: The total cost of the trade. """ return self.__total_cost @@ -67,12 +69,12 @@ class Trade: return self.__date @property - def price_per_coin(self) -> float: + def price_per_coin(self) -> Decimal: """ Calculate the price per coin based on the total cost and current amount. Returns: - float: The price per coin. + Decimal: The price per coin. Raises: ZeroDivisionError: If the current amount is zero. |