Crypto#

This section contains 7 examples for Crypto using the onetick-py.
Each example is a self-contained script that can be run against the OneTick Cloud sample databases.

# onetick-py WebAPI configuration for OneTick Cloud
import os
os.environ['OTP_WEBAPI'] = '1'
os.environ['OTP_HTTP_ADDRESS'] = 'https://rest.cloud.onetick.com'
os.environ['OTP_ACCESS_TOKEN_URL'] = 'https://cloud-auth.parent.onetick.com/realms/OMD/protocol/openid-connect/token'
os.environ['OTP_CLIENT_ID'] = '__FILL_IN__'
os.environ['OTP_CLIENT_SECRET'] = '__FILL_IN__'

Crypto Trade Retrieval#

Retrieving Trades from a Crypto Venue.
Unlike Equities and Futures, the Trade Size on a crypto venue is fractional.

import onetick.py as otp

data = otp.DataSource(db='BINANCE', tick_type='TRD')
data = data.limit(1000)
result = otp.run(data,
                 start=otp.dt(2026, 7, 28),
                 end=otp.dt(2026, 7, 29),
                 timezone='UTC',
                 symbols='BTCUSD')
result
Time EXCH_TIME PRICE SIZE TRADE_VENUE BUYER SELLER AGGRESSOR_SIDE TRADE_TYPE TRADE_PERIOD BOOK_TYPE TRADE_ID OMDSEQ DELETED_TIME TICK_STATUS
0 2026-07-28 00:00:00.145371 2026-07-28 00:00:00.144160 63686.99 0.00008 BINANCE B - 0 606647 0 1970-01-01 0
1 2026-07-28 00:00:04.350446 2026-07-28 00:00:04.349603 63687.03 0.00130 BINANCE S - 0 606648 0 1970-01-01 0
2 2026-07-28 00:00:04.383817 2026-07-28 00:00:04.382689 63687.03 0.00158 BINANCE S - 0 606649 0 1970-01-01 0
3 2026-07-28 00:00:04.517794 2026-07-28 00:00:04.516315 63687.04 0.00008 BINANCE B - 0 606650 0 1970-01-01 0
4 2026-07-28 00:00:05.096039 2026-07-28 00:00:05.094642 63687.04 0.00008 BINANCE B - 0 606651 0 1970-01-01 0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
995 2026-07-28 11:18:21.409880 2026-07-28 11:18:21.408894 63456.00 0.00008 BINANCE B - 0 607642 0 1970-01-01 0
996 2026-07-28 11:18:21.409888 2026-07-28 11:18:21.408894 63472.87 0.00001 BINANCE B - 0 607643 1 1970-01-01 0
997 2026-07-28 11:18:33.822821 2026-07-28 11:18:33.821948 63480.00 0.00008 BINANCE B - 0 607644 0 1970-01-01 0
998 2026-07-28 11:18:45.583787 2026-07-28 11:18:45.582047 63468.01 0.00008 BINANCE B - 0 607645 0 1970-01-01 0
999 2026-07-28 11:18:45.584726 2026-07-28 11:18:45.583657 63480.01 0.00072 BINANCE S - 0 607646 0 1970-01-01 0

1000 rows × 15 columns

Crypto Quote Retrieval#

Retrieving Quotes from a Crypto Venue.
Unlike Equities and Futures, the Bid and Ask Sizes on a crypto venue are fractional.

import onetick.py as otp

data = otp.DataSource(db='BINANCE', tick_type='QTE')
data = data.limit(1000)
result = otp.run(data,
                 start=otp.dt(2026, 7, 28),
                 end=otp.dt(2026, 7, 29),
                 timezone='UTC',
                 symbols='BTCUSD')
