TheRockTrading Python Library

IperGiove
The Rock Trading
Published in
4 min readFeb 8, 2022

TheRockTrading (TRT) has announced the new library to communicate with JSON REST API v1.0

The Rock Trading

In this article it is shown some example of how use TheRockTrading library. The full documentation for the all endpoints is here (API v1.0) and the full python code is on GitHub:

The code

1. Installation

First of all we have to install the python library with pip command, as shown below:

pip install py-therocktrading-api

2. Quick start

To use the TRT library you can import it and use the Client method.
An example of simple quick start it is to see the all currencies:

from TheRockTrading import Client

trt = Client(API='API', API_SECRET='API_SECRET', staging=False)
print(trt.currencies())

The result is:

{
"currencies": [
{
"symbol": "BTC",
"common_name": "Bitcoin",
"decimals": 8
},
{
"symbol": "LTC",
"common_name": "Litecoin",
"decimals": 8
},
{.
.
.},
]
}

3. Benchmark

The new client is a synchronous client, however it is quit faster.
Here it is compared to the ccxt library always in python.

CCXT

time python -c "
import ccxt
[ccxt.therock().fetch_order_book('BTCEUR') for _ in range(10)]
"
real 0m14,958s
user 0m0,542s
sys 0m0,017s

TheRockTrading

time python -c "
from TheRockTrading import Client
client = Client()
[client.orderbook('BTCEUR') for _ in range(10)]
"
real 0m4,706s
user 0m0,247s
sys 0m0,020s

So TheRockTrading it is almost 3 times faster than cctx library.

4. Example: Download historical data

With TheRockTrading python library you can download the historical price of specific currency.
To do that we can create the Download class where we will interact with the endpoint for the OHLC statistics.

First of wall we have to initialize the class and the initial parameters.

Here the Client is the client of the TRT library, and the query function permits to store all parameters that we want to download, the period (min 1minute), the symbol e and the start_date (format equal to y-m-dThh:mm:ssZ).
Than we can create the function to download the data.

In the download function there is a while loop to download the data from the start_data to now.
In the while loop there is the ohcl function that return a pandas dataframe of the data.
Finally we can sort the data correctly and save the dataframe.

To call the class Downloader we can do that:

5. Example: Save and plot orderbook

In this example it is shown how to save the orderbook data in a csv and plot them using plotly library.
To do that we have to install before pandas and plotly, so:

pip install pandas
pip install plotly

After the installation we can start to code. First of all we can import the needed libraries and the prototype of main with some constants.

In this case we will take the orderbook for the pair SACUSDT and we will save the data in a folder /data.
As we can see the main call the generate_df function:

This function create a dataframe with the followings columns: pairs, datetime, orderbook, price and amounts.
Before to create the dataframe however the data must be acquired with the function asks_bids(pair):

Here the client it used to get the asks and bids data and return them.

Now we have the asks and bids data in the generat_df function, but we have to unpack them in order to take only price and amount, to do that there is the unpack function:

So now we can create the dataframe from the returned dict like that:

df_asks = pd.DataFrame(unpack(
pair = pair,
date = date,
book_tag = "ask",
book = asks))
df_bids = pd.DataFrame(unpack(
pair = pair,
date = date,
book_tag = "bid",
book = bids))

We use the pandas function concat to concatenate the df_asks and df_bids and than we use the function save(df) to save the dataframe and plot(df) to plot the dataframe.

Below an example of the plot for the BTCUSDC pair:

Orderbook for BTCUSDC at 9/05/2022

If you are not registered yet, you can use the link below as promo to get 50% of discount over the fee for 30 days.

Conclusion

In this article I want to show some examples of how use the TheRockTrading python library to communicate with JSON REST API v1.0.
This article is constantly updated with new examples so stay be tunes.
The full code for the examples is
here.

References

  1. https://github.com/IperGiove
  2. https://github.com/therocktrading/py-therocktrading-api
  3. https://api.therocktrading.com/doc/v1/index.html
  4. https://www.therocktrading.com/

--

--