Basic Usage¶
Import package¶
import dxfeed as dx
from datetime import datetime # for timed subscription
Configure and create connection with Endpoint class¶
Create instance of Endpoint class which will connect provided address.
endpoint = dx.Endpoint('demo.dxfeed.com:7300')
Endpoint instance contains information about the connection, e.g. connection address or status
print(f'Connected address: {endpoint.address}')
print(f'Connection status: {endpoint.connection_status}')
Connected address: demo.dxfeed.com:7300
Connection status: Connected and authorized
Configure and create subscription¶
You should specify event type. For timed subscription (conflated stream) you should also provide time to start subscription from.
trade_sub = endpoint.create_subscription('Trade', data_len=-1)
Attach default listener - function that process incoming events
trade_sub = trade_sub.attach_listener()
Add tikers you want to recieve events for
trade_sub = trade_sub.add_symbols(['C', 'AAPL'])
For timed subscription you may provide either datetime object or string. String might be incomlete, in this case you will get warning with how your provided date parsed automatically
tns_sub = endpoint.create_subscription('TimeAndSale', date_time=datetime.now()) \
.attach_listener() \
.add_symbols(['AMZN'])
candle_sub = endpoint.create_subscription('Candle', date_time='2020-04-16 13:05')
candle_sub = candle_sub.attach_listener()
candle_sub = candle_sub.add_symbols(['AAPL', 'MSFT'])
Subscription instance properties¶
print(f'Subscription event type: {tns_sub.event_type}')
print(f'Subscription symbols: {candle_sub.symbols}')
Subscription event type: TimeAndSale
Subscription symbols: ['AAPL', 'MSFT']
Access data¶
Data is stored as deque. Its length is configured with data_len parameter and by default is 100000. When you call method below you extracts all data recieved to the moment and clears the buffer in class.
candle_sub.get_data()
Close connection¶
endpoint.close_connection()
print(f'Connection status: {endpoint.connection_status}')
Connection status: Not connected
Transform data to pandas DataFrame¶
trade_df = trade_sub.to_dataframe()
tns_df = tns_sub.to_dataframe()
candle_df = candle_sub.to_dataframe()