Skip to content

Commit e68587b

Browse files
committed
[EN-1328] core notebook formatted
1 parent b727044 commit e68587b

File tree

1 file changed

+67
-47
lines changed

1 file changed

+67
-47
lines changed

docs/core_usage.rst

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,83 +3,103 @@
33
Core functionality
44
==================
55

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+
~~~~~~~~~~~~~~~~~~~~~
88

9-
First of all you have to import the package:
9+
Here we deal with low level C styled api
1010

11-
.. code-block:: python
11+
.. code:: ipython3
1212
13-
import dxfeed.core.DXFeedPy as dxc
13+
from dxfeed.core import DXFeedPy as dxc
14+
import time # for timed suscription
1415
15-
Next, the connection to dxfeed server should be established:
16+
Create connection
17+
~~~~~~~~~~~~~~~~~
1618

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.
1822

19-
con = dxc.dxf_create_connection(address='demo.dxfeed.com:7300')
23+
.. code:: ipython3
2024
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')
2326
24-
.. code-block:: python
27+
Create subscription
28+
~~~~~~~~~~~~~~~~~~~
2529

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.
2834

29-
.. note::
35+
.. code:: ipython3
3036
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))
3339
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+
~~~~~~~~~~~~~~~
3842

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`.
4046

41-
dxc.dxf_attach_listener(sub1)
42-
dxc.dxf_attach_listener(sub2)
47+
.. code:: ipython3
4348
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)
4551
46-
.. code-block:: python
52+
Add tickers
53+
~~~~~~~~~~~
4754

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
5056

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
5358
54-
.. code-block:: python
59+
dxc.dxf_add_symbols(sub, ['AAPL', 'MSFT'])
60+
dxc.dxf_add_symbols(sub_timed, ['AAPL', 'C'])
5561
56-
sub1.get_data()
57-
sub2.get_data()
62+
Access data
63+
~~~~~~~~~~~
5864

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.
6168

62-
.. code-block:: python
69+
.. code:: ipython3
6370
64-
sub1.to_dataframe()
65-
sub2.to_dataframe()
71+
sub.get_data()
6672
67-
To stop receiving events just detach the listener:
73+
.. code:: ipython3
6874
69-
.. code-block:: python
75+
sub_timed.get_data()
7076
71-
dxc.dxf_detach_listener(sub1)
72-
dxc.dxf_detach_listener(sub2)
77+
Detach listener
78+
~~~~~~~~~~~~~~~
7379

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
7581

76-
.. code-block:: python
82+
.. code:: ipython3
7783
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)
8086
81-
Same with connection:
87+
Close connection
88+
~~~~~~~~~~~~~~~~
8289

83-
.. code-block:: python
90+
.. code:: ipython3
8491
8592
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

Comments
 (0)