Skip to content

Commit

Permalink
Merge pull request #31 from dxFeed/EN-1939-correct-examples
Browse files Browse the repository at this point in the history
[EN-1939] Custom handler example correction
  • Loading branch information
alimantu authored Sep 4, 2020
2 parents a12bd07 + abcc307 commit cbd0b6c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 63 deletions.
63 changes: 32 additions & 31 deletions docs/custom_handler.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,39 @@ you should also provide time to start subscription from.
self.volume_changes = DequeWithLock()
self.counter = 0
def update(self, event):
if not np.isnan(event.ask_volume): # AskVolume not nan
self.counter += 1
print(f'Symbol: {event.symbol}')
if self.counter == 1:
self.__prev_open = event.open
self.__prev_high = event.high
self.__prev_low = event.low
self.__prev_close = event.close
self.__prev_volume = event.ask_volume
print('First event processed')
print('-------------------')
else:
print(f'Open changed by: {event.open - self.__prev_open}')
self.__prev_open = event.open
print(f'High changed by: {event.high - self.__prev_high}')
self.__prev_high = event.high
print(f'Open changed by: {event.low - self.__prev_low}')
self.__prev_low = event.low
print(f'Close changed by: {event.close - self.__prev_close}')
self.__prev_close = event.close
# Volume logic
vol_change = event.ask_volume - self.__prev_volume
self.volume_changes.safe_append(vol_change)
print(f'Volume changed by: {vol_change}, from {self.__prev_volume}, to {event.ask_volume}')
self.__prev_volume = event.ask_volume
print(f'Ask events prcessed: {self.counter}')
print('-------------------')
if self.counter % 10 == 0:
print(f'Average volume change for 10 past ask events is: {sum(self.volume_changes) / len(self.volume_changes)}')
self.volume_changes.clear()
def update(self, events):
for event in events:
if not np.isnan(event.ask_volume): # AskVolume not nan
self.counter += 1
print(f'Symbol: {event.symbol}')
if self.counter == 1:
self.__prev_open = event.open
self.__prev_high = event.high
self.__prev_low = event.low
self.__prev_close = event.close
self.__prev_volume = event.ask_volume
print('First event processed')
print('-------------------')
else:
print(f'Open changed by: {event.open - self.__prev_open}')
self.__prev_open = event.open
print(f'High changed by: {event.high - self.__prev_high}')
self.__prev_high = event.high
print(f'Open changed by: {event.low - self.__prev_low}')
self.__prev_low = event.low
print(f'Close changed by: {event.close - self.__prev_close}')
self.__prev_close = event.close
# Volume logic
vol_change = event.ask_volume - self.__prev_volume
self.volume_changes.safe_append(vol_change)
print(f'Volume changed by: {vol_change}, from {self.__prev_volume}, to {event.ask_volume}')
self.__prev_volume = event.ask_volume
print(f'Ask events prcessed: {self.counter}')
print('-------------------')
if self.counter % 10 == 0:
print(f'Average volume change for 10 past ask events is: {sum(self.volume_changes) / len(self.volume_changes)}')
self.volume_changes.clear()
print('-------------------')
For Candle event type along with base symbol, you should specify an
aggregation period. You can also set price type. More details:
Expand Down
65 changes: 33 additions & 32 deletions examples/CustomHandlerExample.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -85,38 +85,39 @@
" self.volume_changes = DequeWithLock()\n",
" self.counter = 0\n",
" \n",
" def update(self, event):\n",
" if not np.isnan(event.ask_volume): # AskVolume not nan\n",
" self.counter += 1\n",
" print(f'Symbol: {event.symbol}')\n",
" if self.counter == 1:\n",
" self.__prev_open = event.open\n",
" self.__prev_high = event.high\n",
" self.__prev_low = event.low\n",
" self.__prev_close = event.close\n",
" self.__prev_volume = event.ask_volume\n",
" print('First event processed')\n",
" print('-------------------')\n",
" else:\n",
" print(f'Open changed by: {event.open - self.__prev_open}')\n",
" self.__prev_open = event.open\n",
" print(f'High changed by: {event.high - self.__prev_high}')\n",
" self.__prev_high = event.high\n",
" print(f'Open changed by: {event.low - self.__prev_low}')\n",
" self.__prev_low = event.low\n",
" print(f'Close changed by: {event.close - self.__prev_close}')\n",
" self.__prev_close = event.close\n",
" # Volume logic\n",
" vol_change = event.ask_volume - self.__prev_volume\n",
" self.volume_changes.safe_append(vol_change)\n",
" print(f'Volume changed by: {vol_change}, from {self.__prev_volume}, to {event.ask_volume}')\n",
" self.__prev_volume = event.ask_volume\n",
" print(f'Ask events prcessed: {self.counter}')\n",
" print('-------------------')\n",
" if self.counter % 10 == 0:\n",
" print(f'Average volume change for 10 past ask events is: {sum(self.volume_changes) / len(self.volume_changes)}')\n",
" self.volume_changes.clear()\n",
" print('-------------------')"
" def update(self, events):\n",
" for event in events:\n",
" if not np.isnan(event.ask_volume): # AskVolume not nan\n",
" self.counter += 1\n",
" print(f'Symbol: {event.symbol}')\n",
" if self.counter == 1:\n",
" self.__prev_open = event.open\n",
" self.__prev_high = event.high\n",
" self.__prev_low = event.low\n",
" self.__prev_close = event.close\n",
" self.__prev_volume = event.ask_volume\n",
" print('First event processed')\n",
" print('-------------------')\n",
" else:\n",
" print(f'Open changed by: {event.open - self.__prev_open}')\n",
" self.__prev_open = event.open\n",
" print(f'High changed by: {event.high - self.__prev_high}')\n",
" self.__prev_high = event.high\n",
" print(f'Open changed by: {event.low - self.__prev_low}')\n",
" self.__prev_low = event.low\n",
" print(f'Close changed by: {event.close - self.__prev_close}')\n",
" self.__prev_close = event.close\n",
" # Volume logic\n",
" vol_change = event.ask_volume - self.__prev_volume\n",
" self.volume_changes.safe_append(vol_change)\n",
" print(f'Volume changed by: {vol_change}, from {self.__prev_volume}, to {event.ask_volume}')\n",
" self.__prev_volume = event.ask_volume\n",
" print(f'Ask events prcessed: {self.counter}')\n",
" print('-------------------')\n",
" if self.counter % 10 == 0:\n",
" print(f'Average volume change for 10 past ask events is: {sum(self.volume_changes) / len(self.volume_changes)}')\n",
" self.volume_changes.clear()\n",
" print('-------------------')"
]
},
{
Expand Down

0 comments on commit cbd0b6c

Please sign in to comment.