def get_bars(self,
                 order_book_id,
                 dt,
                 bar_count=1,
                 frequency='1d',
                 fields=None,
                 skip_suspended=True,
                 include_now=False,
                 adjust_type='pre',
                 adjust_orig=None,
                 convert_to_dataframe=False,
                 time_format='datetime'):

        order_book_id = to_order_book_id(order_book_id)
        dt = to_date_object(dt)  #TODO if we want minute bar?

        if fields is None:
            fields = [
                'datetime', 'open', 'high', 'low', 'close', 'volume',
                'total_turnover'
            ]

        bars = super(DataSource,
                     self).history_bars(order_book_id=order_book_id,
                                        bar_count=bar_count,
                                        frequency=frequency,
                                        field=fields,
                                        dt=dt,
                                        skip_suspended=skip_suspended,
                                        include_now=include_now,
                                        adjust_type=adjust_type,
                                        adjust_orig=adjust_orig)
        if convert_to_dataframe:
            df = pd.DataFrame.from_dict(bars)
            if 'datetime' in df.columns:
                if time_format == 'datetime':
                    df['datetime'] = df['datetime'].map(
                        lambda x: convert_int_to_datetime(x))
                if time_format == 'int' and frequency == '1d':
                    df['datetime'] = df['datetime'].map(lambda x: x / 1000000)
                df.set_index('datetime', inplace=True)
                # df.index.name = ''
            # df.index[0] is like: Timestamp('2005-01-04 00:00:00'),type is pandas._libs.tslib.Timestamp
            return df

        return bars  # type(bars): numpy.ndarray
Beispiel #2
0
    def get_bars_all(self,
                     order_book_id,
                     dt=None,
                     frequency='1d',
                     fields=None,
                     skip_suspended=True,
                     include_now=False,
                     adjust_type='pre',
                     adjust_orig=None,
                     convert_to_dataframe=False):
        order_book_id = to_order_book_id(order_book_id)
        if dt is None:
            dt = datetime.datetime.now()
        dt = to_date_object(dt)

        if fields is None:
            fields = [
                'datetime', 'open', 'high', 'low', 'close', 'volume',
                'total_turnover'
            ]

        bars = super(DataSource,
                     self).get_bars_all(order_book_id=order_book_id,
                                        frequency=frequency,
                                        field=fields,
                                        dt=dt,
                                        skip_suspended=skip_suspended,
                                        include_now=include_now,
                                        adjust_type=adjust_type,
                                        adjust_orig=adjust_orig)
        if convert_to_dataframe:
            df = pd.DataFrame.from_dict(bars)
            if 'datetime' in df.columns:
                df['datetime'] = df['datetime'].map(
                    lambda x: convert_int_to_datetime(x))
                df.set_index('datetime', inplace=True)
                df.index.name = ''
            return df

        return bars
Beispiel #3
0
 def history_bars(self,
                  order_book_id,
                  bar_count,
                  frequency,
                  field,
                  dt,
                  skip_suspended=True,
                  include_now=False,
                  adjust_type='pre',
                  adjust_orig=None):
     order_book_id = to_order_book_id(order_book_id)
     dt = to_date_object(dt)
     bars = super(DataSource,
                  self).history_bars(order_book_id=order_book_id,
                                     bar_count=bar_count,
                                     frequency=frequency,
                                     field=field,
                                     dt=dt,
                                     skip_suspended=skip_suspended,
                                     include_now=include_now,
                                     adjust_type=adjust_type,
                                     adjust_orig=adjust_orig)
     return bars
 def get_bar(self, order_book_id, dt, frequency='1d'):
     order_book_id = to_order_book_id(order_book_id)
     dt = to_date_object(dt)
     return super(DataSource, self).get_bar(order_book_id=order_book_id,
                                            dt=dt,
                                            frequency=frequency)