core/bfxbot/utils.py

54 lines
1.3 KiB
Python

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<base>[a-zA-Z]{3})(?P<quote>[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
}
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()
}