|
| 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 | + |
0 commit comments