Exemple #1
0
    def resample(self, sampling_frequency: str) -> Optional['TradeBars']:
        '''
        Downsample the trade bars data into a new bar frequency
        
        Args:
            sampling_frequency: See sampling frequency in pandas
        '''
        if sampling_frequency is None:
            return self

        df = self.df()
        # Rename timestamps to timestamp
        df.index.name = 'timestamp'

        df = resample_trade_bars(df, sampling_frequency)
        o = df.o if 'o' in df.columns else None
        h = df.h if 'h' in df.columns else None
        _l = df.l if 'l' in df.columns else None
        v = df.v if 'v' in df.columns else None
        vwap = df.vwap if 'vwap' in df.columns else None

        trade_bar = TradeBars(df.timestamp, df.c, o, h, _l, v, vwap)

        trade_bar._set_valid_rows()

        return trade_bar
Exemple #2
0
 def _resample(self, sampling_frequency: Optional[str]) -> None:
     if sampling_frequency is None: return None
     for data in self.data_list:
         if isinstance(data, TimeSeries) or isinstance(data, TradeSet):
             data.timestamps, data.values = resample_ts(data.timestamps, data.values, sampling_frequency)
         elif isinstance(data, TradeBarSeries):
             df_dict = {}
             cols = ['timestamps', 'o', 'h', 'l', 'c', 'v', 'vwap']
             for col in cols:
                 val = getattr(data, col)
                 if val is not None:
                     df_dict[col] = val
             df = pd.DataFrame(df_dict)
             df = df.set_index('timestamps')
             df = resample_trade_bars(df, sampling_frequency)
             for col in cols:
                 if col in df:
                     setattr(data, col, df[col].values)
         else:
             raise Exception(f'unknown type: {data}')