Book Depth#

A series of simple examples are provided showing how to retrieve book depth from OneTick Cloud.

Book Depth Events#

Book depth events are retrieved by specifying either the PRL or PRL_FULL table, depending on whether Market by Level (MBL) or Market by Order (MBO) data is available. Typically book depth data is analysed using orderbook processing.

Book Depth Event Retrieval#

Book Depth Event Retrieval#
def book_depth_events():
    pt = otq.Passthrough().tick_type('PRL_FULL')
    graph = otq.Graph(pt)
    return graph

result = otq.run(book_depth_events(),
    http_address=rest_url,access_token=access_token,
    output_mode="pandas",
    start=datetime(2024,1,3,0,0,0),
    end = datetime(2024,1,3,9,30,0),
    timezone='Europe/London',
    symbols='LSE_SAMPLE::VOD'
    )
Book Depth Event Retrieval Results#

Time

UPDATE_TYPE

ORDER_ID

PART_ID

ORDER_TYPE

BUY_SELL_FLAG

PRICE

SIZE

RECORD_TYPE

TICK_STATUS

DELETED_TIME

OMDSEQ

0

2024-01-03 05:00:07.876

0

0.0

0

Z

0

1970-01-01 01:00:00

0

1

2024-01-03 05:00:07.876

A

233244094926020495

L

0

69.0

600

R

0

1970-01-01 01:00:00

0

2

2024-01-03 05:00:07.876

A

233244094926209592

L

0

69.0

5800

R

0

1970-01-01 01:00:00

1

3

2024-01-03 05:00:07.876

A

232452446553925487

L

0

67.2

50000

R

0

1970-01-01 01:00:00

2

4

2024-01-03 05:00:07.876

A

233244094925636785

L

0

67.0

10000

R

0

1970-01-01 01:00:00

3

5

2024-01-03 05:00:07.876

A

233244094925636407

L

0

66.94

7300

R

0

1970-01-01 01:00:00

4

6

2024-01-03 05:00:07.876

A

233244094925845524

L

0

66.66

20000

R

0

1970-01-01 01:00:00

5

7

2024-01-03 05:00:07.876

A

233244094925636400

L

0

66.34

7360

R

0

1970-01-01 01:00:00

6

8

2024-01-03 05:00:07.876

A

233244094925636417

L

0

66.0

3000

R

0

1970-01-01 01:00:00

7

9

2024-01-03 05:00:07.876

A

233244094925636406

L

0

65.0

70000

R

0

1970-01-01 01:00:00

8

Order Book Price Level Updates#

Updates to order books are retrieved by using the ObSnapshot object, specifying is_running_aggr=True and show_only_changes=True, along with defining the table which is either PRL or PRL_FULL , depending on whether Market by Level (MBL) or Market by Order (MBO) data is available. An initial state of the book is returned, following by deltas if show_only_changes=True. Alternatively each new book can be output if show_only_changes=False.

When using the ObSnapshot object, data is returned with each row representing a price level identified with the LEVEL field, and side identified with the BUY_SELL_FLAG field. PRICE and SIZE fields present the current price and remaining size or quantity of the price level.

Alternative representations are available using:

  • ObSnapshot for each record representing either an order, or a price level and side.

  • ObSnapshotWide for each record representing a price level with both Bid and Ask.

  • ObSnapshotFlat for each record represnting every level in the book

The returned results can be further filtered by additionally specifying:

  • Maximum Number of Price Levels to return through the max_levels attribute

  • Maximum Depth in Cumulative Size to return through the max_depth_shares attribute

  • Maximum Depth in % Price Skew from BBO to return through the max_depth_for_price attribute

Order Book Depth Updates Retrieval#
def ob_tick():
    ob = otq.ObSnapshot(is_running_aggr=True,show_only_changes=True).tick_type('PRL_FULL')
    graph = otq.Graph(ob)
    return graph

