From 2be6700b5ddd85e50da27fda87807d699d5b4d85 Mon Sep 17 00:00:00 2001 From: retgal Date: Tue, 12 Mar 2024 19:03:27 +0100 Subject: [PATCH] New value change calculation for bitmex --- balancer.py | 28 ++++++++++++++++++++++------ balancer_test.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/balancer.py b/balancer.py index c1c3347..9116e46 100755 --- a/balancer.py +++ b/balancer.py @@ -54,7 +54,7 @@ def __init__(self): try: props = config['config'] - self.bot_version = '1.4.3' + self.bot_version = '1.4.4' self.exchange = str(props['exchange']).strip('"').lower() self.api_key = str(props['api_key']).strip('"') self.api_secret = str(props['api_secret']).strip('"') @@ -315,7 +315,7 @@ def create_mail_content(daily: bool = False): """ if not daily: order = ORDER if ORDER else get_closed_order() - trade = ["Last trade", "----------", '\n'.join(create_report_part_trade(order)), '\n\n'] + trade = ["Trade", "----------", '\n'.join(create_report_part_trade(order)), '\n\n'] text = '\n'.join(trade) else: text = '' @@ -461,7 +461,7 @@ def create_report_part_performance(daily: bool): def create_report_part_trade(last_order: Order): - return ["Executed: {:>17}".format(str(last_order))] + return ["{:>17}".format(str(last_order))] def send_mail(subject: str, text: str, attachment: str = None): @@ -537,6 +537,7 @@ def append_balances(part: dict, margin_balance: float, daily: bool): margin_balance_of_fiat = get_margin_balance_of_fiat() today = calculate_daily_statistics(margin_balance, margin_balance_of_fiat['total'], price, stats, daily) append_margin_change(part, today) + append_position_change(part, today) else: c_bal = get_crypto_balance() crypto_total = c_bal['total'] if c_bal else 0 @@ -588,7 +589,7 @@ def append_liquidation_price(part: dict): def append_margin_change(part: dict, today: dict): """ - Appends crypto margin and fiat position and their change (bitmex only) + Appends crypto margin and its change (bitmex only) """ part['labels'].append("Margin {}".format(CONF.base)) part['labels'].append("Change %") @@ -603,6 +604,10 @@ def append_margin_change(part: dict, today: dict): part['mail'].append(m_bal) part['csv'].append("{:.4f};{}".format(today['mBal'], change)) +def append_position_change(part: dict, today: dict): + """ + Appends fiat position and its change (bitmex only) + """ fm_bal = "Position {}: {:>{}}".format(CONF.quote, round(today['fmBal']), 24-len(CONF.quote)) change = NA if 'fmBalChan24' in today: @@ -642,13 +647,24 @@ def append_balance_change(part: dict, today: dict): def append_value_change(part: dict, today: dict, yesterday: dict, price: float): part['labels'].append("Value Change %") change = NA - if yesterday and 'mBal' in today and 'fmBal' in today: + if CONF.exchange == 'bitmex': + if 'mBalChan24' in today and 'priceChan24' in today: + margin_change = today['mBalChan24'] + price_change = today['priceChan24'] + margin_leverage = get_margin_leverage() + if margin_leverage: + margin_leverage = round(margin_leverage * 100) + change = "{:+.4f}".format(margin_change - price_change * margin_leverage / 100) + elif yesterday and 'mBal' in today and 'fmBal' in today: yesterday_total_in_fiat = yesterday['mBal'] * yesterday['price'] + yesterday['fmBal'] today_total_in_fiat = today['mBal'] * price + today['fmBal'] change = "{:+.2f}".format( (today_total_in_fiat / yesterday_total_in_fiat - 1) * 100) if yesterday_total_in_fiat > 0 else NA if change != NA: - part['mail'].append("Value change: {:>21}%*".format(change)) + if CONF.exchange == 'bitmex': + part['mail'].append("Value change: {:>19}%*".format(change)) + else: + part['mail'].append("Value change: {:>21}%*".format(change)) else: part['mail'].append("Value change: {:>21}*".format(change)) part['csv'].append("{}".format(change)) diff --git a/balancer_test.py b/balancer_test.py index c28b19b..6abb4d4 100644 --- a/balancer_test.py +++ b/balancer_test.py @@ -1173,6 +1173,7 @@ def test_append_performance_no_deposits(self): self.assertTrue(csv_part.rfind('n/a') > 0) def test_append_net_change_positive_fiat(self): + balancer.CONF = self.create_default_conf() part = {'mail': [], 'csv': [], 'labels': []} today = {'mBal': 1, 'fmBal': 10100} yesterday = {'mBal': 1, 'fmBal': 10000, 'price': 10000} @@ -1183,6 +1184,7 @@ def test_append_net_change_positive_fiat(self): self.assertEqual('Value Change %', part['labels'][0]) def test_append_net_change_positive_crypto(self): + balancer.CONF = self.create_default_conf() part = {'mail': [], 'csv': [], 'labels': []} today = {'mBal': 1.01, 'fmBal': 10000} yesterday = {'mBal': 1, 'fmBal': 10000, 'price': 10000} @@ -1193,6 +1195,7 @@ def test_append_net_change_positive_crypto(self): self.assertEqual('Value Change %', part['labels'][0]) def test_append_net_change_positive_crypto_by_price(self): + balancer.CONF = self.create_default_conf() part = {'mail': [], 'csv': [], 'labels': []} today = {'mBal': 1, 'fmBal': 4000} yesterday = {'mBal': 1, 'fmBal': 4000, 'price': 10000} @@ -1202,7 +1205,38 @@ def test_append_net_change_positive_crypto_by_price(self): self.assertEqual('+0.50', part['csv'][0]) self.assertEqual('Value Change %', part['labels'][0]) + @patch('balancer.get_margin_leverage') + def test_append_net_change_positive_bitmex(self, mock_get_margin_leverage): + balancer.CONF = self.create_default_conf() + balancer.CONF.exchange = 'bitmex' + balancer.CONF.quote = 'USD' + mock_get_margin_leverage.return_value = 0.02 + part = {'mail': [], 'csv': [], 'labels': []} + today = {'mBalChan24': 0.17, 'priceChan24': 3.61} + yesterday = {} + + balancer.append_value_change(part, today, yesterday, 0) + + self.assertEqual('+0.0978', part['csv'][0]) + self.assertEqual('Value Change %', part['labels'][0]) + + @patch('balancer.get_margin_leverage') + def test_append_net_change_negative_bitmex(self, mock_get_margin_leverage): + balancer.CONF = self.create_default_conf() + balancer.CONF.exchange = 'bitmex' + balancer.CONF.quote = 'USD' + mock_get_margin_leverage.return_value = 0.02 + part = {'mail': [], 'csv': [], 'labels': []} + today = {'mBalChan24': -0.02, 'priceChan24': -0.2} + yesterday = {} + + balancer.append_value_change(part, today, yesterday, 0) + + self.assertEqual('-0.0160', part['csv'][0]) + self.assertEqual('Value Change %', part['labels'][0]) + def test_append_net_change_negative_fiat(self): + balancer.CONF = self.create_default_conf() part = {'mail': [], 'csv': [], 'labels': []} today = {'mBal': 1, 'fmBal': 10000} yesterday = {'mBal': 1, 'fmBal': 10100, 'price': 10000} @@ -1213,6 +1247,7 @@ def test_append_net_change_negative_fiat(self): self.assertEqual('Value Change %', part['labels'][0]) def test_append_net_change_negative_crypto(self): + balancer.CONF = self.create_default_conf() part = {'mail': [], 'csv': [], 'labels': []} today = {'mBal': 1, 'fmBal': 10000} yesterday = {'mBal': 1.01, 'fmBal': 10000, 'price': 10000} @@ -1223,6 +1258,7 @@ def test_append_net_change_negative_crypto(self): self.assertEqual('Value Change %', part['labels'][0]) def test_append_net_change_negative_crypto_by_price(self): + balancer.CONF = self.create_default_conf() part = {'mail': [], 'csv': [], 'labels': []} today = {'mBal': 1, 'fmBal': 10000} yesterday = {'mBal': 1, 'fmBal': 10000, 'price': 10100}