Low-level API¶
Import low-level functions¶
Here we deal with low level C styled api
from dxfeed.core import DXFeedPy as dxc
import time # for timed suscription
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(time.time() * 1000))
Attach listener¶
A special function that processes incoming events should be initialized. There are default ones for each event type. You can write a listener that will do your instructions. For details: Custom listener.
dxc.dxf_attach_listener(sub)
dxc.dxf_attach_listener(sub_timed)
Add tickers¶
Symbols that will be processed should be defined
dxc.dxf_add_symbols(sub, ['AAPL', 'MSFT'])
dxc.dxf_add_symbols(sub_timed, ['AAPL', 'C'])
Access data¶
Data is stored as deque in subscription class. Its length by default is 100000. When you call method below you extracts all data recieved to the moment and clears the buffer in class.
sub.get_data()
sub_timed.get_data()
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 = sub.to_dataframe()
df1.head()
df2 = sub_timed.to_dataframe()
df2.head()