veighna-global/vnpy_okx

OKEX获取历史数据不足的问题

Closed this issue · 3 comments

启动后获取历史数据不足,策略需要10天的历史数据,但是只能获取到1天的历史数据,如何解决。
数据不足
获取最近1天的数据

  1. 更换 okex_gateway.py 代码
 def query_history(self, req: HistoryRequest) -> List[BarData]:
        """
        查询历史数据

        K线数据每个粒度最多可获取最近1440条
        """
        buf: Dict[datetime, BarData] = {}
        end_time: str = ""
        path: str = "/api/v5/market/history-candles"   # 替换为  history-candles
  1. 添加循环计算
  def time_range(self, start, end, interval):

      s = datetime.strptime(str(start), '%Y-%m-%d %H:%M:%S+08:00')
      e = datetime.strptime(str(end), '%Y-%m-%d %H:%M:%S+08:00')

      step: int = 0
      if interval == Interval.DAILY:
          step = (e-s).days / 100
      elif interval == Interval.WEEKLY:
          step = ((e-s).days / 7) / 100
      elif interval == Interval.HOUR:
          step = (e-s).days * 24 / 100
      elif interval == Interval.MINUTE:
          step = (e-s).days * 24 * 60 / 100
      elif interval == Interval.TICK:
          step = (e-s).days * 24 * 60 * 60 / 100

      return int(step)

要是整段修改的话可以尝试直接覆盖粘贴这一段:

    def time_range(self, start, end, interval):

        s = datetime.strptime(str(start), '%Y-%m-%d %H:%M:%S+08:00')
        e = datetime.strptime(str(end), '%Y-%m-%d %H:%M:%S+08:00')

        step: int = 0
        if interval == Interval.DAILY:
            step = (e-s).days / 100
        elif interval == Interval.WEEKLY:
            step = ((e-s).days / 7) / 100
        elif interval == Interval.HOUR:
            step = (e-s).days * 24 / 100
        elif interval == Interval.MINUTE:
            step = (e-s).days * 24 * 60 / 100
        elif interval == Interval.TICK:
            step = (e-s).days * 24 * 60 * 60 / 100

        return int(step)

    def query_history(self, req: HistoryRequest) -> List[BarData]:
        """
        查询历史数据

        K线数据每个粒度最多可获取最近1440条
        """
        buf: Dict[datetime, BarData] = {}
        end_time: str = ""
        path: str = "/api/v5/market/history-candles"

        s_time = req.start # start_time
        e_time = req.end # end_time

        steps = self.time_range(s_time, e_time, req.interval)

        count = 0
        for i in range(steps):
            # 创建查询参数
            params: dict = {
                "instId": req.symbol,
                "bar": INTERVAL_VT2OKEX[req.interval]
            }

            if end_time:
                params["after"] = end_time

            count += 1
            if count > 20:
                time.sleep(3)



谢谢!感谢分享!!

u can send a PR for this, i will merge it.