VBA-等待响应请求/ Web API

时间:2019-06-24 17:55:06

标签: excel vba

我正在尝试将订单簿从bitmex转到excel。根据Internet上的示例,我设法只编写了部分代码。它只能工作一次,我仍然需要进行循环,以便它可以自动工作。这样,下载数据后便会发出下一个请求。

我使用了从github下载并添加了参考文献的json转换器库:

  • Microsoft xml6.0和Microsoft Winhttp Verion 5.1

Public Sub bitmexAPI()
    Dim http As Object, JSON As Object, i As Integer
    Set http = CreateObject("MSXML2.XMLHTTP")

    http.Open "GET", "https://www.bitmex.com/api/v1/orderBook/L2?symbol=XBT&depth=5", False
    http.Send

    Set JSON = ParseJson(http.ResponseText)
    i = 2

    For Each Item In JSON
        Sheets(2).Cells(i, 1).Value = Item("symbol")
        Sheets(2).Cells(i, 2).Value = Item("id")        
        Sheets(2).Cells(i, 3).Value = Item("side")        
        Sheets(2).Cells(i, 4).Value = Item("size")        
        Sheets(2).Cells(i, 5).Value = Item("price")                
        i = i + 1        
    Next

    Application.OnTime Now + TimeValue("00:00:02"), "bitmexAPI"                   
End Sub

我添加了2秒的延迟,但仅刷新一次数据。 如何进行循环以使宏等待上一个查询的响应?

1 个答案:

答案 0 :(得分:1)

尝试一下:

// { id: 1, startDate: "2019-01-22 20:58:47", finishDate: "2019-01-22 23:23:47" }
// { id: 2, startDate: "2019-01-22 11:58:47", finishDate: "2019-01-22 23:12:47" }
// { id: 3, startDate: "2019-01-22 13:58:47", finishDate: "2019-01-22 23:23:47" }
db.collection.aggregate([
  { $addFields: {
      period: { $subtract: [ { $toDate: "$finishDate" }, { $toDate: "$startDate" } ] }
  } },
  { $sort: { period: -1 } },
  { $limit: 2 }
])
// { id: 2, period: NumberLong(40440000), startDate: "2019-01-22 11:58:47", finishDate: "2019-01-22 23:12:47" }
// { id: 3, period: NumberLong(33900000), startDate: "2019-01-22 13:58:47", finishDate: "2019-01-22 23:23:47" }