A basic rebalance trading bot for the Kraken Cryptocurrency Exchange.
Project description
Welcome to the Spot Rebalance Bot for the Kraken Cryptocurrency Exchange 🐙
This is an unofficial trading bot that performs buys and sells on the Kraken cryptocurrency exchange using Python.
📌 Disclaimer
There is no guarantee that this software will work flawlessly at this or later times. Of course, no responsibility is taken for possible profits or losses. This software probably has some errors in it, so use it at your own risk. Also no one should be motivated or tempted to invest assets in speculative forms of investment. By using this software you release the author(s) from any liability regarding the use of this software.
It is not certain that this software will ever lead to profits.
Package Update
- January 25, 2023
📝 The Strategy
This algorithm can buy and sell one or more Spot assets without leverage.
The goal is to hold a certain amount of each base currency so that, for example, there is always about $1000 worth of BTC in the portfolio. If the price of Bitocoin increases so that there is now $1050 worth of Bitcoin in the portfolio, the excess $50 is sold. If the price of Bitcoin falls, so that the Bitcoin in the portfolio are only worth $950, the algorithm buys Bitcoin, to hold a value of about $1000 in Bitcoin in the portfolio again.
The algorithm checks the price range every 6 hours by default. The margin, from how many percent price difference the algorithm becomes active, can also be adjusted.
Actions can be logged on the command-line using the logging module with active INFO-level and can also be sent to a telegram bot.
⚙️ Quick start and configuration
0. Check the source code of this algorithm on GitHub and read the README.md carefully
1. Install the Python module:
python3 -m pip install kraken-rebalance-bot
2. Register at Kraken and generate API keys with trading access:
3. (optional) Create a Telegram Bot to get notified when the algorithm takes action
- Create a bot using @BotFather
- Write down/remember the token
- Start @RawDataBot and write down your personal chat id
4. Setup the configuration and start the algorithm
a.) Using docker
Build an image and pass a config when starting the container:
docker build -t krb:latest docker_src
docker run --env-file docker_src/.docker.env krb
b.) Using a Python script
In the following a minimal working example is shown that uses this strategy to hold a target_quantity
of $500 of ETH and $500 worth of XBT. Both are traded agains USD. The demo
key must be set to False
to enable the trading functionality. Of course, this also works with only one asset, too.
from krakenRebalanceBot.bot import RebalanceBot
def main() -> None:
bot = RebalanceBot(
key='kraken-api-key',
secret='kraken-secret-key',
config={
'base_currency': ['ETH', 'XBT'], # base assets to maintain
'quote_currency': ['USD', 'USD'], # quote assets to trade with
'target_quantity': [500, 500], # how many of the base to hold (value in quote)
'quote_to_maintain': [200, 200], # freezed quote/dont trade with this
'margin': [0.035, 0.035], # buy/sell threshold
'lowest_buy_price': [1000, 15000], # dont buy below this price
'times': ['06:00', '18:00'], # optional of use_build_in_sheduler is True
'use_build_in_sheduler': False, # if set to False, the script will only run once
'demo': True, # set to false to enable trading
'telegram': { # optional to get notified via telegram
'token': 'telegram-bot-token',
'chat_id': 'your-telegram-chat-id'
}
})
bot.run() # start the bot
if __name__ == '__main__': main()
The file /example/main.py
serves as an example on how to initialize and run this trading algorithm using the .env
and config.json
files (see GitHub). There is also a Jupyter notebook in which one can test the algorithm without risking any assets.
- To see the output on the command line you need to enable logging with level INFO as shown in the example script.
- If
use_build_in_sheduler
is enabled, there will be no output until the time is one oftimes
.
📖 Documentation of configuration arguments:
Key | Type | Description |
---|---|---|
base_currency |
List[str] |
Array of base currrncies to trade and hold. |
quote_currency |
List[str] |
Array of quote currencies to trade the base currency with. |
target_quantity |
List[float] | List[int] |
Defines how much of a base currency should be held. This value is the worth of the base currency in quote currency. |
quote_to_maintain |
List[float] | List[int] |
How much of quote currency should not be touched in the portfolio. |
margin |
List[float] |
Rebalance levels e.g. 0.035 = 3.5%: at a change of 3.5% the algorithm will buy or sell the missing/surplus quantity |
lowest_buy_price |
List[float] |
(optional) The bot will not buy if the price falls below this price to avoid catching a falling knife. Acts Kind of stop loss, but without selling. |
times |
List[str] |
(optional, default: ['00:00', '06:00', '12:00', '18:00'] ) At which time the bot should check the balances. |
use_build_in_sheduler |
bool |
(optional, default: False ) Checks the balances once and exits if set to False. Otherwise the program will run forever and check the balances at the specified times |
demo |
bool |
Trade or not sample trade output. Set to True if you know what this algorithm does. |
telegram |
{'chat_id': str, 'token': str} |
(optional) Specify token and chat id to get notified when the bot does something. |
If use_build_in_sheduler
is set to False
, the program is executed once and ends after the iteration over all assets. This offers the possibility to create own scripts, which execute this algorithm at individual times (e.g. using cron).
📍 Notes
-
Make sure to always have enough quote currency in your Kraken portfolio. Too low
target_quantity
values can cause the bot not to trade or even crash. Therefore, pay attention to the minimum order sizes.- Example:
- situation:
- minimal order size of ETH is 0.01
- price of ETH: $1300
margin
is set to 0.04target_quantity
is 200
- what will happen:
- If your actual holdings of ETH is $192 the bot tries to buy Ethereum with a volume of $8 because $200 - $200 * 0.04 will trigger the buy order. But the minimum ordersize of Ethereum is 0.01 ETH (see here), and with a price of $1300 0.01 ETH equals $13 so: $13 < $8 will raise an error.
- situation:
- So make sure that the minimum order sizes of the respective assets are consistent with the
margin
value and thetarget_quantity
. The example would work if thetarge_quantity
is set to 500, because $500 _ 0.04 = $20 which is larger than $1300 _ 0.01 = $13. - Also make sure that there is enough quote currency, otherwise the bot cannot buy anything.
- Example:
-
This strategy is one of the simplest and most basic approaches for trading cryptocurrencies. For this reason, it should be noted here that this does not necessarily lead to profits. Before running such an algorithm, everyone should be clear about what products are being traded, what these products are for in the first place, and what makes them valuable. Even the best companies, stocks, materials, and also cryptocurrencies can become worthless from one day to the next, so everyone should do their own research and make their decisions based on these results.
-
It has been decided here not to present any material regarding the profitability of this algorithm, as this could lead you to make your decisions based on my successes and failures. What works once or over a long period of time does not necessarily work in the future. But please let me know what you think about this basic algorithm and what could be improved.
-
For any problems, issues, and errors, please open an issue.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file kraken-rebalance-bot-0.5.7.2.tar.gz
.
File metadata
- Download URL: kraken-rebalance-bot-0.5.7.2.tar.gz
- Upload date:
- Size: 25.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 172a38df8eee2309160cf4260fbf00082a576dfecd63bc2a1c4a28ba73d540a1 |
|
MD5 | a13a1310541234f08ba6fdb6d4b31572 |
|
BLAKE2b-256 | 314b0b7e873e75f8ec1e1ad5e804f5c25d1ea743b2f3cb9e5bc22f74cf18a2f4 |
File details
Details for the file kraken_rebalance_bot-0.5.7.2-py2.py3-none-any.whl
.
File metadata
- Download URL: kraken_rebalance_bot-0.5.7.2-py2.py3-none-any.whl
- Upload date:
- Size: 21.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 661469a88ed481947d1d5df3ec10108a4f9af488c253e88e0fbb0068a249179d |
|
MD5 | ffabed637afb77ae8f46b0d184d2def7 |
|
BLAKE2b-256 | 5af9ee902a867b4d03efe988f20521e23090141197441c3e6269747cecea2aaa |