import re from bfxbot.bfxwrapper import Balance from bfxbot.models import PositionWrapper class CurrencyPair: def __init__(self, base: str, quote: str): self.base: str = base self.quote: str = quote @staticmethod def from_str(string: str): symbol_regex = re.compile("t(?P[a-zA-Z]{3})(?P[a-zA-Z]{3})") match = symbol_regex.match(string) if not match: return None return CurrencyPair(match.group("base"), match.group("quote")) def net_pl_percentage(perc: float, reference_fee_perc: float): return perc - reference_fee_perc def balance_to_json(balance: Balance): return { 'currency': balance.currency, 'amount': balance.amount, 'kind': balance.kind.value, 'quote': balance.quote, 'quote_equivalent': balance.quote_equivalent } def pw_to_posprop(pw: PositionWrapper): pair = CurrencyPair.from_str(pw.position.symbol) if not pair: raise ValueError return { "id": pw.position.id, "amount": pw.position.amount, "base_price": pw.position.base_price, "state": str(pw.state()), "pair": { "base": pair.base, "quote": pair.quote }, "profit_loss": pw.net_profit_loss(), "profit_loss_percentage": pw.net_profit_loss_percentage() }