33Basic Usage
44===========
55
6+
7+ There are three levels in the dxfeed package. The lowest is the C API
8+ library, the highest is Python wrapper classes. Cython level in the
9+ middle aims to connect these two. Here we are going to look into Python
10+ level.
11+
12+ Python level, in its turn, mainly consists of three class types. The
13+ first one is the Endpoint. This class is responsible for connection
14+ management.
15+
16+ The Endpoint is also responsible for creating dependent classes, for
17+ example Subscription. One Endpoint may have several different
18+ Subscriptions, but each Subscription is related to one Endpoint. This
19+ class sets the type of subscription (stream or timed), the type of
20+ events (e.g. Trade, Candle), etc.
21+
22+ After you specified the data you want to receive, you have to specify
23+ how to process upcoming events. This is where the EventHandler class and
24+ its children come into play. Every time an event arrives Cython event
25+ listener will call ``self.update(event) `` method. You have to inherit
26+ from the EventHandler class and redefine the update method. Or you may
27+ use DefaultHandler which stores upcoming data in deque of the length
28+ 100k.
29+
630Import package
731~~~~~~~~~~~~~~
832
@@ -28,10 +52,12 @@ e.g. connection address or status
2852 print(f'Connected address: {endpoint.address}')
2953 print(f'Connection status: {endpoint.connection_status}')
3054
31- .. code :: text
55+
56+ .. parsed-literal ::
3257
3358 Connected address: demo.dxfeed.com:7300
3459 Connection status: Connected and authorized
60+
3561
3662 Configure and create subscription
3763~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -41,59 +67,254 @@ you should also provide time to start subscription from.
4167
4268.. code :: python3
4369
44- trade_sub = endpoint.create_subscription('Trade', data_len=-1 )
70+ trade_sub = endpoint.create_subscription('Trade')
4571
46- **Attach default listener ** - function that process incoming events
72+ **Set event handler ** - class that process incoming events. Here we use
73+ default one
4774
4875.. code :: python3
4976
50- trade_sub = trade_sub.attach_listener()
77+ trade_handler = dx.DefaultHandler()
78+ trade_sub.set_event_handler(trade_handler);
5179
5280 **Add tikers ** you want to recieve events for
5381
5482.. code :: python3
5583
56- trade_sub = trade_sub.add_symbols(['C', 'AAPL '])
84+ trade_sub = trade_sub.add_symbols(['C', 'IBM '])
5785
58- For timed subscription you may provide either datetime object or string.
59- String might be incomlete, in this case you will get warning with how
60- your provided date parsed automatically
86+ For timed subscription you should provide either datetime object or
87+ string. String might be incomlete, in this case you will get warning
88+ with how your provided date parsed automatically
6189
6290.. code :: python3
6391
6492 tns_sub = endpoint.create_subscription('TimeAndSale', date_time=datetime.now()) \
65- .attach_listener() \
6693 .add_symbols(['AMZN'])
6794
6895 .. code :: python3
6996
7097 candle_sub = endpoint.create_subscription('Candle', date_time='2020-04-16 13:05')
71- candle_sub = candle_sub.attach_listener()
7298 candle_sub = candle_sub.add_symbols(['AAPL', 'MSFT'])
7399
100+
101+ .. parsed-literal ::
102+
103+ c:\j ob\p ython-api\d xfeed\w rappers\c lass_utils.py:38: UserWarning: Datetime argument does not exactly match %Y-%m-%d %H:%M:%S.%f format, date was parsed automatically as 2020-04-16 13:05:00.000000
104+ warn(warn_message, UserWarning)
105+
106+
107+ **Note ** Two previous subscriptions attached DefaultHandler implicitly.
108+ To retrieve instances just call ``get_event_handler() `` method.
109+
110+ .. code :: python3
111+
112+ tns_handler = tns_sub.get_event_handler()
113+ candle_handler = candle_sub.get_event_handler()
114+
74115 Subscription instance properties
75116^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
76117
77118.. code :: python3
78119
79- print(f'Subscription event type: {tns_sub.event_type}')
80- print(f'Subscription symbols: {candle_sub.symbols}')
120+ print(f'TimeAndSale subscription event type: {tns_sub.event_type}')
121+ print(f'Cnadle subscription symbols: {candle_sub.symbols}')
122+
123+
124+ .. parsed-literal ::
125+
126+ TimeAndSale subscription event type: TimeAndSale
127+ Cnadle subscription symbols: ['AAPL', 'MSFT']
128+
129+
130+ Access data from DefaultHandler instance
131+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132+
133+ You can get colums, list or dataframe. You are also allowed to write
134+ handler that stores no data.
135+
136+ .. code :: python3
137+
138+ print(f'Trade columns: {trade_handler.columns}')
139+ print(f'Candle columns: {candle_handler.columns}')
140+
81141
82- .. code :: text
142+ .. parsed-literal ::
83143
84- Subscription event type: TimeAndSale
85- Subscription symbols: ['AAPL', 'MSFT']
144+ Trade columns: ['Symbol', 'Price', 'ExchangeCode', 'Size', 'Tick', 'Change', 'DayVolume', 'Time', 'IsETH']
145+ Candle columns: ['Symbol', 'Index', 'Time', 'Sequence', 'Count', 'Open', 'High', 'Low', 'Close', 'Volume', 'VWap', 'BidVolume', 'AskVolume', 'OpenInterest', 'ImpVolatility']
146+
147+
148+ .. code :: python3
149+
150+ candle_handler.get_dataframe().head(3)
151+
152+
153+
154+
155+ .. raw :: html
156+
157+ <div >
158+ <style scoped >
159+ .dataframe tbody tr th :only-of-type {
160+ vertical-align : middle ;
161+ }
162+
163+ .dataframe tbody tr th {
164+ vertical-align : top ;
165+ }
166+
167+ .dataframe thead th {
168+ text-align : right ;
169+ }
170+ </style >
171+ <table border =" 1" class =" dataframe" >
172+ <thead >
173+ <tr style =" text-align : right ;" >
174+ <th ></th >
175+ <th >Symbol</th >
176+ <th >Index</th >
177+ <th >Time</th >
178+ <th >Sequence</th >
179+ <th >Count</th >
180+ <th >Open</th >
181+ <th >High</th >
182+ <th >Low</th >
183+ <th >Close</th >
184+ <th >Volume</th >
185+ <th >VWap</th >
186+ <th >BidVolume</th >
187+ <th >AskVolume</th >
188+ <th >OpenInterest</th >
189+ <th >ImpVolatility</th >
190+ </tr >
191+ </thead >
192+ <tbody >
193+ <tr >
194+ <th >0</th >
195+ <td >MSFT</td >
196+ <td >6838531241273198328</td >
197+ <td >2020-06-15 11:13:50.566</td >
198+ <td >1784</td >
199+ <td >1.0</td >
200+ <td >184.17</td >
201+ <td >184.17</td >
202+ <td >184.17</td >
203+ <td >184.17</td >
204+ <td >635.0</td >
205+ <td >184.17</td >
206+ <td >635.0</td >
207+ <td >NaN</td >
208+ <td >0</td >
209+ <td >NaN</td >
210+ </tr >
211+ <tr >
212+ <th >1</th >
213+ <td >MSFT</td >
214+ <td >6838531241273198326</td >
215+ <td >2020-06-15 11:13:50.566</td >
216+ <td >1782</td >
217+ <td >1.0</td >
218+ <td >184.17</td >
219+ <td >184.17</td >
220+ <td >184.17</td >
221+ <td >184.17</td >
222+ <td >100.0</td >
223+ <td >184.17</td >
224+ <td >100.0</td >
225+ <td >NaN</td >
226+ <td >0</td >
227+ <td >NaN</td >
228+ </tr >
229+ <tr >
230+ <th >2</th >
231+ <td >MSFT</td >
232+ <td >6838531058896471782</td >
233+ <td >2020-06-15 11:13:08.092</td >
234+ <td >1766</td >
235+ <td >1.0</td >
236+ <td >184.17</td >
237+ <td >184.17</td >
238+ <td >184.17</td >
239+ <td >184.17</td >
240+ <td >100.0</td >
241+ <td >184.17</td >
242+ <td >100.0</td >
243+ <td >NaN</td >
244+ <td >0</td >
245+ <td >NaN</td >
246+ </tr >
247+ </tbody >
248+ </table >
249+ </div >
86250
87- Access data
88- ~~~~~~~~~~~
89251
90- Data is stored as deque. Its length is configured with data_len
91- parameter and by default is 100000. When you call method below you
92- extracts all data recieved to the moment and clears the buffer in class.
93252
94253.. code :: python3
95254
96- candle_sub.get_data()
255+ candle_handler.get_list()[:3]
256+
257+
258+
259+
260+ .. parsed-literal ::
261+
262+ [['MSFT',
263+ 6838531241273198328,
264+ 1592219630566,
265+ 1784,
266+ 1.0,
267+ 184.17,
268+ 184.17,
269+ 184.17,
270+ 184.17,
271+ 635.0,
272+ 184.17,
273+ 635.0,
274+ nan,
275+ 0,
276+ nan],
277+ ['MSFT',
278+ 6838531241273198326,
279+ 1592219630566,
280+ 1782,
281+ 1.0,
282+ 184.17,
283+ 184.17,
284+ 184.17,
285+ 184.17,
286+ 100.0,
287+ 184.17,
288+ 100.0,
289+ nan,
290+ 0,
291+ nan],
292+ ['MSFT',
293+ 6838531058896471782,
294+ 1592219588092,
295+ 1766,
296+ 1.0,
297+ 184.17,
298+ 184.17,
299+ 184.17,
300+ 184.17,
301+ 100.0,
302+ 184.17,
303+ 100.0,
304+ nan,
305+ 0,
306+ nan]]
307+
308+
309+
310+ Close subscription
311+ ~~~~~~~~~~~~~~~~~~
312+
313+ .. code :: python3
314+
315+ trade_sub.close_subscription()
316+ tns_sub.close_subscription()
317+ candle_sub.close_subscription()
97318
98319 Close connection
99320~~~~~~~~~~~~~~~~
@@ -103,15 +324,8 @@ Close connection
103324 endpoint.close_connection()
104325 print(f'Connection status: {endpoint.connection_status}')
105326
106- .. code :: text
107-
108- Connection status: Not connected
109327
110- Transform data to pandas DataFrame
111- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
328+ .. parsed-literal ::
112329
113- .. code :: python3
114-
115- trade_df = trade_sub.to_dataframe()
116- tns_df = tns_sub.to_dataframe()
117- candle_df = candle_sub.to_dataframe()
330+ Connection status: Not connected
331+
0 commit comments