Skip to content

Commit 630bf44

Browse files
committed
[EN-1570] output in different color
1 parent 714d4e1 commit 630bf44

File tree

4 files changed

+153
-8
lines changed

4 files changed

+153
-8
lines changed

docs/_static/custom.css

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ table {
1919
overflow-x: scroll;
2020
}
2121

22-
.highlight {
22+
.highlight-text > .highlight > pre {
23+
background-color: wheat;
2324
display: block;
24-
max-height: 200px;
25+
max-height: 300px;
2526
overflow-y: auto;
2627
}

docs/basic_usage.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ e.g. connection address or status
5353
print(f'Connection status: {endpoint.connection_status}')
5454
5555
56-
.. parsed-literal::
56+
.. code-block:: text
5757
5858
Connected address: demo.dxfeed.com:7300
5959
Connection status: Connected and authorized
@@ -98,7 +98,7 @@ with how your provided date parsed automatically
9898
candle_sub = candle_sub.add_symbols(['AAPL', 'MSFT'])
9999
100100
101-
.. parsed-literal::
101+
.. code-block:: text
102102
103103
c:\job\python-api\dxfeed\wrappers\class_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
104104
warn(warn_message, UserWarning)
@@ -121,7 +121,7 @@ Subscription instance properties
121121
print(f'Cnadle subscription symbols: {candle_sub.symbols}')
122122
123123
124-
.. parsed-literal::
124+
.. code-block:: text
125125
126126
TimeAndSale subscription event type: TimeAndSale
127127
Cnadle subscription symbols: ['AAPL', 'MSFT']
@@ -139,7 +139,7 @@ handler that stores no data.
139139
print(f'Candle columns: {candle_handler.columns}')
140140
141141
142-
.. parsed-literal::
142+
.. code-block:: text
143143
144144
Trade columns: ['Symbol', 'Price', 'ExchangeCode', 'Size', 'Tick', 'Change', 'DayVolume', 'Time', 'IsETH']
145145
Candle columns: ['Symbol', 'Index', 'Time', 'Sequence', 'Count', 'Open', 'High', 'Low', 'Close', 'Volume', 'VWap', 'BidVolume', 'AskVolume', 'OpenInterest', 'ImpVolatility']
@@ -257,7 +257,7 @@ handler that stores no data.
257257
258258
259259
260-
.. parsed-literal::
260+
.. code-block:: text
261261
262262
[['MSFT',
263263
6838531241273198328,
@@ -325,7 +325,7 @@ Close connection
325325
print(f'Connection status: {endpoint.connection_status}')
326326
327327
328-
.. parsed-literal::
328+
.. code-block:: text
329329
330330
Connection status: Not connected
331331

docs/custom_handler.rst

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
.. _custom_handler:
2+
3+
Custom Event Handler
4+
====================
5+
6+
Here we will look into custom event handler implementation. Unlike the
7+
Basic Usage example, we will not pay much attention to anything behind
8+
the handler.
9+
10+
Handler is a special object inherited from dxfeed.EventHandler class. It
11+
is attached to the Subscription. When an event comes, internal
12+
structures call the update method of your handler with event as a
13+
parameter. event is a list with data specific to each event type. For
14+
example, for the Trade event type: ‘Symbol’, ‘Price’, ‘ExchangeCode’,
15+
‘Size’, ‘Tick’, ‘Change’, ‘DayVolume’, ‘Time’, ‘IsETH’.
16+
17+
After adding symbols or attaching a default listener (what is actually
18+
done implicitly in the first case) list of one-word descriptions of
19+
event data is stored in columns field of your handler object, attached
20+
to Subscription.
21+
22+
In this example, we will implement the event handler that prints price
23+
and volume changes for candle ask events. It also prints average volume
24+
change for the last 10 ask events.
25+
26+
Import package
27+
~~~~~~~~~~~~~~
28+
29+
.. code:: python3
30+
31+
import dxfeed as dx
32+
from datetime import datetime # for timed subscription
33+
from dateutil.relativedelta import relativedelta
34+
35+
Configure and create connection with Endpoint class
36+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37+
38+
Create instance of Endpoint class which will connect provided address.
39+
40+
.. code:: python3
41+
42+
endpoint = dx.Endpoint('demo.dxfeed.com:7300')
43+
44+
Configure and create subscription
45+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46+
47+
You should specify event type. For timed subscription (conflated stream)
48+
you should also provide time to start subscription from.
49+
50+
.. code:: python3
51+
52+
candle_sub = endpoint.create_subscription('Candle', date_time=datetime.now() - relativedelta(minutes=30))
53+
54+
.. code:: python3
55+
56+
class DiffHandler(dx.EventHandler):
57+
def __init__(self):
58+
self.__prev_open = 0
59+
self.__prev_high = 0
60+
self.__prev_low = 0
61+
self.__prev_close = 0
62+
self.__prev_volume = 0
63+
self.volume_changes = list()
64+
self.counter = 0
65+
66+
def update(self, event):
67+
if event[12] == event[12]: # AskVolume not nan
68+
print(f'Symbol: {event[0]}')
69+
print(f'Open changed by: {event[5] - self.__prev_open}')
70+
self.__prev_open = event[5]
71+
print(f'High changed by: {event[6] - self.__prev_high}')
72+
self.__prev_high = event[6]
73+
print(f'Open changed by: {event[7] - self.__prev_low}')
74+
self.__prev_low = event[7]
75+
print(f'Close changed by: {event[8] - self.__prev_close}')
76+
self.__prev_close = event[8]
77+
# Volume logic
78+
vol_change = event[12] - self.__prev_volume
79+
self.volume_changes.append(vol_change)
80+
self.counter +=1
81+
print(f'Volume changed by: {vol_change}, from {self.__prev_volume}, to {event[12]}')
82+
self.__prev_volume = event[12]
83+
print(f'Ask events processed: {self.counter}')
84+
print('-------------------')
85+
if self.counter % 10 == 0:
86+
print(f'Average volume change for 10 past ask events is: {sum(self.volume_changes) / len(self.volume_changes)}')
87+
self.volume_changes.clear()
88+
print('-------------------')
89+
90+
.. code:: python3
91+
92+
handler = DiffHandler()
93+
candle_sub.set_event_handler(handler).add_symbols(['AAPL'])
94+
95+
96+
.. code-block:: text
97+
98+
Symbol: AAPL
99+
Open changed by: 336.3
100+
High changed by: 336.3
101+
Open changed by: 336.3
102+
Close changed by: 336.3
103+
Volume changed by: 200.0, from 0, to 200.0
104+
Ask events processed: 1
105+
-------------------
106+
Symbol: AAPL
107+
Open changed by: 0.05989999999997053
108+
High changed by: 0.05989999999997053
109+
Open changed by: 0.05989999999997053
110+
Close changed by: 0.05989999999997053
111+
Volume changed by: -75.0, from 200.0, to 125.0
112+
Ask events processed: 2
113+
-------------------
114+
Symbol: AAPL
115+
Open changed by: -0.009899999999959164
116+
High changed by: -0.009899999999959164
117+
Open changed by: -0.009899999999959164
118+
Close changed by: -0.009899999999959164
119+
Volume changed by: -25.0, from 125.0, to 100.0
120+
Ask events processed: 3
121+
-------------------
122+
123+
124+
Close subscription
125+
~~~~~~~~~~~~~~~~~~
126+
127+
.. code:: python3
128+
129+
candle_sub.close_subscription()
130+
131+
Close connection
132+
~~~~~~~~~~~~~~~~
133+
134+
.. code:: python3
135+
136+
endpoint.close_connection()
137+
print(f'Connection status: {endpoint.connection_status}')
138+
139+
140+
.. code-block:: text
141+
142+
Connection status: Not connected
143+

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Table of contents:
3131
self
3232
installation.rst
3333
basic_usage.rst
34+
custom_handler.rst
3435
api.rst
3536
core_usage.rst
3637
custom_listener.rst

0 commit comments

Comments
 (0)