Example #1
0
    def history_bars(self, instrument, bar_count, frequency, fields, dt,
                     skip_suspended=True, include_now=False,
                     adjust_type='pre', adjust_orig=None):
        if frequency != '1d':
            raise NotImplementedError

        if skip_suspended and instrument.type == 'CS':
            bars = self._filtered_day_bars(instrument)
        else:
            bars = self._all_day_bars_of(instrument)

        if bars is None or not self._are_fields_valid(fields, bars.dtype.names):
            return None

        dt = np.uint64(convert_date_to_int(dt))
        i = bars['datetime'].searchsorted(dt, side='right')
        left = i - bar_count if i >= bar_count else 0
        bars = bars[left:i]
        if adjust_type == 'none' or instrument.type in {'Future', 'INDX'}:
            # 期货及指数无需复权
            return bars if fields is None else bars[fields]

        if isinstance(fields, str) and fields not in FIELDS_REQUIRE_ADJUSTMENT:
            return bars if fields is None else bars[fields]

        return adjust_bars(bars, self.get_ex_cum_factor(instrument.order_book_id),
                           fields, adjust_type, adjust_orig)
Example #2
0
    def history_bars(self, instrument, bar_count, frequency, fields, dt,
                     skip_suspended=True, include_now=False,
                     adjust_type='pre', adjust_orig=None):
        if frequency != '1d':
            raise NotImplementedError

        if skip_suspended and instrument.type == 'CS':
            bars = self._filtered_day_bars(instrument)
        else:
            bars = self._all_day_bars_of(instrument)

        if bars is None or not self._are_fields_valid(fields, bars.dtype.names):
            return None

        dt = np.uint64(convert_date_to_int(dt))
        i = bars['datetime'].searchsorted(dt, side='right')
        left = i - bar_count if i >= bar_count else 0
        bars = bars[left:i]
        if adjust_type == 'none' or instrument.type in {'Future', 'INDX'}:
            # 期货及指数无需复权
            return bars if fields is None else bars[fields]

        if isinstance(fields, str) and fields not in FIELDS_REQUIRE_ADJUSTMENT:
            return bars if fields is None else bars[fields]

        return adjust_bars(bars, self.get_ex_cum_factor(instrument.order_book_id),
                           fields, adjust_type, adjust_orig)
Example #3
0
 def history_bars(self,
                  instrument,
                  bar_count,
                  frequency,
                  fields,
                  dt,
                  skip_suspended=True,
                  include_now=False,
                  adjust_type='pre',
                  adjust_orig=None):
     if self.is_base_frequency(instrument, frequency):
         bars = self.raw_history_bars(instrument,
                                      frequency,
                                      end_dt=dt,
                                      length=bar_count)
     else:
         num = int(frequency[:-1])
         freq = frequency[-1]
         if freq == "m":
             lower_bar_count = (bar_count + 1) * num
             bars = self.raw_history_bars(instrument,
                                          "1" + freq,
                                          end_dt=dt,
                                          length=lower_bar_count)
             if bars is None:
                 return super(OddFrequencyDataSource, self).history_bars(
                     instrument,
                     bar_count,
                     frequency,
                     fields,
                     dt,
                     skip_suspended=skip_suspended,
                     include_now=include_now,
                     adjust_type=adjust_type,
                     adjust_orig=adjust_orig)
             else:
                 if bars.size:
                     bars = self._resample_bars(bars, frequency)
                     dti = convert_dt_to_int(dt)
                     if bars["datetime"][-1] != dti and not include_now:
                         bars = bars[:-1]
                         bars = bars[-bar_count:]
                     else:
                         bars = bars[-bar_count:]
                         # TODO 跳过停牌
         else:
             return super(OddFrequencyDataSource,
                          self).history_bars(instrument,
                                             bar_count,
                                             frequency,
                                             fields,
                                             dt,
                                             skip_suspended=skip_suspended,
                                             include_now=include_now,
                                             adjust_type=adjust_type,
                                             adjust_orig=adjust_orig)
             # if fields is not None:
             #     if not isinstance(fields, six.string_types):
             #         fields = [field for field in fields if field in bar_data]
     if adjust_type == "none" or instrument.type in {"Future", "INDX"}:
         return bars if fields is None else bars[fields]
     if isinstance(fields, str) and fields not in FIELDS_REQUIRE_ADJUSTMENT:
         return bars if fields is None else bars[fields]
     return adjust_bars(bars,
                        self.get_ex_cum_factor(instrument.order_book_id),
                        fields, adjust_type, adjust_orig)