Skip to content

Commit

Permalink
kraken: drop OHLC.ticks field and just inject to quote before send
Browse files Browse the repository at this point in the history
  • Loading branch information
goodboy committed Jun 20, 2023
1 parent d42e831 commit 4dcc92d
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions piker/brokers/kraken/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
)


class OHLC(Struct):
class OHLC(Struct, frozen=True):
'''
Description of the flattened OHLC quote format.
Expand All @@ -76,6 +76,8 @@ class OHLC(Struct):
chan_id: int # internal kraken id
chan_name: str # eg. ohlc-1 (name-interval)
pair: str # fx pair

# unpacked from array
time: float # Begin time of interval, in seconds since epoch
etime: float # End time of interval, in seconds since epoch
open: float # Open price of interval
Expand All @@ -85,8 +87,6 @@ class OHLC(Struct):
vwap: float # Volume weighted average price within interval
volume: float # Accumulated volume **within interval**
count: int # Number of trades within interval
# (sampled) generated tick data
ticks: list[Any] = []


async def stream_messages(
Expand Down Expand Up @@ -150,14 +150,15 @@ async def process_data_feed_msgs(
pair
]:
if 'ohlc' in chan_name:
array: list = payload_array[0]
ohlc = OHLC(
chan_id,
chan_name,
pair,
*payload_array[0]
*map(float, array[:-1]),
count=array[-1],
)
ohlc.typecast()
yield 'ohlc', ohlc
yield 'ohlc', ohlc.copy()

elif 'spread' in chan_name:

Expand Down Expand Up @@ -430,7 +431,7 @@ async def subscribe(ws: NoBsWs):
feed_is_live.set()

# keep start of last interval for volume tracking
last_interval_start = ohlc_last.etime
last_interval_start: float = ohlc_last.etime

# start streaming
topic: str = mkt.bs_fqme
Expand All @@ -448,24 +449,23 @@ async def subscribe(ws: NoBsWs):

# new OHLC sample interval
if quote.etime > last_interval_start:
last_interval_start = quote.etime
tick_volume = volume
last_interval_start: float = quote.etime
tick_volume: float = volume

else:
# this is the tick volume *within the interval*
tick_volume = volume - ohlc_last.volume
tick_volume: float = volume - ohlc_last.volume

ohlc_last = quote
last = quote.close

quote = normalize(quote)
if tick_volume:
quote.ticks.append({
quote['ticks'] = [{
'type': 'trade',
'price': last,
'size': tick_volume,
})

quote = normalize(quote)
}]

case 'l1':
# passthrough quote msg
Expand Down

0 comments on commit 4dcc92d

Please sign in to comment.