result = otq.run(ob_tick(),
    http_address=rest_url,access_token=access_token,
    output_mode="pandas",
    start=datetime(2024,1,3,8,0,0),
    end = datetime(2024,1,3,9,0,0),
    timezone='Europe/London',
    symbols='LSE_SAMPLE::VOD'
    )
Order Book Depth Updates Retrieval Results#

Time

PRICE

UPDATE_TIME

SIZE

LEVEL

BUY_SELL_FLAG

0

2024-01-03 08:00:00

59.3

2024-01-03 07:58:05.142

17048

1

1

1

2024-01-03 08:00:00

66.49

2024-01-03 07:50:48.472

24478

2

1

2

2024-01-03 08:00:00

68.0

2024-01-03 07:50:00.075

6000

3

1

3

2024-01-03 08:00:00

68.45

2024-01-03 07:59:58.750

2642

4

1

4

2024-01-03 08:00:00

68.57

2024-01-03 07:59:59.968

2642

5

1

5

2024-01-03 08:00:00

68.72

2024-01-03 07:59:58.061

2642

6

1

6

2024-01-03 08:00:00

68.95

2024-01-03 07:59:56.514

1743

7

1

7

2024-01-03 08:00:00

69.5

2024-01-03 07:59:58.978

1114

8

1

8

2024-01-03 08:00:00

69.64

2024-01-03 07:59:58.978

477

9

1

9

2024-01-03 08:00:00

69.66

2024-01-03 07:51:00.459

2

10

1

Order Book L3 Depth Updates#

For Level 3 Book depth stored in PRL_FULL tables, book updates can be retrieved at an order by order basis by specifying show_full_detail=True. In this case the ORDER_ID field will also be output in the results.

Order Book Full Depth Updates Retrieval#
def ob_tick():
    ob = otq.ObSnapshot(is_running_aggr=True,show_full_detail=True,show_only_changes=True).tick_type('PRL_FULL')
    graph = otq.Graph(ob)
    return graph

result = otq.run(ob_tick(),
    http_address=rest_url,access_token=access_token,
    output_mode="pandas",
    start=datetime(2024,1,3,8,0,0),
    end = datetime(2024,1,3,9,0,0),
    timezone='Europe/London',
    symbols='LSE_SAMPLE::VOD'
    )
Order Book Full Depth Updates Retrieval Results#

Time

UPDATE_TYPE

ORDER_ID

PART_ID

ORDER_TYPE

BUY_SELL_FLAG

PRICE

SIZE

RECORD_TYPE

TICK_STATUS

DELETED_TIME

OMDSEQ

LEVEL

UPDATE_TIME

0

2024-01-03 08:00:00

A

233402424600036698

L

1

59.3

90

R

0

1970-01-01 01:00:00

0

1

2024-01-03 07:50:10.362

1

2024-01-03 08:00:00

M

233402424600037169

L

1

59.3

7807

R

0

1970-01-01 01:00:00

0

1

2024-01-03 07:50:55.122

2

2024-01-03 08:00:00

A

233402424600038771

L

1

59.3

9151

R

0

1970-01-01 01:00:00

0

1

2024-01-03 07:58:05.142

3

2024-01-03 08:00:00

A

233402424600037126

L

1

66.49

24478

R

0

1970-01-01 01:00:00

7

2

2024-01-03 07:50:48.472

4

2024-01-03 08:00:00

A

233402424600035724

L

1

68.0

6000

R

0

1970-01-01 01:00:00

160

3

2024-01-03 07:50:00.075

5

2024-01-03 08:00:00

A

233402424600039933

L

1

68.45

2642

R

0

1970-01-01 01:00:00

100

4

2024-01-03 07:59:58.750

6

2024-01-03 08:00:00

A

233402424600040158

L

1

68.57

2642

R

0

1970-01-01 01:00:00

154

5

2024-01-03 07:59:59.968

7

2024-01-03 08:00:00

A

233402424600039860

L

1

68.72

2642

R

0

1970-01-01 01:00:00

77

6

