2020-12-21 12:54:40 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
from bfxbot.bfxwrapper import Balance
|
2020-12-16 11:28:48 +00:00
|
|
|
from bfxbot.models import PositionWrapper
|
|
|
|
|
2020-11-30 14:38:28 +00:00
|
|
|
|
2020-12-21 12:54:40 +00:00
|
|
|
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"))
|
|
|
|
|
|
|
|
|
2020-12-23 17:15:37 +00:00
|
|
|
def average(a):
|
|
|
|
return sum(a) / len(a)
|
2020-12-16 11:28:48 +00:00
|
|
|
|
|
|
|
|
2020-12-21 12:54:40 +00:00
|
|
|
def balance_to_json(balance: Balance):
|
|
|
|
return {
|
2020-12-23 17:15:37 +00:00
|
|
|
'currency': balance.currency().name(),
|
|
|
|
'amount': balance.currency().amount(),
|
|
|
|
'kind': balance.wallet().value,
|
|
|
|
'quote': balance.quote().value,
|
|
|
|
'quote_equivalent': balance.quote_equivalent()
|
2020-12-21 12:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-23 17:15:37 +00:00
|
|
|
def net_pl_percentage(perc: float, reference_fee_perc: float):
|
|
|
|
return perc - reference_fee_perc
|
|
|
|
|
|
|
|
|
2020-12-21 12:54:40 +00:00
|
|
|
def pw_to_posprop(pw: PositionWrapper):
|
|
|
|
pair = CurrencyPair.from_str(pw.position.symbol)
|
|
|
|
|
|
|
|
if not pair:
|
|
|
|
raise ValueError
|
|
|
|
|
2020-12-16 11:28:48 +00:00
|
|
|
return {
|
|
|
|
"id": pw.position.id,
|
|
|
|
"amount": pw.position.amount,
|
|
|
|
"base_price": pw.position.base_price,
|
|
|
|
"state": str(pw.state()),
|
2020-12-21 12:54:40 +00:00
|
|
|
"pair": {
|
|
|
|
"base": pair.base,
|
|
|
|
"quote": pair.quote
|
|
|
|
},
|
2020-12-16 11:28:48 +00:00
|
|
|
"profit_loss": pw.net_profit_loss(),
|
|
|
|
"profit_loss_percentage": pw.net_profit_loss_percentage()
|
|
|
|
}
|