159 lines
3.6 KiB
Python
159 lines
3.6 KiB
Python
import re
|
|
from enum import Enum
|
|
from typing import Optional, List
|
|
|
|
|
|
class Symbol(Enum):
|
|
XMR = "XMR"
|
|
BTC = "BTC"
|
|
ETH = "ETH"
|
|
USD = "USD"
|
|
|
|
def __repr__(self):
|
|
return self.__str__()
|
|
|
|
def __str__(self):
|
|
return self.value
|
|
|
|
def __eq__(self, other):
|
|
return self.value == other.value
|
|
|
|
|
|
class TradingPair(Enum):
|
|
XMR = "XMR"
|
|
BTC = "BTC"
|
|
ETH = "ETH"
|
|
|
|
def __repr__(self):
|
|
return f"t{self.value}USD"
|
|
|
|
def __str__(self):
|
|
return self.__repr__()
|
|
|
|
def __eq__(self, other):
|
|
return self.value == other.value
|
|
|
|
def __hash__(self):
|
|
return hash(self.__repr__())
|
|
|
|
@staticmethod
|
|
def from_str(string: str):
|
|
match = re.compile("t([a-zA-Z]+)USD").match(string)
|
|
|
|
if not match:
|
|
raise ValueError
|
|
|
|
currency = match.group(1).lower()
|
|
|
|
if currency in ("xmr"):
|
|
return TradingPair.XMR
|
|
elif currency in ("btc"):
|
|
return TradingPair.BTC
|
|
elif currency in ("eth"):
|
|
return TradingPair.ETH
|
|
else:
|
|
return NotImplementedError
|
|
|
|
|
|
class Currency:
|
|
def __init__(self, name: str, amount: float, price: float = None):
|
|
self.__name: str = name
|
|
self.__amount: float = amount
|
|
self.__price: float = price
|
|
|
|
def __str__(self):
|
|
return f"{self.__name} {self.__amount} @ {self.__price}"
|
|
|
|
def __repr__(self):
|
|
return self.__str__()
|
|
|
|
def amount(self) -> float:
|
|
return self.__amount
|
|
|
|
def name(self) -> str:
|
|
return self.__name
|
|
|
|
def price(self) -> Optional[float]:
|
|
return self.__price
|
|
|
|
|
|
class WalletKind(Enum):
|
|
EXCHANGE = "exchange",
|
|
MARGIN = "margin"
|
|
|
|
@staticmethod
|
|
def from_str(string: str):
|
|
string = string.lower()
|
|
|
|
if "margin" in string:
|
|
return WalletKind.MARGIN
|
|
if "exchange" in string:
|
|
return WalletKind.EXCHANGE
|
|
|
|
return None
|
|
|
|
|
|
class Balance:
|
|
def __init__(self, currency: Currency, quote: Symbol, wallet: Optional[WalletKind] = None):
|
|
self.__currency: Currency = currency
|
|
self.__quote: Symbol = quote
|
|
self.__quote_equivalent: float = 0.0
|
|
self.__wallet: Optional[WalletKind] = wallet
|
|
|
|
if currency.name == quote.value:
|
|
self.__quote_equivalent = currency.amount()
|
|
else:
|
|
self.__quote_equivalent = currency.amount() * currency.price()
|
|
|
|
def currency(self) -> Currency:
|
|
return self.__currency
|
|
|
|
def quote(self) -> Symbol:
|
|
return self.__quote
|
|
|
|
def quote_equivalent(self) -> float:
|
|
return self.__quote_equivalent
|
|
|
|
def wallet(self) -> Optional[WalletKind]:
|
|
return self.__wallet
|
|
|
|
|
|
class BalanceGroup:
|
|
def __init__(self, quote: Symbol, balances: Optional[List[Balance]] = None):
|
|
if balances is None:
|
|
balances = []
|
|
|
|
self.__quote: Symbol = quote
|
|
self.__balances: Optional[List[Balance]] = balances
|
|
self.__quote_equivalent: float = 0.0
|
|
|
|
def __iter__(self):
|
|
return self.__balances.__iter__()
|
|
|
|
def add_balance(self, balance: Balance):
|
|
self.__balances.append(balance)
|
|
|
|
self.__quote_equivalent += balance.quote_equivalent()
|
|
|
|
def balances(self) -> Optional[List[Balance]]:
|
|
return self.__balances
|
|
|
|
def currency_names(self) -> List[str]:
|
|
return list(map(lambda x: x.name, self.balances()))
|
|
|
|
def quote(self) -> Symbol:
|
|
return self.__quote
|
|
|
|
def quote_equivalent(self) -> float:
|
|
return self.__quote_equivalent
|
|
|
|
|
|
class Direction(Enum):
|
|
UP = 1,
|
|
DOWN = -1
|
|
|
|
|
|
class OrderType(Enum):
|
|
EXCHANGE = "EXCHANGE",
|
|
MARGIN = "MARGIN"
|