I'm using Binance.Net as a Binance API wrapper and recently I ran into a problem with DateTime.AddSeconds
.
The following code is supposed to find the candle before the last order.
// OpenTime and CloseTime are both DateTime
var openTime = lastOrder.OpenTime; // 10:50:00
var closeTime = lastOrder.CloseTime; // 10:54:59
// It should return 10:55:00 - 10:50:00 = 5 minutes
// The reason I'm doing this is because I don't know the exact interval my bot is working with.
// What I know is the OpenTime (begin) and CloseTime (end).
var difference = closeTime.AddSeconds(1) - openTime; // closeTime.AddSeconds(1) doesn't work
var dateTime = openTime - difference; // 10:50:00 - 00:05:00.9990000 = 10:39:99
// 10:39:99 is an invalid startTime/endTime
var kline = _client.GetKlines(symbol, interval, startTime: dateTime, endTime: dateTime, limit: 1).Data;
The problem:
The problem is that .AddSeconds(1)
doesn't actually add that second and as a result _client.GetKlines
fails.
I don't know if there is a better way to get the previous candle in Binance's API but if there is, I would accept it as a solution.
Problem visual representation:
Edit:
Fixed it! When I checked the DateTime properties, I realized that I missed the miliseconds in the CloseTime property. So in the end, it was a wrong calculation. Instead of .AddSeconds(1)
, I made it .AddMiliseconds(1)
.