예제 #1
0
 def raw_history_bars(self, instrument, frequency, start_dt=None, end_dt=None, length=None):
     symbol = instrument_to_tushare(instrument)
     if frequency in ["1d"]:
         if start_dt and end_dt:
             s_date_int = convert_date_to_int(start_dt.date())
             e_date_int = convert_date_to_int(end_dt.date())
         elif start_dt and length:
             dates = self._dates_index(instrument)
             s_date_int = convert_date_to_int(start_dt.date())
             s_pos = safe_searchsorted(dates, s_date_int)
             s_date_int = int(dates[s_pos])
             e_date_int = int(dates[min(s_pos + length, len(dates)) - 1])
         elif end_dt and length:
             dates = self._dates_index(instrument)
             e_date_int = convert_date_to_int(end_dt.date())
             e_pos = safe_searchsorted(dates, e_date_int, side="right")
             s_date_int = int(dates[max(e_pos - length, 0)])
             e_date_int = int(dates[e_pos - 1])
         else:
             raise RuntimeError("At least two of [start_dt,end_dt,length] should be given.")
         data, msg = self._api.daily(symbol, freq=frequency, adjust_mode=None,
                                     start_date=s_date_int // 1000000,
                                     end_date=e_date_int // 1000000)
         if isinstance(data, pd.DataFrame) and data.size:
             data = data[data["volume"] > 0]  # TODO sikp_suspended?
             return QuantOsConverter.df2np(data)
         else:
             if msg:
                 system_log.warning(msg)
             return QuantOsConverter.empty()
     else:
         return MiniteBarDataSourceMixin.raw_history_bars(
             self, instrument, frequency, start_dt=start_dt, end_dt=end_dt, length=length
         )
 def get_bars(self,
              instrument,
              frequency,
              trade_date=None,
              start_time=None,
              end_time=None):
     symbol = instrument_to_tushare(instrument)
     kwargs = {}
     if start_time is not None:
         kwargs["start_time"] = start_time
     elif end_time is not None:
         kwargs["end_time"] = end_time
     retry = 0
     while retry < self.MAX_RETRY:
         retry += 1
         try:
             freq = frequency[:-1] + frequency[-1].upper()
             params = dict(symbol=symbol, freq=freq, trade_date=0, **kwargs)
             bars, msg = self._api.bar(**params)
             code = msg.split(",")[0]
             if not isinstance(bars, pd.DataFrame) or code != "0":
                 raise QuantOsQueryError(msg)
             else:
                 break
         except QuantOsQueryError as e:
             if retry <= self.MAX_RETRY:
                 user_system_log.warning(
                     "[japs] Exception occurs when call api.bar with param [%s]: %s"
                     % (params, e))
                 time.sleep(retry)
             else:
                 raise e
     return QuantOsConverter.df2np(bars)
예제 #3
0
 async def _get_bars_in_day(self, instrument=None, frequency=None, trade_date=None, start_time=0, end_time=150000):
     # TODO retry when net error occurs
     symbol = instrument_to_tushare(instrument)
     trade_date = convert_date_to_int(trade_date) // 1000000
     start_time = max(start_time, 80000)
     end_time = min(end_time, 160000)
     return self._api.bar(symbol=symbol, freq=frequency[:-1] + frequency[-1].upper(),
                          trade_date=trade_date, start_time=start_time, end_time=end_time)
예제 #4
0
 def get_bars(self,
              instrument,
              frequency,
              trade_date=None,
              start_time=None,
              end_time=None):
     symbol = instrument_to_tushare(instrument)
     kwargs = {}
     if start_time is not None:
         kwargs["start_time"] = start_time
     elif end_time is not None:
         kwargs["end_time"] = end_time
     bars, msg = self._api.bar(symbol=symbol,
                               freq=frequency[:-1] + frequency[-1].upper(),
                               trade_date=0,
                               **kwargs)
     if not isinstance(bars, pd.DataFrame):
         raise RuntimeError(msg)
     return QuantOsConverter.df2np(bars)