result
Time EXCH_TIME BID_PRICE BID_SIZE ASK_PRICE ASK_SIZE QUOTE_VENUE OMDSEQ
0 2026-07-28 00:00:00.005578 1970-01-01 63686.98 0.03223 63714.40 0.00319 BINANCE 18
1 2026-07-28 00:00:00.011008 1970-01-01 63686.98 0.03223 63686.99 0.00008 BINANCE 0
2 2026-07-28 00:00:00.141446 1970-01-01 63681.91 0.00099 63686.99 0.00008 BINANCE 1
3 2026-07-28 00:00:00.144103 1970-01-01 63682.16 0.03228 63686.99 0.00008 BINANCE 1
4 2026-07-28 00:00:00.145341 1970-01-01 63682.16 0.03228 63714.40 0.00319 BINANCE 0
... ... ... ... ... ... ... ... ...
995 2026-07-28 00:11:57.949695 1970-01-01 63532.69 0.02221 63558.95 0.01555 BINANCE 14
996 2026-07-28 00:11:58.084374 1970-01-01 63532.69 0.02221 63566.55 0.01555 BINANCE 0
997 2026-07-28 00:11:58.370894 1970-01-01 63536.78 0.04423 63566.55 0.01555 BINANCE 48
998 2026-07-28 00:11:58.374133 1970-01-01 63536.95 0.02221 63566.55 0.01555 BINANCE 3
999 2026-07-28 00:11:58.419118 1970-01-01 63538.37 0.00099 63566.55 0.01555 BINANCE 3

1000 rows × 8 columns

Crypto Book Update Retrieval#

Retrieving Book Updates from a Crypto Venue.
Book Updates are provided as an L2 dataset, providing updates to Price Levels.
Basic retrieval is useful for counting order book changes.
To reconstruct the order book, order book aggregations like ob_snapshot_wide() should be used.

import onetick.py as otp

data = otp.DataSource(db='BINANCE', tick_type='PRL')
data = data.limit(1000)
result = otp.run(data,
                 start=otp.dt(2026, 7, 28),
                 end=otp.dt(2026, 7, 29),
                 timezone='UTC',
                 symbols='BTCUSD')
result
Time EXCH_TIME BUY_SELL_FLAG PRICE SIZE RECORD_TYPE TICK_STATUS DELETED_TIME OMDSEQ
0 2026-07-28 00:00:00.000000 1970-01-01 00:00:00.000000 0 0.00 0.00000 Z 0 1970-01-01 91088
1 2026-07-28 00:00:00.000000 1970-01-01 00:00:00.000000 0 63686.98 0.03223 C 0 1970-01-01 91089
2 2026-07-28 00:00:00.000000 1970-01-01 00:00:00.000000 0 63681.91 0.00099 C 0 1970-01-01 91090
3 2026-07-28 00:00:00.000000 1970-01-01 00:00:00.000000 0 63680.47 0.04853 C 0 1970-01-01 91091
4 2026-07-28 00:00:00.000000 1970-01-01 00:00:00.000000 0 63676.83 0.00022 C 0 1970-01-01 91092
... ... ... ... ... ... ... ... ... ...
995 2026-07-28 00:01:06.071861 2026-07-28 00:01:06.071349 1 63718.84 0.00000 R 0 1970-01-01 6
996 2026-07-28 00:01:07.272235 2026-07-28 00:01:07.271454 0 63693.00 0.00000 R 0 1970-01-01 1
997 2026-07-28 00:01:07.272235 2026-07-28 00:01:07.271454 0 63680.73 0.00000 R 0 1970-01-01 2
998 2026-07-28 00:01:07.272235 2026-07-28 00:01:07.271454 0 63676.63 0.04785 R 0 1970-01-01 3
999 2026-07-28 00:01:07.272235 2026-07-28 00:01:07.271454 1 63711.05 0.01457 R 0 1970-01-01 4

1000 rows × 9 columns

Crypto Book Snapshot Retrieval#

Retrieving a Book Snapshot at a Specified Time from a Crypto Venue.
To reconstruct the order book, the ob_snapshot_wide() aggregation is used.
As this is a crypto book, the size_max_fractional_digits attribute is set, allowing the book to be reconstructed with size stored with up to 9 fractional digits.
ob_snapshot_wide() returns the book with Bid and Ask on the same row.

import onetick.py as otp