2024-01-03 07:59:58.061

8

2024-01-03 08:00:00

A

233402424600039693

L

1

68.95

1743

R

0

1970-01-01 01:00:00

0

7

2024-01-03 07:59:56.514

9

2024-01-03 08:00:00

A

233402424600039989

L

1

69.5

1114

R

0

1970-01-01 01:00:00

3

8

2024-01-03 07:59:58.978

Order Book Depth Bars#

Order Book state can be returned at periodic intervals generating book depth bars, by specifying the bucket_interval attribute, whcih represents a time in seconds. Milliseconds are supported by specifying a decimal (e.g. 0.1)

As before, alternative representations are available using:

  • ObSnapshot for each record representing either an order, or a price level and side.

  • ObSnapshotWide for each record representing a price level with both Bid and Ask.

  • ObSnapshotFlat for each record represnting every level in the book

Order Book 1 Minute Bars to 5 Levels of Depth Retrieval#
def ob_bars():
    bars = otq.ObSnapshot(bucket_interval=60,max_levels=5).tick_type('PRL_FULL')
    graph = otq.Graph(bars)
    return graph

result = otq.run(ob_bars(),
    http_address=rest_url,access_token=access_token,
    output_mode="pandas",
    start=datetime(2024,1,3,8,0,0),
    end = datetime(2024,1,3,16,0,0),
    timezone='Europe/London',
    symbols='LSE_SAMPLE::VOD'
    )
Order Book 1 Minute Bars to 5 Levels of Depth Retrieval Results#

Symbol

Timestamp

PRICE

SIZE

SYMBOL_NAME

TICK_TYPE

UPDATE_TIME

LEVEL

BUY_SELL_FLAG

Time

PRICE

UPDATE_TIME

SIZE

LEVEL

BUY_SELL_FLAG

0

2024-01-03 08:01:00

70.45

2024-01-03 08:00:59.243

5657

1

1

1

2024-01-03 08:01:00

70.47

2024-01-03 08:00:59.792

12161

2

1

2

2024-01-03 08:01:00

70.48

2024-01-03 08:00:58.503

108029

3

1

3

2024-01-03 08:01:00

70.49

2024-01-03 08:00:59.262

27858

4

1

4

2024-01-03 08:01:00

70.5

2024-01-03 08:00:59.056

20344

5

1

5

2024-01-03 08:01:00

70.36

2024-01-03 08:00:59.796

247

1

0

6

2024-01-03 08:01:00

70.35

2024-01-03 08:00:59.243

100

2

0

7

2024-01-03 08:01:00

70.34

2024-01-03 08:00:59.792

6657

3

0

8

2024-01-03 08:01:00

70.33

2024-01-03 08:00:59.796

6377

4

0

9

2024-01-03 08:01:00

70.32

2024-01-03 08:00:58.187

6398

5

0

Order Book Wide (Bid & Ask on same row) 1 Minute Bars to 5 Levels of Depth Retrieval#
def ob_bars():
    bars = otq.ObSnapshotWide(bucket_interval=60,max_levels=5).tick_type('PRL_FULL')
    graph = otq.Graph(bars)
    return graph

result = otq.run(ob_bars(),
    http_address=rest_url,access_token=access_token,
    output_mode="pandas",
    start=datetime(2024,1,3,8,0,0),
    end = datetime(2024,1,3,16,0,0),
    timezone='Europe/London',
    symbols='LSE_SAMPLE::HSBA'
    )
Order Book Wide (Bid & Ask on same row) 1 Minute Bars to 5 Levels of Depth Retrieval Results#

Time

BID_PRICE

BID_UPDATE_TIME

BID_SIZE

ASK_PRICE

ASK_UPDATE_TIME

ASK_SIZE

LEVEL

0

2024-01-03 08:01:00

635.2

2024-01-03 08:00:59.775

1698

635.7

2024-01-03 08:00:59.776

1700

1

1

2024-01-03 08:01:00

635.1

