|
3 | 3 | Basic Usage
|
4 | 4 | ===========
|
5 | 5 |
|
6 |
| -All the functions in C API have similar ones in Python with the same name. Not all arguments are |
7 |
| -supported by now, this work is in progress. |
| 6 | +Import package |
| 7 | +~~~~~~~~~~~~~~ |
8 | 8 |
|
9 |
| -First of all you have to import the package: |
10 |
| - |
11 |
| -.. code-block:: python |
| 9 | +.. code:: python3 |
12 | 10 |
|
13 | 11 | import dxfeed as dx
|
| 12 | + from datetime import datetime # for timed subscription |
| 13 | +
|
| 14 | +Configure and create connection with Endpoint class |
| 15 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 16 | + |
| 17 | +Create instance of Endpoint class which will connect provided address. |
| 18 | + |
| 19 | +.. code:: python3 |
| 20 | +
|
| 21 | + endpoint = dx.Endpoint('demo.dxfeed.com:7300') |
| 22 | +
|
| 23 | +Endpoint instance contains information about the connection, |
| 24 | +e.g. connection address or status |
| 25 | + |
| 26 | +.. code:: python3 |
| 27 | +
|
| 28 | + print(f'Connected address: {endpoint.address}') |
| 29 | + print(f'Connection status: {endpoint.connection_status}') |
| 30 | +
|
| 31 | +.. code:: text |
| 32 | +
|
| 33 | + Connected address: demo.dxfeed.com:7300 |
| 34 | + Connection status: Connected and authorized |
| 35 | +
|
| 36 | +Configure and create subscription |
| 37 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 38 | + |
| 39 | +You should specify event type. For timed subscription (conflated stream) |
| 40 | +you should also provide time to start subscription from. |
| 41 | + |
| 42 | +.. code:: python3 |
14 | 43 |
|
15 |
| -Next, the connection to dxfeed server should be established: |
| 44 | + trade_sub = endpoint.create_subscription('Trade', data_len=-1) |
16 | 45 |
|
17 |
| -.. code-block:: python |
| 46 | +**Attach default listener** - function that process incoming events |
18 | 47 |
|
19 |
| - con = dx.dxf_create_connection(address='demo.dxfeed.com:7300') |
| 48 | +.. code:: python3 |
20 | 49 |
|
21 |
| -To get events of certain types the subscription with this type should be |
22 |
| -create. One connection may have several subscriptions. |
| 50 | + trade_sub = trade_sub.attach_listener() |
23 | 51 |
|
24 |
| -.. code-block:: python |
| 52 | +**Add tikers** you want to recieve events for |
25 | 53 |
|
26 |
| - sub1 = dx.dxf_create_subscription(con, 'Trade') |
27 |
| - sub2 = dx.dxf_create_subscription(con, 'Quote') |
| 54 | +.. code:: python3 |
28 | 55 |
|
29 |
| -.. note:: |
| 56 | + trade_sub = trade_sub.add_symbols(['C', 'AAPL']) |
30 | 57 |
|
31 |
| - 'Trade', 'Quote', 'Summary', 'Profile', 'Order', 'TimeAndSale', 'Candle', 'TradeETH', 'SpreadOrder', |
32 |
| - 'Greeks', 'TheoPrice', 'Underlying', 'Series', 'Configuration' event types are supported. |
| 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 |
33 | 61 |
|
34 |
| -Special function called listener should be attached to the subscription to start receiving |
35 |
| -events. There are default listeners already implemented in dxpyfeed, but you |
36 |
| -can write your own with cython: :ref:`custom_listener`. To attach |
37 |
| -default listener just call `dxf_attach_listener` |
| 62 | +.. code:: python3 |
38 | 63 |
|
39 |
| -.. code-block:: python |
| 64 | + tns_sub = endpoint.create_subscription('TimeAndSale', date_time=datetime.now()) \ |
| 65 | + .attach_listener() \ |
| 66 | + .add_symbols(['AMZN']) |
40 | 67 |
|
41 |
| - dx.dxf_attach_listener(sub1) |
42 |
| - dx.dxf_attach_listener(sub2) |
| 68 | +.. code:: python3 |
43 | 69 |
|
44 |
| -Each subscription should be provided with tickers to get events for: |
| 70 | + candle_sub = endpoint.create_subscription('Candle', date_time='2020-04-16 13:05') |
| 71 | + candle_sub = candle_sub.attach_listener() |
| 72 | + candle_sub = candle_sub.add_symbols(['AAPL', 'MSFT']) |
45 | 73 |
|
46 |
| -.. code-block:: python |
| 74 | +Subscription instance properties |
| 75 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
47 | 76 |
|
48 |
| - dx.dxf_add_symbols(sub1, ['AAPL', 'MSFT']) |
49 |
| - dx.dxf_add_symbols(sub2, ['AAPL', 'C']) |
| 77 | +.. code:: python3 |
50 | 78 |
|
51 |
| -The data can be extracted with `get_data()` method. It is stored as dict with list of columns and list |
52 |
| -of events. Note that `get_data` extracts the data and then clean the field. To look at data call this property: |
| 79 | + print(f'Subscription event type: {tns_sub.event_type}') |
| 80 | + print(f'Subscription symbols: {candle_sub.symbols}') |
53 | 81 |
|
54 |
| -.. code-block:: python |
| 82 | +.. code:: text |
55 | 83 |
|
56 |
| - sub1.get_data() |
57 |
| - sub2.get_data() |
| 84 | + Subscription event type: TimeAndSale |
| 85 | + Subscription symbols: ['AAPL', 'MSFT'] |
58 | 86 |
|
59 |
| -The more convenient way to look at data is to convert it into pandas DataFrame. |
60 |
| -`to_dataframe` method of subscription class is responsible for that: |
| 87 | +Access data |
| 88 | +~~~~~~~~~~~ |
61 | 89 |
|
62 |
| -.. code-block:: python |
| 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. |
63 | 93 |
|
64 |
| - sub1.to_dataframe() |
65 |
| - sub2.to_dataframe() |
| 94 | +.. code:: python3 |
66 | 95 |
|
67 |
| -To stop receiving events just detach the listener: |
| 96 | + candle_sub.get_data() |
68 | 97 |
|
69 |
| -.. code-block:: python |
| 98 | +Close connection |
| 99 | +~~~~~~~~~~~~~~~~ |
70 | 100 |
|
71 |
| - dx.dxf_detach_listener(sub1) |
72 |
| - dx.dxf_detach_listener(sub2) |
| 101 | +.. code:: python3 |
73 | 102 |
|
74 |
| -When you are done with subscription you'd better close it: |
| 103 | + endpoint.close_connection() |
| 104 | + print(f'Connection status: {endpoint.connection_status}') |
75 | 105 |
|
76 |
| -.. code-block:: python |
| 106 | +.. code:: text |
77 | 107 |
|
78 |
| - dx.dxf_close_subscription(sub1) |
79 |
| - dx.dxf_close_subscription(sub2) |
| 108 | + Connection status: Not connected |
80 | 109 |
|
81 |
| -Same with connection: |
| 110 | +Transform data to pandas DataFrame |
| 111 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
82 | 112 |
|
83 |
| -.. code-block:: python |
| 113 | +.. code:: python3 |
84 | 114 |
|
85 |
| - dx.dxf_close_connection(con) |
| 115 | + trade_df = trade_sub.to_dataframe() |
| 116 | + tns_df = tns_sub.to_dataframe() |
| 117 | + candle_df = candle_sub.to_dataframe() |
0 commit comments