def _get_future_trading_minutes(self, trading_date):
     trading_minutes = set()
     universe = self._get_universe()
     for order_book_id in universe:
         if get_account_type(order_book_id) == DEFAULT_ACCOUNT_TYPE.STOCK.name:
             continue
         trading_minutes.update(self._env.data_proxy.get_trading_minutes_for(order_book_id, trading_date))
     return set([convert_int_to_datetime(minute) for minute in trading_minutes])
Beispiel #2
0
 def datetime(self):
     """
     [datetime.datetime] 当前快照数据的时间戳
     """
     if self._dt is not None:
         return self._dt
     if not self.isnan:
         return convert_int_to_datetime(self._data['datetime'])
     return datetime.datetime.min
Beispiel #3
0
 def _get_future_trading_minutes(self, trading_date):
     trading_minutes = set()
     universe = self._get_universe()
     for order_book_id in universe:
         if self._env.get_account_type(
                 order_book_id) == DEFAULT_ACCOUNT_TYPE.STOCK:
             continue
         trading_minutes.update(
             self._env.data_proxy.get_trading_minutes_for(
                 order_book_id, trading_date))
     return set(
         [convert_int_to_datetime(minute) for minute in trading_minutes])
Beispiel #4
0
 def history(self, order_book_id, bar_count, frequency, field, dt):
     data = self.history_bars(order_book_id,
                              bar_count,
                              frequency, ['datetime', field],
                              dt,
                              skip_suspended=False,
                              adjust_orig=dt)
     if data is None:
         return None
     return pd.Series(
         data[field],
         index=[convert_int_to_datetime(t) for t in data['datetime']])
Beispiel #5
0
 def datetime(self):
     """
     [datetime.datetime] 当前快照数据的时间戳
     """
     if self._dt is not None:
         return self._dt
     if not self.isnan:
         dt = self._data['datetime']
         if dt > 10000000000000000:  # ms
             return convert_ms_int_to_datetime(dt)
         else:
             return convert_int_to_datetime(dt)
     return datetime.datetime.min
Beispiel #6
0
 def datetime(self):
     """
     [datetime.datetime] 当前快照数据的时间戳
     """
     try:
         dt = self._tick_dict['datetime']
     except (KeyError, ValueError):
         return datetime.datetime.min
     else:
         if not isinstance(dt, datetime.datetime):
             if dt > 10000000000000000:  # ms
                 return convert_ms_int_to_datetime(dt)
             else:
                 return convert_int_to_datetime(dt)
         return dt
Beispiel #7
0
 def datetime(self):
     """
     [datetime.datetime] 当前快照数据的时间戳
     """
     try:
         dt = self._tick_dict['datetime']
     except (KeyError, ValueError):
         return datetime.datetime.min
     else:
         if not isinstance(dt, datetime.datetime):
             if dt > 10000000000000000:  # ms
                 return convert_ms_int_to_datetime(dt)
             else:
                 return convert_int_to_datetime(dt)
         return dt
 def get_future_trading_points(env, trading_date, frequency):
     if frequency == "1m":
         trading_minutes = set()
         universe = env.get_universe()
         for order_book_id in universe:
             if get_account_type(
                     order_book_id) == DEFAULT_ACCOUNT_TYPE.STOCK:
                 continue
             trading_minutes.update(
                 env.data_proxy.get_trading_minutes_for(
                     order_book_id, trading_date))
         return set([
             convert_int_to_datetime(minute) for minute in trading_minutes
         ])
     # TODO future hours
     return set()
    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 #10
0
 def resample_week_bars(self, bars, bar_count, fields):
     df_bars = pd.DataFrame(bars)
     df_bars['datetime'] = df_bars.apply(lambda x: convert_int_to_datetime(x['datetime']), axis=1)
     df_bars = df_bars.set_index('datetime')
     nead_fields = fields
     if isinstance(nead_fields, str):
         nead_fields = [nead_fields]
     hows = {field: BAR_RESAMPLE_FIELD_METHODS[field] for field in nead_fields if field in BAR_RESAMPLE_FIELD_METHODS}
     df_bars = df_bars.resample('W-Fri').agg(hows)
     df_bars.index = df_bars.index.map(self._update_weekly_trading_date_index)
     df_bars = df_bars[~df_bars.index.duplicated(keep='first')]
     df_bars.sort_index(inplace=True)
     df_bars = df_bars[-bar_count:]
     df_bars = df_bars.reset_index()
     df_bars['datetime'] = df_bars.apply(lambda x: np.uint64(convert_date_to_int(x['datetime'].date())), axis=1)
     df_bars = df_bars.set_index('datetime')
     bars = df_bars.to_records()
     return bars
Beispiel #11
0
 def datetime(self):
     if self._dt is not None:
         return self._dt
     enviroment = Environment.get_instance()
     dict = enviroment.config.mod.tushare
     #print(dict.__dict__.get('enabled'))
     if dict.__dict__.get('enabled') == True:
         print('enviroment.config.mod.tushare[\'enabled\'] == True')
         return self._data['date']
     dict2 = enviroment.config.mod.mysql
     # print(dict2.__dict__.get('enabled'))
     if dict2.__dict__.get('enabled') == True:
         print('enviroment.config.mod.mysql[\'enabled\'] == True')
         print(self._data)
         return self._data['date']
     #datetime1 = convert_int_to_datetime(self._data['date'])
     #datetime1 = convert_int_to_datetime(self._data['datetime'])
     return convert_int_to_datetime(self._data['datetime'])
Beispiel #12
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
 def last_dt(self):
     if len(self):
         return convert_int_to_datetime(self._data[-1]["datetime"])
     else:
         return None
Beispiel #14
0
 def datetime(self):
     if self._dt is not None:
         return self._dt
     return convert_int_to_datetime(self._data['datetime'])
Beispiel #15
0
 def history(self, order_book_id, bar_count, frequency, field, dt):
     data = self.history_bars(order_book_id, bar_count, frequency,
                              ['datetime', field], dt, skip_suspended=False, adjust_orig=dt)
     if data is None:
         return None
     return pd.Series(data[field], index=[convert_int_to_datetime(t) for t in data['datetime']])
Beispiel #16
0
 def datetime(self):
     if self._dt is not None:
         return self._dt
     return convert_int_to_datetime(self._data['datetime'])