Skip to content

Commit

Permalink
1.0.0 (piebotai#1)
Browse files Browse the repository at this point in the history
* .gitignore file

* Environment variables setup

* Wildcard rule to ignore any test Python files

* Adds first version of the bot file

* Adds extra sleep functions to prevent the bot from hitting the API rate limits. Pass over the forloop if the price of the coin pair is 0 - piebotai#3

* Unocomments pass print - piebotai#3

* The bot will now only schedue and place orders in production mode

* Kill the script if no environment has been defined

* Moves tests into their own directory

* Adds a new utility script that shows you the current values of each coin pair, and how they relate to the current target values

* Typo

* Increases max_order_value to 0.5

* Check if the pair value and coins quantity have come back

* Adds a new utility script that lets you know how far away each coin pair is from meeting the minimum Crypto.com App staking requirements

* REST API (piebotai#7)

* Starting REST API version

* Asyncio setup

* Adds list of trading pairs

* Don't use asyncio

* Gets the current USDT balance

* Calculates the total portfolio balance in USDT

* Calculates the target for each coin pair

* Adds some more checks before the bot runs

* Makes printing the current a time a function

* Adds buy/sell logic

* Moves current file to old for tidy up

* Starting to move functions into separate file

* Pre-flight Checks function, moves coin pairs to a config file

* Working and ready to start adding code back

* Lets users know the bot is running and waiting to be called

* USDT reserve should be a percentage of the portfolio value - piebotai#5

* Gets the total USDT balance of the portfolio

* Fully working version

* Makes the sell quantity for SHIB orders an integer because of the low pricing

* The current_time function can now be configured to show the time on an empty line or on its own line

* Prints waiting statement at the end of each loop

* Fixes print statements

* Quantity precisions must be defined for sell orders

* Removes extra time print

* Removes old PieBot file

* Removes unecessary sleep

* Preflight checks for the minimum and maximum order values

* The USDT reserve should be a percentage of the entire portfolio value - Closes piebotai#5

* _config.py should be in .gitignore

* Environment, API key, and API secret are now set from the config file

* Adds comments to functions

* Print order responses for debugging

* Temporary fix where some sell orders weren't going through to the exchange - piebotai#8

* Fixes an issue where some sell orders weren't completing because the quantity values weren't in the correct format - Fixes piebotai#8

* Basic emoji for order confirmations

* Print order error messages

* Adds price_precision - piebotai#2

* Buy order notionals should be in the correct number of decimal places - piebotai#2

* Removes SHIB from the example config file

* Completely different order confirmation print statements for production and dev

* Removes utility scripts for now, these will come back in a later release

* Get price and quantity precision values from the API (piebotai#13)

* Basic working first version

* Removes price and quantity precisions from the example config file

* Price and quantity precision now come from the API

* Adjusts config comment to make it clearer
  • Loading branch information
Jamie Wade authored May 16, 2021
1 parent 4ad0e92 commit 00c0572
Show file tree
Hide file tree
Showing 4 changed files with 437 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# General
.DS_Store
.idea

# Environment
__pycache__
get-pip.py
venv

# Project
.env
tests
_config.py
150 changes: 150 additions & 0 deletions PieBot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
from functions import *
import emoji
import schedule

pre_flight_checks()


def piebot(pairs):
# Let users know the bot has been called and is running
print()
print(emoji.emojize(':mag:', use_aliases=True), end=" ")
print(colored("Collecting current balances", "cyan"))

total_balance = 0

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

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

total_balance = total_balance + (coin_balance * coin_price)

# Get the total balance of USDT and add it to the current collected balance
usdt_total_balance = get_coin_balance("USDT")
total_balance = total_balance + usdt_total_balance

# Keeps aside the defined USDT reserves
usdt_reserve_value = (total_balance / 100) * (usdt_reserve * 100)
total_balance = total_balance - usdt_reserve_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_balance / len(pair_list)

print(emoji.emojize(':white_check_mark:', use_aliases=True), end=" ")
print(colored("Balances collected", "green"))

print(emoji.emojize(':money_bag:', use_aliases=True), end=" ")
print(colored("Placing orders", "cyan"))

for pair in pair_list:
# Sets null defaults
buy_order = False
sell_order = False
difference = 0
order_value = 0
pair_value = 0

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

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

pair_value = coin_balance * coin_price

# If the coin pair value is over target, sell the excess if it's greater than the minimum order value
if pair_value > target_per_coin:
difference = pair_value - target_per_coin
if difference >= min_order_value:
sell_order = True
order_value = difference / coin_price

# If the coin pair value is under target, work out how much we need to buy
elif pair_value < target_per_coin:
difference = target_per_coin - pair_value

# If the difference is between min_order_value and max_order_value (inclusive), set the difference as the order value
if min_order_value <= difference <= max_order_value:
buy_order = True
order_value = difference

# If the difference is greater than max_order_value, set the order value as max_order_value
elif difference > max_order_value:
buy_order = True
order_value = max_order_value

if buy_order:
if environment == "production":
order_confirmed = False
order = order_buy(pair[1], order_value)
time.sleep(0.25)
if order.status_code == 200:
order_confirmed = True

print_value = round(order_value, 2)
current_time(True)
print(str(print_value) + " USDT - " + pair[0], end=" ")
print(colored("[BUY]", "green"), end=" ")

if order_confirmed:
print(emoji.emojize(':white_check_mark:', use_aliases=True))
else:
print(emoji.emojize(':x:', use_aliases=True))
print(order.status_code, order.reason)
print(order.content)

else:
print_value = round(order_value, 2)
current_time(True)
print(str(print_value) + " USDT - " + pair[0], end=" ")
print(colored("[BUY]", "green"))

elif sell_order:
if environment == "production":
order_confirmed = False
order = order_sell(pair[1], order_value)
time.sleep(0.25)
if order.status_code == 200:
order_confirmed = True

print_value = round(difference, 2)
current_time(True)
print(str(print_value) + " USDT - " + pair[0], end=" ")
print(colored("[SELL]", "magenta"), end=" ")

if order_confirmed:
print(emoji.emojize(':white_check_mark:', use_aliases=True))
else:
print(emoji.emojize(':x:', use_aliases=True))
print(order.status_code, order.reason)
print(order.content)

else:
print_value = round(difference, 2)
current_time(True)
print(str(print_value) + " USDT - " + pair[0], end=" ")
print(colored("[SELL]", "magenta"))

else:
current_time(True)
print(pair[0], end=" ")
print(colored("[SKIP]", "yellow"))

print(emoji.emojize(':hourglass:', use_aliases=True), end=" ")
print(colored("Waiting to be called", "cyan"))


if environment == "production":
print(emoji.emojize(':hourglass:', use_aliases=True), end=" ")
print(colored("Waiting to be called", "cyan"))
schedule.every().hour.at(":00").do(piebot, pairs=pair_list)

while True:
schedule.run_pending()
time.sleep(1)

else:
piebot(pairs=pair_list)
26 changes: 26 additions & 0 deletions _config-example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
environment = "dev"
api_key = "xxx"
api_secret = "xxx"

# The list of coin pairs you want to trade with
pair_list = [
("ADA", "ADA_USDT"),
("ALGO", "ALGO_USDT"),
("ATOM", "ATOM_USDT"),
("BTC", "BTC_USDT"),
("CRO", "CRO_USDT"),
("DOT", "DOT_USDT"),
("ETH", "ETH_USDT"),
("LTC", "LTC_USDT"),
("XLM", "XLM_USDT"),
("XRP", "XRP_USDT")
]

# How much USDT do you want to keep as a reserve. This is a percentage of the total portfolio balance
# 0.05 = 5%
# 0.15 = 15%
usdt_reserve = 0.05

# Sets the minimum and maximum order values, so we don't eat into our USDT balance too quickly
min_order_value = 0.25
max_order_value = 0.50
Loading

0 comments on commit 00c0572

Please sign in to comment.