data = otp.DataSource(db='BINANCE', tick_type='PRL')
data = data.ob_snapshot_wide(size_max_fractional_digits=9)
data = data[['BID_PRICE', 'BID_SIZE', 'ASK_PRICE', 'ASK_SIZE', 'LEVEL']]
result = otp.run(data,
                 start=otp.dt(2026, 7, 28, 12),
                 end=otp.dt(2026, 7, 28, 12),
                 timezone='UTC',
                 symbols='BTCUSD')
result
Time BID_PRICE BID_SIZE ASK_PRICE ASK_SIZE LEVEL
0 2026-07-28 12:00:00 63372.00 0.00008 63392.01 0.00008 1
1 2026-07-28 12:00:00 63366.05 0.02220 63396.00 0.00008 2
2 2026-07-28 12:00:00 63366.04 0.00100 63396.88 0.00076 3
3 2026-07-28 12:00:00 63364.00 0.00008 63399.00 0.00008 4
4 2026-07-28 12:00:00 63362.46 0.04722 63399.40 0.04762 5
... ... ... ... ... ... ...
229 2026-07-28 12:00:00 32000.00 0.51281 NaN 0.00000 230
230 2026-07-28 12:00:00 30000.00 0.13842 NaN 0.00000 231
231 2026-07-28 12:00:00 23600.02 0.04238 NaN 0.00000 232
232 2026-07-28 12:00:00 19389.02 0.10057 NaN 0.00000 233
233 2026-07-28 12:00:00 18785.00 0.00368 NaN 0.00000 234

234 rows × 6 columns

Crypto Book Snapshot Retrieval with Accumulative Values#

Retrieving a Book Snapshot at a Specified Time from a Crypto Venue, outputting Accumulative Depth.
To reconstruct the order book, the ob_snapshot_wide() aggregation is used.
As this is a crypto book, the size_max_fractional_digits attribute is set.
ob_snapshot_wide() returns the book in a format with Bid and Ask on the same row.
Bid and Ask Value is calculated using PRICE * SIZE.
The Bid and Ask Sizes are used to calculate accumulative sizes across the book depth, computed with a running sum aggregation across the levels.

import onetick.py as otp

data = otp.DataSource(db='BINANCE', tick_type='PRL')
data = data.ob_snapshot_wide(size_max_fractional_digits=9)

# Value per level = PRICE * SIZE
data['BID_VALUE'] = data['BID_PRICE'] * data['BID_SIZE']
data['ASK_VALUE'] = data['ASK_PRICE'] * data['ASK_SIZE']

# Accumulative sizes across the book depth
data = data.agg({'ACCUM_BID_SIZE': otp.agg.sum('BID_SIZE'),
                 'ACCUM_ASK_SIZE': otp.agg.sum('ASK_SIZE')},
                running=True, all_fields=True)

data = data[['BID_PRICE', 'BID_SIZE', 'ASK_PRICE', 'ASK_SIZE', 'LEVEL',
             'BID_VALUE', 'ASK_VALUE', 'ACCUM_BID_SIZE', 'ACCUM_ASK_SIZE']]
result = otp.run(data,
                 start=otp.dt(2026, 7, 28, 12),
                 end=otp.dt(2026, 7, 28, 12),
                 timezone='UTC',
                 symbols='BTCUSD')
