def test_resample(): df = to_dataframe(data) assert isinstance(resample(df, "2d"), DataFrame) assert list(resample(df, "2d").index.values[-2:]) == [ numpy.datetime64("2019-05-05T00:00:00.000000000"), numpy.datetime64("2019-05-07T00:00:00.000000000"), ]
def test_resample_calendar(): df = to_dataframe(data) assert isinstance(resample(df, "W-Mon"), DataFrame) assert list(resample(df, "W-Mon").index.values[-2:]) == [ numpy.datetime64("2019-05-06T00:00:00.000000000"), numpy.datetime64("2019-05-13T00:00:00.000000000"), ]
def resample_ohlc(price, vol, scale, mode='ohlcv'): timescales = { '5min': 5, '15min': 15, '30min': 30, '1h': 60, '4h': 240, '12h': 720, '1d': 1440, '3d': 4320, '1w': 10080 } t = timescales.get(scale) if mode == 'ohlcv': new_price = resample(price, scale) new_vol = list(new_price.volume) if mode == 'close': new_price = [] new_vol = [] for i in range(int(len(price) / t)): pos = i * t p = price[pos + t - 1] new_price.append(p) v = sum(vol[pos:pos + t]) new_vol.append(v) if mode == 'mean': new_price = [] new_vol = [] for i in range(int(len(price) / t)): pos = i * t p = statistics.mean(price[pos:pos + t]) new_price.append(p) v = sum(vol[pos:pos + t]) new_vol.append(v) if mode == 'median': new_price = [] new_vol = [] for i in range(int(len(price) / t)): pos = i * t p = statistics.median(price[pos:pos + t]) new_price.append(p) v = sum(vol[pos:pos + t]) new_vol.append(v) return new_price, new_vol
def resample(ohlc: DataFrame, interval: str) -> DataFrame: """resample by <interval>""" return resample(ohlc, interval)