过去两周一直在与此错误消息作斗争。我阅读了有关它的其他帖子并清理了我的代码,但我仍然得到它。
这是我的代码的第一部分:
Public PrevMinute As Integer
Declare PtrSafe Function GetTickCount Lib "kernel32.dll" () As Long
Sub Main()
PrevMinute = 0
Update
'Application.OnTime Now + TimeValue("00:01:00"), "Main"
End Sub
Sub Update()
Ticker
WaitTime (3)
BookGet
WaitTime (3)
TradesGet
WaitTime (3)
CheckBuySellSignal
WaitTime (3)
Workbooks("GDAX Order book and trades.xlsm").Save
CheckTime
End Sub
Sub CheckTime()
NowMinute = Minute(Now)
If NowMinute <> PrevMinute Then
PrevMinute = NowMinute
Call Update
Exit Sub
End If
WaitTime (5)
CheckTime
End Sub
Sub WaitTime(Finish As Long)
Dim NowTick As Long
Dim EndTick As Long
EndTick = GetTickCount + (Finish * 1000)
Do
NowTick = GetTickCount
DoEvents
Loop Until NowTick >= EndTick
End Sub
它在错误处理部分的行TradesGet失败的第二部分eh:我已添加此错误处理部分以添加从服务器返回的错误消息。基本上如果我检测到错误,我等待几秒钟再试一次。 AFTER 3尝试我停止尝试该循环(1分钟)复制/粘贴前一分钟的数据突出显示有问题的值,然后重新开始。
基本上我有Checktime()子,它在每分钟开始时调用Update()子。 Update()子接一个接一个地调用4个subs,当服务器开始抛出错误时,返回错误的那个是TradesGet sub(它也会抛出前一个子BookGet的错误但是这个可以正常。
Sub TradesGet()
On Error GoTo eh
Set MyRequest = CreateObject("WinHTTP.WinHTTPRequest.5.1")
MyRequest.Open "GET", "https://api.gdax.com/products/ETH-EUR/trades"
MyRequest.Send
Set ParsedTrades = JsonConverter.ParseJson(MyRequest.ResponseText)
'to try to fight intermitent bug
Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(19, "U") = MyRequest.ResponseText
WaitTime (2)
'check if did not receive error string from exchange, if so, start again (i should add logger here)
Dim pos As Integer
pos = InStr(MyRequest.ResponseText, "message")
If pos > 0 Then
Application.StatusBar = "Trades: received error message...retrying in 10 seconds"
WaitTime (10)
Application.StatusBar = "..retrying..."
WaitTime (1)
Application.StatusBar = ""
TradesGet
Exit Sub
End If
'to speed up execution when looping
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
For i = 1 To 100
'check if new tradeid > last tradeid, if so, copy
LastTradeId = Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(2, "B")
If ParsedTrades(i)("trade_id") > LastTradeId Then
Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(i + 1, 15) = ParsedTrades(i)("time")
Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(i + 1, 16) = ParsedTrades(i)("trade_id")
Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(i + 1, 17) = ParsedTrades(i)("price")
Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(i + 1, 18) = ParsedTrades(i)("size")
Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(i + 1, 19) = ParsedTrades(i)("side")
End If
Next i
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
'move new data at the end of previous data and sort again
Offset = Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(1, "M").Value + 1
OffsetNew = Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(1, "N").Value + 1
Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Range(Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(2, 15), Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(OffsetNew, "S")).Copy
Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Range(Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(Offset, "A"), Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells((Offset + OffsetNew - 2), "E")).PasteSpecial Paste:=xlPasteValues
'clear content of new data range to prepare for next cycle
Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Range(Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(2, 15), Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(OffsetNew, "S")).ClearContents
'sort data range to get latest tradeid number in cell B2
Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Columns("A:E").Sort key1:=Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Range("B:B"), order1:=xlDescending, Header:=xlYes
'copy trades rates to ticker table
Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Range("J3:K3").Copy
Offset = Workbooks("GDAX Order book and trades.xlsm").Sheets("ticker").Cells(1, "I").Value
Workbooks("GDAX Order book and trades.xlsm").Sheets("ticker").Cells(Offset, "S").PasteSpecial Paste:=xlPasteValues
'copy VSd to ticker table
Workbooks("GDAX Order book and trades.xlsm").Sheets("book").Range("M26").Copy
Offset = Workbooks("GDAX Order book and trades.xlsm").Sheets("ticker").Cells(1, "I").Value
Workbooks("GDAX Order book and trades.xlsm").Sheets("ticker").Cells(Offset, "AI").PasteSpecial Paste:=xlPasteValues
'take snapshot of book t0
Workbooks("GDAX Order book and trades.xlsm").Sheets("book").Range("N8:N9").Copy
Workbooks("GDAX Order book and trades.xlsm").Sheets("book").Range("Q8:Q9").PasteSpecial Paste:=xlPasteValues
Workbooks("GDAX Order book and trades.xlsm").Sheets("book").Range("N15").Copy
Workbooks("GDAX Order book and trades.xlsm").Sheets("book").Range("Q15").PasteSpecial Paste:=xlPasteValues
'delete unused trades data beyond 15min
DeleteLim = Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(1, "L").Value
Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Range(Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells((DeleteLim + 500), "A"), Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(2500, "E")).ClearContents
'move focus back to ticker sheet
Workbooks("GDAX Order book and trades.xlsm").Sheets("ticker").Activate
Done:
Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(13, "K") = 0
Exit Sub
'error handling code, restart sub
eh:
Application.StatusBar = "Trades: unexpected error ...retrying in 20 seconds"
WaitTime (20)
Application.StatusBar = "..retrying..."
WaitTime (1)
Application.StatusBar = ""
'increase error counter by 1
Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(13, "K") = Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(13, "K") + 1
'if error counter > 3, copy paste from previous minute, highlight in blue, and continue to CheckBuySellSignal, else run TradesGet again
If Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(13, "K") < 3 Then
TradesGet
'Exit Sub
Else
Offset = Workbooks("GDAX Order book and trades.xlsm").Sheets("ticker").Cells(1, "I").Value
Workbooks("GDAX Order book and trades.xlsm").Sheets("ticker").Cells(Offset, "S") = Workbooks("GDAX Order book and trades.xlsm").Sheets("ticker").Cells(Offset - 1, "S")
Workbooks("GDAX Order book and trades.xlsm").Sheets("ticker").Cells(Offset, "T") = Workbooks("GDAX Order book and trades.xlsm").Sheets("ticker").Cells(Offset - 1, "T")
Workbooks("GDAX Order book and trades.xlsm").Sheets("ticker").Cells(Offset, "S").Interior.ColorIndex = 28
Workbooks("GDAX Order book and trades.xlsm").Sheets("ticker").Cells(Offset, "T").Interior.ColorIndex = 28
'CheckBuySellSignal
'Exit Sub
End If
Exit Sub
End Sub