From 160955741749d4db7c9ec46e8b14afe329a542cf Mon Sep 17 00:00:00 2001 From: Jamie Wade Date: Tue, 31 Aug 2021 08:43:06 +0100 Subject: [PATCH] Threshold rebalancing (#37) * Creates config variable * Comments adjustments * Adds checks for rebalance_threshold * Backwards compatibility * Adds logic for Rebalance thresholds * Writes documentation * Threshold orders should also be greater than or equal to the min_order_value * Corrects default value --- PieBot.py | 43 +++++++++++++++++++++++++++++++++++++------ README.md | 21 ++++++++++++++++++++- _config-example.py | 5 +++++ 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/PieBot.py b/PieBot.py index 251ef34..ec740c0 100644 --- a/PieBot.py +++ b/PieBot.py @@ -7,8 +7,23 @@ pre_flight_checks() +# Hard codes the minimum order value min_order_value = 0.25 +# Checks whether the Rebalance threshold has been defined, and allows the bot to run if it hasn't +try: + rebalance_threshold +except NameError: + rebalance_threshold = None + +if rebalance_threshold is None: + uses_threshold = False +else: + if rebalance_threshold > 0: + uses_threshold = True + else: + uses_threshold = False + # Buy more coins at a regular interval def buy(pairs): @@ -86,21 +101,37 @@ def rebalance(pairs): coin_price = pair[2] pair_value = pair[3] - # If the coin value is over target, sell the excess if it's difference is greater than or equal to the minimum order value + # The coin value is over target if pair_value > target_per_coin: difference = pair_value - target_per_coin if difference >= min_order_value: - order_value = difference / coin_price - sell_orders_data.append([pair[0], pair[1], order_value, difference]) + if uses_threshold: + difference_percentage = (((pair_value - target_per_coin) / target_per_coin) * 100) - # If the coin value is under target, buy more if it's difference is greater than or equal to the minimum order value + if difference_percentage >= (rebalance_threshold * 100): + order_value = difference / coin_price + sell_orders_data.append([pair[0], pair[1], order_value, difference]) + + else: + order_value = difference / coin_price + sell_orders_data.append([pair[0], pair[1], order_value, difference]) + + # The coin value is under target elif pair_value < target_per_coin: difference = target_per_coin - pair_value if difference >= min_order_value: - order_value = difference - buy_orders_data.append([pair[0], pair[1], order_value, difference]) + if uses_threshold: + difference_percentage = (((target_per_coin - pair_value) / pair_value) * 100) + + if difference_percentage >= (rebalance_threshold * 100): + order_value = difference + buy_orders_data.append([pair[0], pair[1], order_value, difference]) + + else: + order_value = difference + buy_orders_data.append([pair[0], pair[1], order_value, difference]) if len(sell_orders_data) >= 1: for order in sell_orders_data: diff --git a/README.md b/README.md index e98935a..a354e2a 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ PieBot is a DCA (Dollar Cost Averaging) cryptocurrency trading bot, built with P - [pair_list](#pair_list) - [buy_frequency](#buy_frequency) - [rebalance_frequency](#rebalance_frequency) + - [rebalance_threshold](#rebalance_threshold) - [buy_order_value](#buy_order_value) - [usdt_reserve](#usdt_reserve) - [Operation](#operation) @@ -152,6 +153,22 @@ You can stop the Rebalance task from running by setting the value as `0`. In thi --- +#### rebalance_threshold + +Sets the desired price deviation a coin must meet before it is rebalanced. + +By specifying a target percentage, rather than just requiring the deviation to be greater than or equal to the minimum order value, you dramatically reduce the number of trades PieBot completes in any given cycle, which can help reduce fees. + +For example, if `rebalance_threshold` is set to `0.025`, then each coin pair must be greater than or equal to 2.5% above or below the target coin price. So, coins that are within the 0% - 2.49999...% window will not be rebalanced in this cycle. + +The value reflects a percentage, and should be between `0` and `1`. + +For example, 5% = `0.05`, 15% = `0.15` etc. + +**Default value** - `0.03` + +--- + #### buy_order_value The USDT value that PieBot will buy for each enabled coin pair in the Buy task. @@ -164,7 +181,9 @@ For example, with 10 enabled coin pairs, and a `buy_order_value` of `0.5`, the B #### usdt_reserve -This value tells PieBot how much USDT it should keep aside to not trade with. The value reflects a percentage, and should be between `0` and `1`. +This value tells PieBot how much USDT it should keep aside to not trade with. + +The value reflects a percentage, and should be between `0` and `1`. For example, 5% = `0.05`, 15% = `0.15` etc. diff --git a/_config-example.py b/_config-example.py index 404533d..75df07d 100644 --- a/_config-example.py +++ b/_config-example.py @@ -20,6 +20,11 @@ buy_frequency = 6 rebalance_frequency = 1 +# The required value deviation before the coin is rebalanced. This is a percentage +# 0.05 = 5% +# 0.15 = 15% +rebalance_threshold = 0.03 + # The USDT value that PieBot will buy for each enabled coin pair in the "Buy" task buy_order_value = 0.50