result
Time BID_PRICE BID_SIZE ASK_PRICE ASK_SIZE LEVEL BID_VALUE ASK_VALUE ACCUM_BID_SIZE ACCUM_ASK_SIZE
0 2026-07-28 12:00:00 63372.00 0.00008 63392.01 0.00008 1 5.069760 5.071361 0.00008 0.00008
1 2026-07-28 12:00:00 63366.05 0.02220 63396.00 0.00008 2 1406.726310 5.071680 0.02228 0.00016
2 2026-07-28 12:00:00 63366.04 0.00100 63396.88 0.00076 3 63.366040 48.181629 0.02328 0.00092
3 2026-07-28 12:00:00 63364.00 0.00008 63399.00 0.00008 4 5.069120 5.071920 0.02336 0.00100
4 2026-07-28 12:00:00 63362.46 0.04722 63399.40 0.04762 5 2991.975298 3019.079428 0.07058 0.04862
... ... ... ... ... ... ... ... ... ... ...
229 2026-07-28 12:00:00 32000.00 0.51281 NaN 0.00000 230 16409.919968 NaN 47.13080 6.48798
230 2026-07-28 12:00:00 30000.00 0.13842 NaN 0.00000 231 4152.599970 NaN 47.26922 6.48798
231 2026-07-28 12:00:00 23600.02 0.04238 NaN 0.00000 232 1000.168848 NaN 47.31160 6.48798
232 2026-07-28 12:00:00 19389.02 0.10057 NaN 0.00000 233 1949.953741 NaN 47.41217 6.48798
233 2026-07-28 12:00:00 18785.00 0.00368 NaN 0.00000 234 69.128800 NaN 47.41585 6.48798

234 rows × 10 columns

Crypto Book Snapshot Retrieval with Accumulative Values and Best Prices#

Retrieving a Book Snapshot at a Specified Time from a Crypto Venue, outputting Accumulative Depth and Best Prices.
To reconstruct the order book, the ob_snapshot_wide() aggregation is used.
As this is a crypto book, the size_max_fractional_digits attribute is set.
ob_snapshot_wide() returns the book in a format with Bid and Ask on the same row.
Bid and Ask Value is calculated using PRICE * SIZE.
The Bid and Ask Sizes are used to calculate accumulative sizes across the book depth, computed with a running sum aggregation across the levels.
The Bid and Ask Prices are used to return the Best Prices across the book depth, computed with a running first aggregation across the levels.

import onetick.py as otp

data = otp.DataSource(db='BINANCE', tick_type='PRL')
data = data.ob_snapshot_wide(size_max_fractional_digits=9)

# Value per level = PRICE * SIZE
data['BID_VALUE'] = data['BID_PRICE'] * data['BID_SIZE']
data['ASK_VALUE'] = data['ASK_PRICE'] * data['ASK_SIZE']

# Accumulative sizes and best (first) prices across the book depth
data = data.agg({'ACCUM_BID_SIZE': otp.agg.sum('BID_SIZE'),
                 'ACCUM_ASK_SIZE': otp.agg.sum('ASK_SIZE'),
                 'BEST_BID_PRICE': otp.agg.first('BID_PRICE'),
                 'BEST_ASK_PRICE': otp.agg.first('ASK_PRICE')},
                running=True, all_fields=True)

data = data[['BID_PRICE', 'BID_SIZE', 'ASK_PRICE', 'ASK_SIZE', 'LEVEL',
             'BID_VALUE', 'ASK_VALUE', 'ACCUM_BID_SIZE', 'ACCUM_ASK_SIZE',
             'BEST_BID_PRICE', 'BEST_ASK_PRICE']]
result = otp.run(data,
                 start=otp.dt(2026, 7, 28, 12),
                 end=otp.dt(2026, 7, 28, 12),
                 timezone='UTC',
                 symbols='BTCUSD')
result
Time BID_PRICE BID_SIZE ASK_PRICE ASK_SIZE LEVEL BID_VALUE ASK_VALUE ACCUM_BID_SIZE ACCUM_ASK_SIZE BEST_BID_PRICE BEST_ASK_PRICE
0 2026-07-28 12:00:00 63372.00 0.00008 63392.01 0.00008 1 5.069760 5.071361 0.00008 0.00008 63372.0 63392.01
1 2026-07-28 12:00:00 63366.05 0.02220 63396.00 0.00008 2 1406.726310 5.071680 0.02228 0.00016 63372.0 63392.01
2 2026-07-28 12:00:00 63366.04 0.00100 63396.88 0.00076 3 63.366040 48.181629 0.02328 0.00092 63372.0 63392.01
3 2026-07-28 12:00:00 63364.00 0.00008 63399.00 0.00008 4 5.069120 5.071920 0.02336 0.00100 63372.0 63392.01
4 2026-07-28 12:00:00 63362.46 0.04722 63399.40 0.04762 5 2991.975298 3019.079428 0.07058 0.04862 63372.0 63392.01
... ... ... ... ... ... ... ... ... ... ... ... ...
229 2026-07-28 12:00:00 32000.00 0.51281 NaN 0.00000 230 16409.919968 NaN 47.13080 6.48798 63372.0 63392.01
230 2026-07-28 12:00:00 30000.00 0.13842 NaN 0.00000 231 4152.599970 NaN 47.26922 6.48798 63372.0 63392.01
231 2026-07-28 12:00:00 23600.02 0.04238 NaN 0.00000 232 1000.168848 NaN 47.31160 6.48798 63372.0 63392.01
232 2026-07-28 12:00:00 19389.02 0.10057 NaN 0.00000 233 1949.953741 NaN 47.41217 6.48798 63372.0 63392.01
233 2026-07-28 12:00:00 18785.00 0.00368 NaN 0.00000 234 69.128800 NaN 47.41585 6.48798 63372.0 63392.01

