Low-level API

Note

High-Level API have priority support in case of any issues. We highly recommend to use it and touch the Low-Level API only in special cases.

This tutorial is about the Cython level of the package, which connects Python and C API. Though high-level Python classes provide the necessary functionality safely and conveniently, the user is not restricted to use low-level functions.

Import core functions

Here we deal with low level C styled api, so the import is slightly differ.

from dxfeed.core import DXFeedPy as dxc
from dxfeed.core.utils.handler import DefaultHandler
from datetime import datetime  # for timed suscription
from dateutil.relativedelta import relativedelta

Create connection

There are two ways at the moment to create connection: with token or with specifying connection address. Here we use the latter for simplicity.

con = dxc.dxf_create_connection('demo.dxfeed.com:7300')

Create subscription

There are two types of subscriptions: ordinary for delivering stream data as-is and timed for conflated data. Except type of subscription you should provide type of events you want to get. Note: some event types, e.g. Candle, support only timed subscription.

sub = dxc.dxf_create_subscription(con, 'Trade')
sub_timed = dxc.dxf_create_subscription_timed(con, 'Candle', int((datetime.now() - relativedelta(days=3)).timestamp()))

Attach event handler

To process incoming data you have to define define update(event) method in your EventHandler child class. Or you may use DefaultHandler which stores upcoming data in deque of the length 100k. In this example we choose the latter.

trade_handler = DefaultHandler()
candle_handler = DefaultHandler()
sub.set_event_handler(trade_handler)
sub_timed.set_event_handler(candle_handler)

Attach listener

A special function that processes incoming on the C level events should be initialized. There are default ones for each event type.

dxc.dxf_attach_listener(sub)
dxc.dxf_attach_listener(sub_timed)

Add tickers

Symbols that will be processed should be defined. For Candle event type along with base symbol, you should specify an aggregation period. You can also set price type. More details: https://kb.dxfeed.com/display/DS/REST+API#RESTAPI-Candlesymbols.

dxc.dxf_add_symbols(sub, ['AAPL', 'MSFT'])
dxc.dxf_add_symbols(sub_timed, ['AAPL{=d}', 'MSFT{=d}'])

Access data

The DefaultHandler class has get_list() and get_dataframe() methods to access the data.

trade_handler.get_list()[-5:]
[['MSFT', 196.14, 'X', 100, 2, 0.0, 100.0, 1592510399515, 0],
 ['MSFT', 196.27, 'Y', 100, 2, 0.0, 18.0, 1592510398017, 0],
 ['MSFT', 196.33, 'Z', 100, 1, 0.0, 2693.0, 1592510399823, 0],
 ['AAPL', 351.57, 'D', 200, 1, 0.0, 44022.0, 1592510399435, 0],
 ['AAPL', 351.73, 'Q', 1406354, 1, 0.0, 234771.0, 1592510400351, 0]]
candle_handler.get_dataframe().head(3)
Symbol Index Time Sequence Count Open High Low Close Volume VWap BidVolume AskVolume OpenInterest ImpVolatility
0 AAPL{=d} 6839841934068940800 2020-06-19 0 807.0 354.05 355.55 353.35 354.79 184838.0 354.45447 75518.0 109320.0 0 0.3690
1 AAPL{=d} 6839470848894566400 2020-06-18 0 96172.0 351.41 353.45 349.22 351.73 24205096.0 351.56873 8565421.0 10394906.0 0 0.3673
2 AAPL{=d} 6839099763720192000 2020-06-17 0 110438.0 355.15 355.40 351.09 351.59 28601626.0 353.70998 10686232.0 12141490.0 0 0.3713

Detach listener

When you are no longer interested in recieving data detach the listener

dxc.dxf_detach_listener(sub)
dxc.dxf_detach_listener(sub_timed)

Close connection

dxc.dxf_close_connection(con)

Transform data to pandas DataFrame

df1 = trade_handler.get_dataframe()
df1.head(3)
Symbol Price ExchangeCode Size Tick Change DayVolume Time IsETH
0 AAPL 351.73 Q 1406354 1 0.0 234761.0 2020-06-18 20:00:00.351 0
1 AAPL 351.73 Q 1406354 1 0.0 41051.0 2020-06-18 20:00:00.351 0
2 MSFT 196.32 Q 2364517 2 0.0 160741.0 2020-06-18 20:00:00.327 0
df2 = candle_handler.get_dataframe()
df2.head(3)
Symbol Index Time Sequence Count Open High Low Close Volume VWap BidVolume AskVolume OpenInterest ImpVolatility
0 AAPL{=d} 6839841934068940800 2020-06-19 0 807.0 354.05 355.55 353.35 354.79 184838.0 354.45447 75518.0 109320.0 0 0.3690
1 AAPL{=d} 6839470848894566400 2020-06-18 0 96172.0 351.41 353.45 349.22 351.73 24205096.0 351.56873 8565421.0 10394906.0 0 0.3673
2 AAPL{=d} 6839099763720192000 2020-06-17 0 110438.0 355.15 355.40 351.09 351.59 28601626.0 353.70998 10686232.0 12141490.0 0 0.3713