Thanks to visit codestin.com
Credit goes to github.com

Skip to content

alvin-leong/Crypto_trading_robot

 
 

Repository files navigation

Crypto trading robot

Background

This robot was created to help me to exit positions (trailing stops) better, to buy at more appropriate points then 'right now', and also to execute stop losses thoroughly. Out of curiousity, I have also created an integration with Telegram and implemented time analysis to predict price moves. The scripts were written for Python 2.7 and have not been tested on Python 3.

Note that this is not a fully automated bot which works automatically all the time but rather a helper. You will still need to decide when to enter and exit and what your stop losses / take profit targets are.

Files

The following folders are in the project:

  • 'exchanges' includes libraries to work with crypto exchanges
  • 'price_log' is a location of prices generated by price_logger.py
  • 'system_msg' currently includes system log generated by the telegram monitoring script telegramlib.py
  • 'logs_buy' and 'logs_trade' include trading logs generated by loglib.py

The following scripts are used:

  • Exchange_func.py includes wrappers for functions of specific exchanges (from the 'exchange' folder) so that the data has the same format to be processed by the other script. Note that your API keys for exchanges should be specified there.
  • Loglib.py is a library with a few logging functions
  • Platformlib.py is a library to detect your OS and return proper folders to launch additional scripts
  • Price_logger.py gets price ticker from exchanges and saves data in price_log folder
  • Sqltools.py is used for working with workflow.db sqlite database
  • Tdlib.py is used to analyse the price data and return values of time analysis indicators
  • Telegramlib.py works with telegram messages. Note that you would need to register a telegram bot and configure your tokens / chat id in this script.
  • Robot.py is a core script which performs the monitoring of position prices and sells or buys back
  • Smart_buy.py is a core script used to buy positions immediately or on specific events

Scripts logic

I was too busy writing the code and did not have enough time to document what logic was implemented. You could take a look at the scripts to check it out. Just briefly, the following features are implemented:

  • Notifications when market price info is not available and sleeping until trading resumes
  • Error handling (checking if there is enough balance, sum above minimum traded amount, amount is fine, etc.) and logging
  • Speedrun mode and simulation mode
  • Different strategies for BTC, less volatile alts, and more volatile alts
  • 'Quick' (5-min interval-based) and time-based (30min / 1hour series) analysis of confirmations on buys and sells, preventing from being shaken out easily
  • Dynamic price changes when buying and selling using orderbooks and time elapsed since the start of the script
  • Pre-profit and post-profit buybacks
  • Writing results (gains and losses) in a summary xls file

How to set things up

After registering your API keys on exchanges, you would need to specify them in exchange_func.py. Also, if you want to use Telegram functionality, create a bot (via botfather), and configure your tokens and chat id in the telegramlib.py.

Setting a telegram bot

Currently, everyone needs to set their own environment which includes registering the telegram bot, setting its menu (if needed), and specifying tokens & chat id in the telegramlib.py. There are several comprehensive guides on the Internet related to this topic, including the instructions from the telegram core. When running, daemon.py supports the following commands sent in a Telegram chat:

  • status: show running tasks
  • new: run a new take profit (trailing stop) / stop loss task
  • abort: cancel running tasks
  • workflow: launch a workflow which includes starting a buy task and then a take profit (trailing stop) / stop loss task
  • balance: check your balances on exchanges; as I am from Australia, this is displaying balance in AUD through USDT which is not quite accurate
  • sellnow: sell a position from running tp/sl task immediately
  • buy: initiate a buy tasks; various modes are supported (including buying in simulation, on breakout, in a regular mode, and immediately)
  • stopbuy: cancel an active buy task
  • nlong: create a new record about a long-term holding without any buy/sell tasks
  • rmlong: delete records from longs table
  • stoplistener: stop the monitoring bot

Setting an environment

I would recommend setting up a virtual machine or using a cloud VM so that popping screens do not disctract you at your usual working environment. You would need to launch daemon.py (for processing your telegram messages) and price_logger.py (for price updates feeding to tdlib). Please check daemon.py code for available commands and modes. Eventually, you would have an environment like this:

screen_example

Editing the workflow database

This repo includes a dummy database with no records which is a sqlite database (more than enough for the purpose of personal use). You can use any software working with sqlite to edit the tables in case anything goes wrong.

Terms of use and non-responsibility clause

Please use the scripts at your own risk. I am not responsible for your API keys being lost or intercepted, as well as your funds lost as a consequence of using the scripts. These scripts have some fool protection included - however, you are fully responsible for secure storage of the scripts and for your course of actions.

Contributions

As there are many other exchanges out there except for bittrex, I would appreciate anyone's help in including other exchange APIs and writing a wrapper in exchange_func.py (binance being the main priority currently). Additionally, I would appreciate help in more thorough processing of price/time data and developing a predictive model using sklearn. This would require getting historical data, for example, from kaggle, using tdlib for the calculation of parameters, and then training a model based on the data. An example of how this could be done is available at my Enron project repository.

Please feel free to contribute on github directly or contact me first via Telegram (illi4).

Donations

Donations would be much appreciated so that I can spend more time on coding and research.

  • BTC: 3PMXTE771Qj3VN5XxVQrvaJ1u1pC5YV5Ta
  • ETH: 0xb52770c0F71BF80aa97CCd79fe93f3F6520787Dc
  • LTC: M8TUzJMwCsCey4DvGEDHVW4QrMT2hq7zq7

About

A trading robot written on Python and integrated with Telegram.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 100.0%