Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

Commit

Permalink
Threshold rebalancing (#37)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Jamie Wade authored Aug 31, 2021
1 parent 33ea8f3 commit 1609557
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
43 changes: 37 additions & 6 deletions PieBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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.

Expand Down
5 changes: 5 additions & 0 deletions _config-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 1609557

Please sign in to comment.