|
3 | 3 | Core functionality |
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 core functions |
| 7 | +~~~~~~~~~~~~~~~~~~~~~ |
8 | 8 |
|
9 | | -First of all you have to import the package: |
| 9 | +Here we deal with low level C styled api |
10 | 10 |
|
11 | | -.. code-block:: python |
| 11 | +.. code:: ipython3 |
12 | 12 |
|
13 | | - import dxfeed.core.DXFeedPy as dxc |
| 13 | + from dxfeed.core import DXFeedPy as dxc |
| 14 | + import time # for timed suscription |
14 | 15 |
|
15 | | -Next, the connection to dxfeed server should be established: |
| 16 | +Create connection |
| 17 | +~~~~~~~~~~~~~~~~~ |
16 | 18 |
|
17 | | -.. code-block:: python |
| 19 | +There are two ways at the moment to create connection: with token or |
| 20 | +with specifying connection address. Here we use the latter for |
| 21 | +simplicity. |
18 | 22 |
|
19 | | - con = dxc.dxf_create_connection(address='demo.dxfeed.com:7300') |
| 23 | +.. code:: ipython3 |
20 | 24 |
|
21 | | -To get events of certain types the subscription with this type should be |
22 | | -create. One connection may have several subscriptions. |
| 25 | + con = dxc.dxf_create_connection('demo.dxfeed.com:7300') |
23 | 26 |
|
24 | | -.. code-block:: python |
| 27 | +Create subscription |
| 28 | +~~~~~~~~~~~~~~~~~~~ |
25 | 29 |
|
26 | | - sub1 = dxc.dxf_create_subscription(con, 'Trade') |
27 | | - sub2 = dxc.dxf_create_subscription(con, 'Quote') |
| 30 | +There are two types of subscriptions: ordinary for delivering stream |
| 31 | +data as-is and timed for conflated data. Except type of subscription you |
| 32 | +should provide type of events you want to get. Note: some event types, |
| 33 | +e.g. Candle, support only timed subscription. |
28 | 34 |
|
29 | | -.. note:: |
| 35 | +.. code:: ipython3 |
30 | 36 |
|
31 | | - 'Trade', 'Quote', 'Summary', 'Profile', 'Order', 'TimeAndSale', 'Candle', 'TradeETH', 'SpreadOrder', |
32 | | - 'Greeks', 'TheoPrice', 'Underlying', 'Series', 'Configuration' event types are supported. |
| 37 | + sub = dxc.dxf_create_subscription(con, 'Trade') |
| 38 | + sub_timed = dxc.dxf_create_subscription_timed(con, 'Candle', int(time.time() * 1000)) |
33 | 39 |
|
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` |
| 40 | +Attach listener |
| 41 | +~~~~~~~~~~~~~~~ |
38 | 42 |
|
39 | | -.. code-block:: python |
| 43 | +A special function that processes incoming events should be initialized. |
| 44 | +There are default ones for each event type. You can write a listener that will do your instructions. |
| 45 | +For details: :ref:`custom_listener`. |
40 | 46 |
|
41 | | - dxc.dxf_attach_listener(sub1) |
42 | | - dxc.dxf_attach_listener(sub2) |
| 47 | +.. code:: ipython3 |
43 | 48 |
|
44 | | -Each subscription should be provided with tickers to get events for: |
| 49 | + dxc.dxf_attach_listener(sub) |
| 50 | + dxc.dxf_attach_listener(sub_timed) |
45 | 51 |
|
46 | | -.. code-block:: python |
| 52 | +Add tickers |
| 53 | +~~~~~~~~~~~ |
47 | 54 |
|
48 | | - dxc.dxf_add_symbols(sub1, ['AAPL', 'MSFT']) |
49 | | - dxc.dxf_add_symbols(sub2, ['AAPL', 'C']) |
| 55 | +Symbols that will be processed should be defined |
50 | 56 |
|
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: |
| 57 | +.. code:: ipython3 |
53 | 58 |
|
54 | | -.. code-block:: python |
| 59 | + dxc.dxf_add_symbols(sub, ['AAPL', 'MSFT']) |
| 60 | + dxc.dxf_add_symbols(sub_timed, ['AAPL', 'C']) |
55 | 61 |
|
56 | | - sub1.get_data() |
57 | | - sub2.get_data() |
| 62 | +Access data |
| 63 | +~~~~~~~~~~~ |
58 | 64 |
|
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: |
| 65 | +Data is stored as deque in subscription class. Its length by default is |
| 66 | +100000. When you call method below you extracts all data recieved to the |
| 67 | +moment and clears the buffer in class. |
61 | 68 |
|
62 | | -.. code-block:: python |
| 69 | +.. code:: ipython3 |
63 | 70 |
|
64 | | - sub1.to_dataframe() |
65 | | - sub2.to_dataframe() |
| 71 | + sub.get_data() |
66 | 72 |
|
67 | | -To stop receiving events just detach the listener: |
| 73 | +.. code:: ipython3 |
68 | 74 |
|
69 | | -.. code-block:: python |
| 75 | + sub_timed.get_data() |
70 | 76 |
|
71 | | - dxc.dxf_detach_listener(sub1) |
72 | | - dxc.dxf_detach_listener(sub2) |
| 77 | +Detach listener |
| 78 | +~~~~~~~~~~~~~~~ |
73 | 79 |
|
74 | | -When you are done with subscription you'd better close it: |
| 80 | +When you are no longer interested in recieving data detach the listener |
75 | 81 |
|
76 | | -.. code-block:: python |
| 82 | +.. code:: ipython3 |
77 | 83 |
|
78 | | - dxc.dxf_close_subscription(sub1) |
79 | | - dxc.dxf_close_subscription(sub2) |
| 84 | + dxc.dxf_detach_listener(sub) |
| 85 | + dxc.dxf_detach_listener(sub_timed) |
80 | 86 |
|
81 | | -Same with connection: |
| 87 | +Close connection |
| 88 | +~~~~~~~~~~~~~~~~ |
82 | 89 |
|
83 | | -.. code-block:: python |
| 90 | +.. code:: ipython3 |
84 | 91 |
|
85 | 92 | dxc.dxf_close_connection(con) |
| 93 | +
|
| 94 | +Transform data to pandas DataFrame |
| 95 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 96 | + |
| 97 | +.. code:: ipython3 |
| 98 | +
|
| 99 | + df1 = sub.to_dataframe() |
| 100 | + df1.head() |
| 101 | +
|
| 102 | +.. code:: ipython3 |
| 103 | +
|
| 104 | + df2 = sub_timed.to_dataframe() |
| 105 | + df2.head() |
0 commit comments