def test_5m_dataframe_window() -> None: df_5min = random_kline_data_with_start_end(utc_datetime(2016, 1, 1, 0), utc_datetime( 2016, 1, 2, 23, 55), freq='5min') df_5min_between = kline_count_window(df_5min, utc_datetime(2016, 1, 2), 30) assert len(df_5min_between) == 30 assert df_5min_between.index[-1] == utc_datetime(2016, 1, 2) df_5min_not_full_start = kline_count_window(df_5min, utc_datetime(2016, 1, 1, 1), 30) assert len(df_5min_not_full_start) == 13 assert df_5min_not_full_start.index[-1] == utc_datetime(2016, 1, 1, 1) assert df_5min_not_full_start.index[0] == utc_datetime(2016, 1, 1) df_5min_not_full_count_end = kline_count_window( df_5min, utc_datetime(2016, 1, 3, 0, 20), 10) assert len(df_5min_not_full_count_end) == 5 assert df_5min_not_full_count_end.index[-1] == utc_datetime( 2016, 1, 2, 23, 55) df_5min_with_remain_in_end_time = kline_count_window( df_5min, utc_datetime(2016, 1, 2, 12, 13, 3), 30) assert len(df_5min_with_remain_in_end_time) == 30 assert df_5min_with_remain_in_end_time.index[-1] == utc_datetime( 2016, 1, 2, 12, 10)
def test_1m_dataframe_window() -> None: df = random_kline_data_with_start_end(utc_datetime(2016, 1, 1, 0, 1), utc_datetime(2016, 1, 3)) df_between = kline_count_window(df, utc_datetime(2016, 1, 2), 100) assert len(df_between) == 100 assert df_between.index[-1] == utc_datetime(2016, 1, 2) df_not_full_count_start = kline_count_window(df, utc_datetime(2016, 1, 1, 1), 100) assert len(df_not_full_count_start) == 60 assert df_not_full_count_start.index[-1] == utc_datetime(2016, 1, 1, 1) assert df_not_full_count_start.index[0] == utc_datetime(2016, 1, 1, 0, 1) df_not_full_count_end = kline_count_window(df, utc_datetime(2016, 1, 3, 0, 20), 100) assert len(df_not_full_count_end) == 80 assert df_not_full_count_end.index[-1] == utc_datetime(2016, 1, 3) df_way_out_of_start = kline_count_window(df, utc_datetime(2015, 1, 1), 100) assert len(df_way_out_of_start) == 0 df_way_out_of_end = kline_count_window(df, utc_datetime(2017, 1, 1), 100) assert len(df_way_out_of_end) == 0 df_with_second_in_endtime = kline_count_window( df, utc_datetime(2016, 1, 2, 12, 13, 3), 200) assert len(df_with_second_in_endtime) == 200 assert df_with_second_in_endtime.index[-1] == utc_datetime( 2016, 1, 2, 12, 13)
def test_other_dataframe_window() -> None: df_15min = random_kline_data_with_start_end(utc_datetime(2016, 1, 1, 0), utc_datetime(2016, 1, 3), freq='15min') df_15min_between = kline_count_window(df_15min, utc_datetime(2016, 1, 1, 23, 45), 10) assert len(df_15min_between) == 10 assert df_15min_between.index[-1] == utc_datetime(2016, 1, 1, 23, 45) df_30min = random_kline_data_with_start_end(utc_datetime(2016, 1, 1, 0), utc_datetime(2016, 1, 3), freq='30min') df_30min_between = kline_count_window(df_30min, utc_datetime(2016, 1, 1, 23, 30), 10) assert len(df_30min_between) == 10 assert df_30min_between.index[-1] == utc_datetime(2016, 1, 1, 23, 30) df_60min = random_kline_data_with_start_end(utc_datetime(2016, 1, 1, 0), utc_datetime(2016, 1, 3), freq='60min') df_60min_between = kline_count_window(df_60min, utc_datetime(2016, 1, 1, 23), 10) assert len(df_60min_between) == 10 assert df_60min_between.index[-1] == utc_datetime(2016, 1, 1, 23)
def get_kline(self, symbol: str, date_time: datetime.datetime, count: int) -> pandas.DataFrame: assert is_aware_datetime(date_time) kline_frame = self._kline_store.get(symbol) target_klines = kline_count_window(kline_frame, date_time, count) return target_klines
def history_indicator(self, store_key: str, count: int) -> pandas.DataFrame: frame = self._cache_indicator[store_key] return kline_count_window(frame, self.context.now, count)
def history_kline(self, freq: str, instrument: INSTRUMENT, count: int) -> pandas.DataFrame: frame = self._cache_kline[instrument][freq] return kline_count_window(frame, self.context.now, count)