2024-01-03 08:00:59.775

3210

635.8

2024-01-03 08:00:59.782

17859

2

2

2024-01-03 08:01:00

635.0

2024-01-03 08:00:59.739

6306

635.9

2024-01-03 08:00:59.154

2936

3

3

2024-01-03 08:01:00

634.9

2024-01-03 08:00:57.803

3631

636.0

2024-01-03 08:00:59.782

6419

4

4

2024-01-03 08:01:00

634.8

2024-01-03 08:00:55.889

4728

636.1

2024-01-03 08:00:59.761

7335

5

5

2024-01-03 08:02:00

635.6

2024-01-03 08:01:59.903

974

636.0

2024-01-03 08:01:59.286

389

1

6

2024-01-03 08:02:00

635.5

2024-01-03 08:01:59.330

3485

636.1

2024-01-03 08:01:59.893

3986

2

7

2024-01-03 08:02:00

635.4

2024-01-03 08:01:59.285

6735

636.2

2024-01-03 08:01:59.148

1014

3

8

2024-01-03 08:02:00

635.3

2024-01-03 08:01:59.146

4673

636.3

2024-01-03 08:01:59.148

3306

4

9

2024-01-03 08:02:00

635.2

2024-01-03 08:01:59.307

4822

636.4

2024-01-03 08:01:59.200

21442

5

Order Book Flat (All Levels on same row) 1 Minute Bars to 5 Levels of Depth Retrieval#
def ob_bars():
    bars = otq.ObSnapshotFlat(bucket_interval=60,max_levels=5).tick_type('PRL_FULL')
    graph = otq.Graph(bars)
    return graph

result = otq.run(ob_bars(),
    http_address=rest_url,access_token=access_token,
    output_mode="pandas",
    start=datetime(2024,1,3,8,0,0),
    end = datetime(2024,1,3,16,0,0),
    timezone='Europe/London',
    symbols='LSE_SAMPLE::VOD'
    )
Order Book Flat (All Levels on same row) 1 Minute Bars to 5 Levels of Depth Retrieval Results#

Time

BID_PRICE1

BID_UPDATE_TIME1

BID_SIZE1

ASK_PRICE1

ASK_UPDATE_TIME1

ASK_SIZE1

BID_PRICE2

BID_UPDATE_TIME2

BID_SIZE2

ASK_PRICE2

ASK_UPDATE_TIME2

ASK_SIZE2

BID_PRICE3

BID_UPDATE_TIME3

BID_SIZE3

ASK_PRICE3

ASK_UPDATE_TIME3

ASK_SIZE3

BID_PRICE4

BID_UPDATE_TIME4

BID_SIZE4

ASK_PRICE4

ASK_UPDATE_TIME4

ASK_SIZE4

BID_PRICE5

BID_UPDATE_TIME5

BID_SIZE5

ASK_PRICE5

ASK_UPDATE_TIME5

ASK_SIZE5

0

2024-01-03 08:01:00

70.36

2024-01-03 08:00:59.796

247

70.45

2024-01-03 08:00:59.243

5657

70.35

2024-01-03 08:00:59.243

100

70.47

2024-01-03 08:00:59.792

12161

70.34

2024-01-03 08:00:59.792

6657

70.48

2024-01-03 08:00:58.503

108029

70.33

2024-01-03 08:00:59.796

6377

70.49

2024-01-03 08:00:59.262

27858

70.32

2024-01-03 08:00:58.187

6398

70.5

2024-01-03 08:00:59.056

20344

1

2024-01-03 08:02:00

70.51

2024-01-03 08:01:59.461

100

70.6

2024-01-03 08:01:58.413

2236

70.5

2024-01-03 08:01:59.461

1528

70.61

2024-01-03 08:01:59.013

20872

70.49

2024-01-03 08:01:58.436

1401

70.62

2024-01-03 08:01:59.461

20687

70.48

2024-01-03 08:01:57.911

7556

70.63

2024-01-03 08:01:58.613