234 rows × 12 columns

Crypto Book Depth Statistics to Trade a Specified Amount Across Time#

Calculating Bid and Ask VWAP and other Statistics across time from a Crypto Venue.
To calculate Bid and Ask VWAP, the ob_summary() aggregation is used.
As this is a crypto book, the size_max_fractional_digits attribute is set.
The max_depth_shares attribute determines how much should be traded.
The bucket_interval attribute determines how often to output the resulting book metrics.

The returned BID_VWAP and ASK_VWAP can be used to calculate Effective Spread.
The returned BID_SIZE and ASK_SIZE identify if the liquidity is present.
The returned BEST_ASK_PRICE and BEST_BID_PRICE can be used to calculate the Price Skew together with the BID_VWAP and ASK_VWAP.

import onetick.py as otp

data = otp.DataSource(db='BINANCE', tick_type='PRL')
data = data.ob_summary(size_max_fractional_digits=9,
                       bucket_interval=60,
                       max_depth_shares=0.5)
result = otp.run(data,
                 start=otp.dt(2026, 7, 28),
                 end=otp.dt(2026, 7, 29),
                 timezone='UTC',
                 symbols='BTCUSD')
result
Time BID_SIZE BID_VWAP BEST_BID_PRICE WORST_BID_PRICE NUM_BID_LEVELS ASK_SIZE ASK_VWAP BEST_ASK_PRICE WORST_ASK_PRICE NUM_ASK_LEVELS
0 2026-07-28 00:01:00 0.22377 63496.083279 63704.61 61456.58 27 0.21492 63941.122894 63729.88 66042.64 27
1 2026-07-28 00:02:00 0.20724 63431.012768 63661.82 60501.70 27 0.19659 63922.769244 63691.04 65999.89 26
2 2026-07-28 00:03:00 0.21396 63430.612190 63659.39 60501.70 29 0.19111 63926.366503 63687.06 65994.78 28
3 2026-07-28 00:04:00 0.21634 63456.893842 63676.17 60501.70 29 0.19814 63926.443466 63701.86 65960.17 28
4 2026-07-28 00:05:00 0.23383 63463.745030 63684.12 59985.88 34 0.21125 63926.795975 63715.60 65938.23 30
... ... ... ... ... ... ... ... ... ... ... ...
1435 2026-07-28 23:56:00 0.50000 63427.842243 63830.48 61500.00 108 0.50000 64565.474223 63852.92 67217.67 79
1436 2026-07-28 23:57:00 0.50000 63454.580934 63844.64 61500.00 108 0.50000 63911.337560 63865.15 63979.36 20
1437 2026-07-28 23:58:00 0.50000 63492.709298 63867.94 61596.75 107 0.50000 63932.208524 63893.97 63951.99 14
1438 2026-07-28 23:59:00 0.50000 63480.813434 63883.77 61500.00 110 0.50000 64286.294234 63910.23 66227.80 73
1439 2026-07-29 00:00:00 0.50000 63398.522961 63870.00 61500.00 108 0.50000 64120.600046 63871.39 66171.01 75

1440 rows × 11 columns