Home

Awesome

ZTOM

ZTOM is the Python SDK for implementing the Trade Order Management System for crypto exchanges.

It's build upon the CCXT library and simplifies the development of fail-safe applications and trading algorithms by providing tools for managing the trade orders execution and some supplementary tools for configuration, flow control, reporting and etc.

With ZTOM it's possible to create, maintain and cancel trade orders using different triggers and conditions apart from the implementation of exchange's communication API.

Could be used for Algorithmic (algo) and High Frequency Trading (hft) for prototyping and production.

Main Features:

Why it was created

ZTOM was created because of 2 main reasons:

First one is to fail-safe proceeding with the common exchange API use cases like working with orders, exceptions handling so it could be possible to focus on trading algorithms rather than overcoming all the tech things.

This allows to create simple and clean "syntax" framework for order management and flow control so it could be possible easily read the code and understand the implemented algorithm as good as increase the speed of development and leverage on rich Python's analytical abilities.

The second big thing in mind was the implementation of the offline mode for both market data gathering and order execution using the same code as for online trading. This allows more quickly develop the algos, emulate different cases and as a result launch actual live tests more quickly.

Code Example

The order management could be something like this:

import time
import ztom as zt

ex = zt.ccxtExchangeWrapper.load_from_id("binance")
ex.enable_requests_throttle(60, 1200) # 1200 requests per minute for binance

# just checking in offline mode
ex.set_offline_mode("test_data/markets.json", "test_data/tickers.csv")

tickers = ex.fetch_tickers()

# sleep to maintain requests rate
time.sleep(ex.requests_throttle.sleep_time()) 

# below orders object are being created - not actual orders
order1 = zt.ActionOrder.create_from_start_amount(symbol="BTC/USDT",
                                            start_currency="BTC",
                                            amount_start=1,
                                            dest_currency="USDT",
                                            price=tickers["BTC/USDT"]["ask"])

order2 = zt.ActionOrder.create_from_start_amount("USD/RUB", "USD", 1, 
                                                 "RUB", 70)
# new OrderManager object
om = zt.ActionOrderManager(ex)

# adding orders to OM
# they will not be committed to exchange at this point
om.add_order(order1)
om.add_order(order2)

i = 0

while len(om.get_open_orders()) > 0:
	
  # check if order manager has something to do like create or cancel order  
  # and if yes we will not wait for it 
  if om.pending_actions_number() == 0:
        sleep_time = ex.requests_throttle.sleep_time()
        print("Sleeping for {}s".format(sleep_time))
        time.sleep(sleep_time)
  else:
      print("!!! NO SLEEP. ACTIONS ARE PENDING !!!")

 	# here all transaction are committed with the error handling and etc
   om.proceed_orders()
    
   # just to demonstrate how to cancel the order 
   # - operation will be committed on om.proceed_orders()
    if i == 5 and not order.filled > 0 :
      order1.force_close()

    i += 1

print(order1.filled)
print(order2.filled)

Tested Exchanges

Binance

Components and Features

ComponentsFeatures
Coremost used for creation apps and algos
ccxtExchangeWrappercommunication to exchange via the ccxt offline exchange connection emulation
action_orderaction order base class. allows to implement "smart" orders which could perform some actions (creation and cancellation of basic orders) in dependence from the data provided by order manager
order_manageraction orders lifecycle managementsafe communications with echange wrapperdata provision for ActionOrders
botconfiguration, logging, reporting and workflow management
throttlerequests throttle contoll
trade_orderscontainer of basic trade order data
Action Ordersvarious types of action orders
fok_ordersfill-or-kill order implementation (with some amount of time or on price changing)
recovery_ordersstop loss taker order implementation
maker_stop_loss (will be added soon)maker stop loss order
Calculation helpersPrices, amounts and other helpers for calculations
coreessential assets operations (without connections):trade symbol detectionorder side detectionrelative price differenceprecision convertion
orderbookorderbooks in-depth amounts and prices calculations
Tech/Supplementary
utilsvarious general purpose supplementary functions
datastoragecsv file management
clicommand line tools
timeroperations time counter and reporter
errorssome custom exceptions
Reporing
reporterinfluxdb client wrapper for db connection management
reporter_sqlasqlalchemy wrapper for db connection management
data modelssqlalchemy tables to represent trade orders and deals
grafana dashboards (will be added soon)samples of grafana dashboards

Installation

(the installation from pypi will be implemented soon)

Requirements: python3.6+ and some libs

  1. Clone the repo:

    git clone https://github.com/ztomsy/ztom.git

  2. install the dependencies:

    pip3 install -r requirements.txt

  3. install the ztom lib

pip3 install -e .

  1. run some tests:

    python3 -m unittest -v -b

License

This project is licensed under the MIT License - see the LICENSE.md file for details