From 73faafcfc13d98a6f1085ac39c03e4a20d5d4348 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Fri, 4 Feb 2022 16:46:48 -0500 Subject: [PATCH] Add trade "rates" (i.e. trade counts) support B) Though it's not per-tick accurate, accumulate the number of "trades" (i.e. the "clearing rate" - maybe this is a better name?) per bar inside the `dolla_vlm` fsp and average and report wmas of this in the `flow_rates` fsp. --- piker/fsp/_volume.py | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/piker/fsp/_volume.py b/piker/fsp/_volume.py index 1170fcc9..6ad04403 100644 --- a/piker/fsp/_volume.py +++ b/piker/fsp/_volume.py @@ -115,7 +115,12 @@ async def tina_vwap( @fsp( - outputs=('dolla_vlm', 'dark_vlm'), + outputs=( + 'dolla_vlm', + 'dark_vlm', + 'trade_count', + 'dark_trade_count', + ), curve_style='step', ) async def dolla_vlm( @@ -145,8 +150,8 @@ async def dolla_vlm( } i = ohlcv.index - output = vlm = 0 - dvlm = 0 + output = dvlm = vlm = 0 + dark_trade_count = trade_count = 0 async for quote in source: for tick in iterticks( @@ -165,8 +170,8 @@ async def dolla_vlm( li = ohlcv.index if li > i: i = li - vlm = 0 - dvlm = 0 + trade_count = dark_trade_count = dvlm = vlm = 0 + # TODO: for marginned instruments (futes, etfs?) we need to # show the margin $vlm by multiplying by whatever multiplier @@ -178,13 +183,17 @@ async def dolla_vlm( # print(f'dark_trade: {tick}') key = 'dark_vlm' dvlm += price * size - output = dvlm + yield 'dark_vlm', dvlm + dark_trade_count += 1 + yield 'dark_trade_count', dark_trade_count else: # print(f'vlm: {tick}') key = 'dolla_vlm' vlm += price * size - output = vlm + yield 'dolla_vlm', vlm + trade_count += 1 + yield 'trade_count', vlm # TODO: plot both to compare? # c, h, l, v = ohlcv.last()[ @@ -193,8 +202,6 @@ async def dolla_vlm( # tina_lvlm = c+h+l/3 * v # print(f' tinal vlm: {tina_lvlm}') - yield key, output - @fsp( # TODO: eventually I guess we should support some kinda declarative @@ -287,6 +294,12 @@ async def flow_rates( weights=weights, ) yield 'dvlm_rate', dvlm_wma[-1] + trade_rate_wma = _wma( + dvlm_shm.array['trade_count'], + period, + weights=weights, + ) + yield 'trade_rate', trade_rate_wma[-1] # TODO: skip this if no dark vlm is declared # by symbol info (eg. in crypto$) @@ -297,6 +310,13 @@ async def flow_rates( ) yield 'dark_dvlm_rate', dark_dvlm_wma[-1] + dark_trade_rate_wma = _wma( + dvlm_shm.array['dark_trade_count'], + period, + weights=weights, + ) + yield 'dark_trade_rate', dark_trade_rate_wma[-1] + # XXX: ib specific schema we should # probably pre-pack ourselves. # tr = quote.get('tradeRate')