27494

70.47

2024-01-03 08:01:57.702

9783

70.64

2024-01-03 08:01:59.013

26717

2

2024-01-03 08:03:00

70.41

2024-01-03 08:02:47.187

6814

70.51

2024-01-03 08:02:47.190

5093

70.4

2024-01-03 08:02:58.047

12411

70.53

2024-01-03 08:02:50.346

4716

70.39

2024-01-03 08:02:53.018

6260

70.54

2024-01-03 08:02:50.395

16566

70.38

2024-01-03 08:02:53.664

15139

70.55

2024-01-03 08:02:46.535

24048

70.37

2024-01-03 08:02:53.228

33771

70.56

2024-01-03 08:02:50.395

29900

3

2024-01-03 08:04:00

70.39

2024-01-03 08:03:57.934

1381

70.49

2024-01-03 08:03:57.933

7154

70.38

2024-01-03 08:03:57.945

7012

70.5

2024-01-03 08:03:58.609

28055

70.37

2024-01-03 08:03:57.925

1529

70.51

2024-01-03 08:03:58.133

28303

70.36

2024-01-03 08:03:59.065

8369

70.52

2024-01-03 08:03:58.233

41389

70.35

2024-01-03 08:03:55.984

8462

70.53

2024-01-03 08:03:57.934

16198

4

2024-01-03 08:05:00

70.43

2024-01-03 08:04:59.431

2682

70.5

2024-01-03 08:04:54.898

4716

70.42

2024-01-03 08:04:54.544

7867

70.51

2024-01-03 08:04:54.306

5427

70.41

2024-01-03 08:04:54.543

6878

70.52

2024-01-03 08:04:59.502

19109

70.4

2024-01-03 08:04:54.638

5581

70.53

2024-01-03 08:04:57.025

28692

70.39

2024-01-03 08:04:55.976

23024

70.54

2024-01-03 08:04:54.870

19374

5

2024-01-03 08:06:00

70.36

2024-01-03 08:05:25.721

2330

70.42

2024-01-03 08:05:54.602

4440

70.34

2024-01-03 08:05:59.668

2385

70.43

2024-01-03 08:05:54.602

5700

70.33

2024-01-03 08:05:53.802

2400

70.45

2024-01-03 08:05:30.008

4144

70.32

2024-01-03 08:05:53.802

6420

70.46

2024-01-03 08:05:52.802

13645

70.31

2024-01-03 08:05:53.802

18988

70.47

2024-01-03 08:05:59.302

22078

6

2024-01-03 08:07:00

70.27

2024-01-03 08:06:43.573

14355

70.34

2024-01-03 08:06:51.495

2450

70.25

2024-01-03 08:06:44.309

4022

70.35

2024-01-03 08:06:59.300

23415

70.23

2024-01-03 08:06:50.346

27028

70.36

2024-01-03 08:06:43.807

29850

70.22

2024-01-03 08:06:59.550

14461

70.37

2024-01-03 08:06:44.409

27319

70.21

2024-01-03 08:06:44.285

27573

70.38

2024-01-03 08:06:52.094

14507

7

2024-01-03 08:08:00

70.34

2024-01-03 08:07:59.564

5400

70.41

2024-01-03 08:07:59.610

4716

70.33

2024-01-03 08:07:59.610

11226

70.42

2024-01-03 08:07:59.610

12311

70.32

2024-01-03 08:07:40.113

11903

70.43

2024-01-03 08:07:43.463

31079

70.31

2024-01-03 08:07:59.563

15814

70.44

2024-01-03 08:07:43.463

14756

70.3

2024-01-03 08:07:50.002

21272

70.45

2024-01-03 08:07:36.074

27306

8

2024-01-03 08:09:00

70.45

2024-01-03 08:08:59.397

100

70.51

2024-01-03 08:08:59.291

6124

70.44

2024-01-03 08:08:59.290

7265

70.52

2024-01-03 08:08:59.291

17069

70.43

