Simplify numpy mapping logic

kucoin_backend
jaredgoldman 2023-04-15 21:05:04 -04:00
parent 59249a8c1e
commit 8403d8a482
1 changed files with 45 additions and 26 deletions

View File

@ -327,6 +327,29 @@ class Client:
Get OHLC data and convert to numpy array for perffff: Get OHLC data and convert to numpy array for perffff:
https://docs.kucoin.com/#get-klines https://docs.kucoin.com/#get-klines
Kucoin bar data format:
[
"1545904980", //Start time of the candle cycle 0
"0.058", //opening price 1
"0.049", //closing price 2
"0.058", //highest price 3
"0.049", //lowest price 4
"0.018", //Transaction volume 5
"0.000945" //Transaction amount 6
],
piker ohlc numpy array format:
[
('index', int),
('time', int),
('open', float),
('high', float),
('low', float),
('close', float),
('volume', float),
('bar_wap', float), # will be zeroed by sampler if not filled
]
''' '''
# Generate generic end and start time if values not passed # Generate generic end and start time if values not passed
# Currently gives us 12hrs of data # Currently gives us 12hrs of data
@ -360,34 +383,30 @@ class Client:
bars: list[list[str]] = data bars: list[list[str]] = data
break break
# Map to OHLC values to dict then to np array
new_bars = [] new_bars = []
for i, bar in enumerate(bars[::-1]): reversed_bars = bars[::-1]
data = {
'index': i,
'time': bar[0],
'open': bar[1],
'close': bar[2],
'high': bar[3],
'low': bar[4],
'volume': bar[5],
'amount': bar[6],
'bar_wap': 0.0,
}
row = [] for i, bar in enumerate(reversed_bars):
for _, (field_name, field_type) in enumerate(_ohlc_dtype): new_bars.append(
value = data[field_name] (
# index
match field_name: i,
case 'index': # time
row.append(int(value)) int(bar[0]),
case 'time': # open
row.append(value) float(bar[1]),
case _: # high
row.append(float(value)) float(bar[3]),
# low
new_bars.append(tuple(row)) float(bar[4]),
# close
float(bar[2]),
# volume
float(bar[5]),
# bar_wap
0.0,
)
)
array = np.array(new_bars, dtype=_ohlc_dtype) if as_np else bars array = np.array(new_bars, dtype=_ohlc_dtype) if as_np else bars
return array return array