adshao / go-binance

A Go SDK for Binance API
MIT License
1.49k stars 661 forks source link

ERROR: -1021, Timestamp for this request is outside of the recvWindow. #344

Closed ljubomirb closed 2 years ago

ljubomirb commented 2 years ago

Hi, I received this error, but it is not happening every time, and it probably happens when I give order. I only guess, since error did not show up until today, so it's hard for me to find it. But, as for google tells me, it is probably because computer time and binance time are different as explained here The solution binance tells us is that we must sync computer time with binance server, whereas I would like to avoid this. Thus, I've seen that there is a parameter recvWindow that can be changed when giving order, as shown in documentation here. I also see you have it in code on a few places, so I guess it is all already here, just I don't know how to use it, or something small is missing. My questions are:

Thank you for your time!

ljubomirb commented 2 years ago

Ok, so I think I've found a way to push WithRecvWindow option, hope I'm doing it right. So, order might now look something like this:

order, err := client.NewCreateOrderService().Symbol(symbolpair).
        Side(side).Type(binance.OrderTypeMarket).NewClientOrderID(customID).
        Quantity(quantity)).                  //Price(price))
        Do(context.Background(), binance.WithRecvWindow(10000)) //10 secs, default is 5
return order, err
adshao commented 2 years ago

Hi @ljubomirb , You are right, RecvWindow should be passed in Do(ctx, options...). Currently there is no global recvWindow since not every API needs it. It is recommended that you should keep your host time synced with binance server.