vnpy_okx icon indicating copy to clipboard operation
vnpy_okx copied to clipboard

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

Open linhertz opened this issue 2 years ago • 2 comments

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

linhertz avatar Jul 20 '22 16:07 linhertz

  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)



wwdd23 avatar Aug 22 '22 00:08 wwdd23

谢谢!感谢分享!!

linhertz avatar Aug 24 '22 08:08 linhertz

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

vn-crypto avatar Jan 27 '23 03:01 vn-crypto