2024-01-03 08:08:59.316

16077

70.53

2024-01-03 08:08:58.586

21711

70.42

2024-01-03 08:08:59.397

16407

70.54

2024-01-03 08:08:58.560

16231

70.41

2024-01-03 08:08:58.801

15789

70.55

2024-01-03 08:08:59.252

29697

9

2024-01-03 08:10:00

70.48

2024-01-03 08:09:55.303

4251

70.52

2024-01-03 08:09:55.302

4716

70.47

2024-01-03 08:09:59.798

100

70.53

2024-01-03 08:09:55.903

25407

70.46

2024-01-03 08:09:59.041

16130

70.54

2024-01-03 08:09:55.903

32314

70.45

2024-01-03 08:09:59.801

24564

70.55

2024-01-03 08:09:46.413

35360

70.44

2024-01-03 08:09:30.629

20566

70.57

2024-01-03 08:09:55.903

26962

Book Depth Statistics#

Statistics on book depth can be retrieved by using the ObSummary object, and specifying a book filter

  • Maximum Number of Price Levels to return through the max_levels attribute

  • Maximum Depth in Cumulative Size to return through the max_depth_shares attribute

  • Maximum Depth in % Price Skew from BBO to return through the max_depth_for_price attribute

Book depth statistics return:

  • BEST_BID_PRICE and BEST_ASK_PRICE

  • Cumulative BID_SIZE and ASK_SIZE

  • NUM_ASK_LEVELS and NUM_BID_LEVELS

  • WORST_BID_PRICE and WORST_ASK_PRICE

  • BID_VWAP and ASK_VWAP

Book Depth Statistics by minute to trade a quantity of 1000 Retrieval#
def ob_stats():
    ob = otq.ObSummary(bucket_interval=60,max_depth_shares=1000).tick_type('PRL_FULL')
    graph = otq.Graph(ob)
    return graph

result = otq.run(ob_stats(),
    http_address=rest_url,access_token=access_token,
    output_mode="pandas",
    start=datetime(2024,1,3,8,0,0),
    end = datetime(2024,1,3,16,0,0),
    timezone='Europe/London',
    symbols='LSE_SAMPLE::VOD'
    )
Book Depth Statistics by minute to trade a quantity of 1000 Retrieval Results#

Time

ASK_SIZE

BID_SIZE

ASK_VWAP

BID_VWAP

BEST_ASK_PRICE

BEST_BID_PRICE

WORST_ASK_PRICE

WORST_BID_PRICE

NUM_ASK_LEVELS

NUM_BID_LEVELS

0

2024-01-03 08:01:00

1000.0

1000.0

70.45

70.34593999999994

70.45

70.36

70.45

70.34

1

3

1

2024-01-03 08:02:00

1000.0

1000.0

70.59999999999998

70.501

70.6

70.51

70.6

70.5

1

2

2

2024-01-03 08:03:00

1000.0

1000.0

70.51000000000006

70.41

70.51

70.41

70.51

70.41

1

1

3

2024-01-03 08:04:00

1000.0

1000.0

70.49

70.39

70.49

70.39

70.49

70.39

1

1

4

2024-01-03 08:05:00

1000.0

1000.0

70.5

70.43

70.5

70.43

70.5

70.43

1

1

5

2024-01-03 08:06:00

1000.0

1000.0

70.41999999999997

70.35999999999999

70.42

70.36

70.42

70.36

1

1

6

2024-01-03 08:07:00

1000.0

1000.0

70.34

70.27

70.34

70.27

70.34

70.27

1

1

7

2024-01-03 08:08:00

1000.0

1000.0

70.41

70.34

70.41

70.34

70.41

70.34

1

1

8

2024-01-03 08:09:00

1000.0

1000.0

70.51

70.441

70.51

70.45

70.51

70.44

1

2

9

2024-01-03 08:10:00

1000.0

1000.0

70.52000000000002

70.48000000000003

70.52

70.48

70.52

70.48

1

1