Skip to content

Commit

Permalink
General simplifications and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Jan 16, 2024
1 parent 9a6bb47 commit d4dede1
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 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.0'
self.bot_version = '1.4.1'
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 @@ -206,7 +206,7 @@ def set_price(ccxt_price: float = None, price: float = None):

def fetch_mayer(tries: int = 0):
try:
req = requests.get('https://bitcoinition.com/current.json')
req = requests.get('https://bitcoinition.com/current.json', timeout=10)
if req.text:
mayer = req.json()['data']
return {'current': float(mayer['current_mayer_multiple']),
Expand Down Expand Up @@ -553,11 +553,10 @@ def append_balances(part: dict, margin_balance: float, daily: bool):
append_actual_quote(part, price)
append_margin_leverage(part)
part['labels'].append("Target Position {}".format(CONF.quote))
target_position = NA
if CONF.exchange == 'bitmex':
target_position = round(calculate_target_position(calculate_target_quote(), price))
part['mail'].append("Target position {}: {:>{}}".format(CONF.quote, target_position, 17-len(CONF.quote)))
else:
target_position = 'n/a'
part['csv'].append("{}".format(target_position))
append_liquidation_price(part)

Expand Down Expand Up @@ -598,22 +597,20 @@ def append_margin_change(part: dict, today: dict):
part['labels'].append("Position {}".format(CONF.quote))
part['labels'].append("Change %")
m_bal = "Margin balance {}: {:>{}.4f}".format(CONF.base, today['mBal'], 18-len(CONF.base))
change = NA
if 'mBalChan24' in today:
change = "{:+.2f}%".format(today['mBalChan24'])
m_bal += " (" + change + ")*"
change = "{:+.2f}".format(today['mBalChan24'])
else:
change = NA
part['mail'].append(m_bal)
part['csv'].append("{:.4f};{}".format(today['mBal'], change))

fm_bal = "Position {}: {:>{}}".format(CONF.quote, round(today['fmBal']), 24-len(CONF.quote))
change = NA
if 'fmBalChan24' in today:
change = "{:+.2f}%".format(today['fmBalChan24'])
fm_bal += " (" + change + ")*"
change = "{:+.2f}".format(today['fmBalChan24'])
else:
change = NA
part['mail'].append(fm_bal)
part['csv'].append("{};{}".format(round(today['fmBal']), change))

Expand All @@ -627,34 +624,31 @@ def append_balance_change(part: dict, today: dict):
part['labels'].append("Balance {}".format(CONF.quote))
part['labels'].append("Change %")
m_bal = "Balance {}: {:>{}.4f}".format(CONF.base, today['mBal'], 25-len(CONF.base))
change = NA
if 'mBalChan24' in today:
change = "{:+.2f}%".format(today['mBalChan24'])
m_bal += " (" + change + ")*"
change = "{:+.2f}".format(today['mBalChan24'])
else:
change = NA
part['mail'].append(m_bal)
part['csv'].append("{:.4f};{}".format(today['mBal'], change))
fm_bal = "Balance {}: {:>{}}".format(CONF.quote, round(today['fmBal']), 25-len(CONF.quote))
change = NA
if 'fmBalChan24' in today:
change = "{:+.2f}%".format(today['fmBalChan24'])
fm_bal += " (" + change + ")*"
change = "{:+.2f}".format(today['fmBalChan24'])
else:
change = NA
part['mail'].append(fm_bal)
part['csv'].append("{};{}".format(round(today['fmBal']), change))


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:
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
else:
change = NA
if change != NA:
part['mail'].append("Value change: {:>21}%*".format(change))
else:
Expand All @@ -664,11 +658,10 @@ def append_value_change(part: dict, today: dict, yesterday: dict, price: float):

def append_trading_result(part: dict, today: dict, yesterday: dict, price: float):
part['labels'].append("Trading Result {}".format(CONF.quote))
trading_result = NA
if yesterday and 'mBal' in today and 'fmBal' in today:
trading_result = (today['mBal'] - yesterday['mBal']) * price + today['fmBal'] - yesterday['fmBal']
trading_result = "{:+}".format(round(trading_result))
else:
trading_result = NA
part['mail'].append("Trading result in {}: {:>{}}*".format(CONF.quote, trading_result, 15-len(CONF.quote)))
part['csv'].append("{}".format(trading_result))

Expand All @@ -681,12 +674,11 @@ def append_price_change(part: dict, today: dict, price: float):
part['labels'].append("Change %")
padding = 26-len(CONF.base)-len(CONF.quote)
rate = "{} price {}: {:>{}}".format(CONF.base, CONF.quote, round(price), padding)
change = NA
if 'priceChan24' in today:
change = "{:+.2f}%".format(today['priceChan24'])
rate += " (" + change + ")*"
change = "{:+.2f}".format(today['priceChan24'])
else:
change = NA
part['mail'].append(rate)
part['csv'].append("{};{}".format(round(price), change))

Expand Down Expand Up @@ -723,12 +715,12 @@ def append_margin_leverage(part: dict):
def calculate_daily_statistics(m_bal: float, fm_bal: float, price: float, stats: Stats, update_stats: bool):
"""
Calculates, updates and persists the change in the margin balance compared with yesterday
:param m_bal: todays margin balance
:param fm_bal: todays fiat margin balance
:param m_bal: today's margin balance
:param fm_bal: today's fiat margin balance
:param price: the current rate
:param stats: the loaded stats
:param update_stats: update and persists the statistic values
:return: todays statistics including price and margin balance changes compared with 24 hours ago
:return: today's statistics including price and margin balance changes compared with 24 hours ago
"""
today = {'mBal': m_bal, 'fmBal': fm_bal, 'price': price}
if stats is None:
Expand Down Expand Up @@ -1882,8 +1874,6 @@ def check_deposits():
ATTEMPT = 1
if CONF.backtrade_only_on_profit:
LAST_ORDER = ORDER
# we need the values after the trade
BAL = calculate_balances()
do_post_trade_action()
ACTION = None
else:
Expand Down

0 comments on commit d4dede1

Please sign in to comment.