Skip to main content

Trading bot with trail stop and chained orders for pour Bitmex and maybe more...

Project description


KolaBot is a program to handle concurrently several pairs of orders in the BitMEX exchange. A pair is a main order (amoung those allowed by BitMEX) and an opposite order (again among allowed) that acts as a stop. Each order enter the book based on conditions you set in morders.tsv.

Main conditions

  • time condition: an order activate if the time enters the [dateA, dateB] range
  • price condition: an order activate the market enters the [priceA, priceB] range

other conditions

  • a timeout: an order will cancel after timeout minutes. Note this enable speed conditions (eg. activate only if price rise by 60ø in 1 minute, else cancel and restart)
  • a repeat #: an order will repeat # times if canceled or filled
  • a waitting time: if canceled or filled the order pair will wait before repeating (min wait ~1 minute)
  • a hook condition: (new not fully stable yet), an order will activate if a hooked order reach a specified status (filled, partialy filled, canceled)


  • time units: they are generaly in minutes and for some command line params, in seconds.
  • price units can be:
    • relative to market price (index, mark or last Price)
      • in %, of the activating price (eg. if price move by 5% from order activation)
      • in differential of the activating price (eg if price move by -80ø from order activation)
    • absolute, (eg. if price reach 3500ø)

Options specific to the stop order

By defaut kolaBot runs an opening order (main) and a close order (secondary or tail or trail). If your main is a buy, the secondary will be a close and vice-versa. It is possible to run only one of the two order by setting automatic cancelation condition on the not wanted order.

Here’s a simple example: A buy at market (main order) with a stop at market (secondary order). You just need to set the price differential between the main order and the tail order. Like you want a tail at:

  • 100ø below the activating price (relative in differential),
  • or at 2% below of the activating price (relative in %),
  • or at 2500ø what ever the activating price (absolute)

Example scenarios

  • you may want to buy 10% of your available balance if the index price goes down by 2% in 2 hours but this should only true for the next 24 hours.
  • you may want to place a simple buy limit if the price goes up by 100 USD and then automatically set a (trailing) stop at this higher price - 20 USD. This trail stop actually acts both as a stop order and a trail stop. If the market moves against you, it stays in place but if the market move in your favor it will only move once the price will be 20 or higher above the entry price (in this example). Also you can set in the code some flexibility to the trail. Meaning that if the price rise quickly the trail price will close down on the order up to 20% of the initial 20 USD delta you set. This is because usualy chart increase are followed buy sharp reverse and I think it’s wise to go out of the market to pick it up automatically later.
  • you may want to use a sell (short) each time the price hits 20000 USD with a buy at stop 21000 USD and if the stop trigger wait 20 minutes before resetting the condition.

All this and more is feasible with this bot. I do not recommend using it to do trading below 1 order per minute unless you have special API arrangement with BitMEX and in that case you should have an improved bot. :o). The reason is that if you have several orders even If I use the websockets, I make REST call that increase with the number of order pairs to track.

Hooks: Update <2020-05-05 mar.>

You can now use hooks or what was once called chained order in BitMEX. A hook condition is a condition that is true if the hooked order reach the state defined in the condition. The states can be Filled (F), Triggered (T), Canceled (C) or PartiallyFilled (P) Since orders are always passed in pairs. The hook condition can apply to the Principal order (the one to enter the book) or the Secondary order (the one to exit). The syntax to define the hook condition is as follow `<name>-[P|S]-[F|C|T|P]`

  • Examples:
    • src1-P-F, will hook when the principal order of the pair named src1 will be filled
    • src1-P-C, will hook when the principal order of the pair named src1 will cancel
    • foo-S-P, will hook when the principal order of the pair named foo will partially fill …
  1. Why hooks ?

    Hooks can be useful especially with trail orders. Hook price and time settings are relative to the moment their are started. So you can have an order that enter the book when a trailing stop is filled. This could be useful to catch reversal in momentum.


Download or clone the repository

Git Project URL:

git clone
cd kolaBitMexBot

Install dependencies

This will create a virtualenv and install packages required by the program. You need to `pip3` and `python3.8` installed on your system. note To install mutliple python on your system, check `pyenv`.

