Skip to content

Commit

Permalink
New value change calculation for bitmex
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Mar 12, 2024
1 parent 013e2c6 commit 2be6700
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
28 changes: 22 additions & 6 deletions balancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('"')
Expand Down Expand Up @@ -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 = ''
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 %")
Expand All @@ -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:
Expand Down Expand Up @@ -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))
Expand Down
36 changes: 36 additions & 0 deletions balancer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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}
Expand All @@ -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}
Expand All @@ -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}
Expand All @@ -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}
Expand All @@ -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}
Expand Down

0 comments on commit 2be6700

Please sign in to comment.