core/bfxbot/currency.py

162 lines
3.7 KiB
Python
Raw Normal View History

2020-11-30 14:38:28 +00:00
import re
from enum import Enum
from typing import Optional, List
2020-11-30 14:38:28 +00:00
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):
2020-11-30 14:38:28 +00:00
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__())
2020-11-30 14:38:28 +00:00
@staticmethod
def from_str(string: str):
match = re.compile("t([a-zA-Z]+)USD").match(string)
2020-11-30 14:38:28 +00:00
if not match:
raise ValueError
currency = match.group(1).lower()
if currency in ("xmr"):
return TradingPair.XMR
2020-11-30 14:38:28 +00:00
elif currency in ("btc"):
return TradingPair.BTC
2020-11-30 14:38:28 +00:00
elif currency in ("eth"):
return TradingPair.ETH
2020-11-30 14:38:28 +00:00
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: Optional[float] = price
def __str__(self):
if self.__price:
return f"{self.__name} {self.__amount} @ {self.__price}"
else:
return f"{self.__name} {self.__amount}"
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() == str(quote):
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.currency().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"