virtualenv --python=</path/to/python3>  .
source ./bin/activate
pip install -r requirements.txt

# run main programmes
python -m  kolaBot.run_multi_kola -h
python -m  kolaBot.multi_kola -h

pip install

If you just want to use kolaBot, you can install the module directly with `pip`

pip install kolaBitMEX

would recommand doing it as a pip editable module with:

# build package with the
python sdist bdist_wheel; twine check dist/*

# if you used virtualenv wheel and twine will have been installed

# install the package from local source
pip install -e .

Add you API keys in `kolaBot/kola/`

This file created and it should contain the 4 variable keys:


Write your orders in the morder.tsv

Test your orders on

python -m kolaBot.run_multi_kola -l INFO >

Check the file

Satified? Run it live!

python -l INFO --live

Extension TODO

DONE make chained (or hooked) orders <2020-05-05 mar.>

That is, you can an orders starting based on the state of one or more other orders.

Extend dummy bargain to have a personnal test net

Write hyptothesis tests


Why is it free ?

  1. Because I had fun making it.
  2. Also because the gift you may give me having fun using this code, will be infinitely more valuable for me, if its free.:o)

381b5ygUaK3CpHSKH2kKYCYKGMUbH4ruiw (BTC only)

Did I loose money with that bot ?

No, but this is a personnal investissement. I spend a gift of 0.5 XBT to test it live, and during the development phase which I started in September 2017 I used most of it. The reasons are simple: I didn’t know much about trading and I’m not a professional coder. Also, testnet is not as good as real market to make real tests. I’m confident that this bot is a great tool to help anyone willing to gain money. 🥂

Warning !

Check the code! I’m not an professional programmer and if I made many tests along this 2 years journey on building kola BitMEX bot I did not consistently use a test framework yet. THEREFORE there are obviously many improvements to be made.

That been published, THIS CODE IS LICENCE FREE. No Gnu, no MIT, no GitHub©, no what so ever regulation from my part. Although, I may be obliged by some legal contracts I’m not consciously aware off because I did reused codes notably BitMEX’s API connectors and an uncountable number of functions from python’s imported libraries (pandas, request, numpy, datetime…). I hope their authors don’t really care. We are born free, let’s stay so. So, your are free to do what you want with this code BUT… do check it and understand it.

File Index

Core program files

├──  ->  cancel and close all order on testnet
├── kola
│   ├──  ->  handle connections to markets
│   ├──  ->  handle timeouts and thread of active orders
│   ├── connexion
│   │   ├──  ->  authentification to bitMEX
│   │   ├──  ->  websocket API
│   │   └──
│   ├──
│   ├──
│   ├──
│   ├── orders
│   │   ├──  ->  hold condition object to activate orders
│   │   ├──  ->  orders that can hook to other orders
│   │   ├──
│   │   ├──  ->  basic order with condition. other orders inherit it
│   │   ├──  ->  functions to places limit, stop, limit if touched ...
│   │   └──  ->  orders that follow price variation and update
│   ├──  ->  object to follow the different prices indexes
│   ├──  ->  setting files (where your keys may be)
│   ├──  ->  where API keys could be
│   ├──  ->  (new) types to start typing the programm
│   └── utils
│       ├──  ->  handle command line arguments
│       ├──  ->  function to set conditions
│       ├──  ->  constants
│       ├──  ->  function to handle dates
│       ├──  ->  customized exceptions
│       ├──  ->  generic utils
│       ├──
│       ├──  ->  log function
│       ├──  ->  utils to set or check orders
│       └──  ->  utils to set or get prices
├── morders.tsv  ->  where you set your orders
├──  ->  handle the (multiple runs) of one pair of orders
├──  ->  (depreciated...)
├──  ->  handle multiple pairs of orders (parse morders.tsv)
└── tests

5 directories, 33 files

Setup and annexes program files

pyenv local python-version, should be >=3.8
a versatile IDE config file (emacs :))
files that git should ignore
config file for flake, mypy
a permissive license
set of required modules
package file for python

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

kolaBitMEXBot-1.1.9.tar.gz (91.1 kB view hashes)

Uploaded source

Built Distribution

kolaBitMEXBot-1.1.9-py3-none-any.whl (115.0 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page