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

Commit

Permalink
Performance improvements (#36)
Browse files Browse the repository at this point in the history
* Stores Rebalance order data in a 2D list, rather than using more API calls

* Adjusts timeout times

* Adds garbage collection after each task runs
  • Loading branch information
Jamie Wade authored Aug 29, 2021
1 parent e54e042 commit 5134395
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions PieBot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from functions import *
import gc
import schedule
import signal

Expand Down Expand Up @@ -51,6 +52,8 @@ def buy(pairs):
else:
print(colored("Not enough USDT available", "yellow"))

gc.collect()

print(colored("Waiting to be called...", "cyan"))


Expand All @@ -61,51 +64,49 @@ def rebalance(pairs):
print(colored("Rebalance", "yellow"))
print(colored("Placing orders...", "cyan"))

total_portfolio_value = get_portfolio_value(pairs, False)

# Equally divide the balance by the number of coins, so we know the target value each coin should aim for
target_per_coin = total_portfolio_value / len(pairs)

buy_orders = []
sell_orders = []
total_orders = 0
order_data = []
total_portfolio_value = 0

for pair in pairs:
# Sets null defaults
difference = 0
order_value = 0
pair_value = 0

# Gets the total number of coins for this coin
coin_balance = get_coin_balance(pair[0])

# Gets the current price for this coin
coin_price = get_coin_price(pair[1])

pair_value = coin_balance * coin_price

order_data.append([pair[0], pair[1], coin_price, pair_value])
total_portfolio_value += pair_value

# Equally divide the balance by the number of coins, so we know the target value each coin should aim for
target_per_coin = total_portfolio_value / len(pairs)

buy_orders_data = []
sell_orders_data = []

for pair in order_data:
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
if pair_value > target_per_coin:
difference = pair_value - target_per_coin

if difference >= min_order_value:
order_value = difference / coin_price
sell_orders.append([pair[0], pair[1], order_value, difference])
sell_orders_data.append([pair[0], pair[1], order_value, difference])

# If the coin value is under target, buy more if it's difference is greater than or equal to the minimum order value
elif pair_value < target_per_coin:
difference = target_per_coin - pair_value

if difference >= min_order_value:
order_value = difference
buy_orders.append([pair[0], pair[1], order_value, difference])
buy_orders_data.append([pair[0], pair[1], order_value, difference])

if len(sell_orders) >= 1:
for order in sell_orders:
if len(sell_orders_data) >= 1:
for order in sell_orders_data:
if environment == "production":
order_confirmed = False
order_request = order_sell(order[1], order[2])
time.sleep(0.25)
time.sleep(0.1)
if order_request.status_code == 200:
order_confirmed = True

Expand All @@ -124,12 +125,12 @@ def rebalance(pairs):
print(str(print_value) + " USDT - " + order[0], end=" ")
print(colored("[SELL]", "magenta"))

if len(buy_orders) >= 1:
for order in buy_orders:
if len(buy_orders_data) >= 1:
for order in buy_orders_data:
if environment == "production":
order_confirmed = False
order_request = order_buy(order[1], order[2])
time.sleep(0.25)
time.sleep(0.1)
if order_request.status_code == 200:
order_confirmed = True

Expand All @@ -148,11 +149,16 @@ def rebalance(pairs):
print(str(print_value) + " USDT - " + order[0], end=" ")
print(colored("[BUY]", "green"))

total_orders = len(sell_orders) + len(buy_orders)
total_orders = len(sell_orders_data) + len(buy_orders_data)
if total_orders == 0:
current_time(True)
print(colored("No coins were eligible to be rebalanced", "yellow"))

del order_data
del buy_orders_data
del sell_orders_data
gc.collect()

print(colored("Waiting to be called...", "cyan"))


Expand Down

0 comments on commit 5134395

Please sign in to comment.