0

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:

enter image description here

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).

nop
  • 4,711
  • 6
  • 32
  • 93
  • Is this problem with the DateTime calculations or the Biance API? Did you try to hard-code the exact API parameters to confirm it returns what you expect it should return? – Euphoric Feb 12 '20 at 09:25
  • Can you look at what is in milliseconds in openTime and closeTime and include those values in your question? – Euphoric Feb 12 '20 at 09:27
  • If I hardcode the DateTime `new DateTime(...)`, it works fine. – nop Feb 12 '20 at 09:34
  • If the API works fine with your expected values, then either your calculation is wrong or there are values in the date times that are messing up your calculations. Things like milliseconds or time zones can do that. – Euphoric Feb 12 '20 at 09:36
  • Edited my question with a working code, but it works only with hardcoded values. It's 100% `.AddSeconds(1)` issue, I'm debugging it. Not sure but it might need `closeTime = closeTime.AddSeconds(1);` because it is using the unmodified object. – nop Feb 12 '20 at 09:37
  • `closeTime = closeTime.AddSeconds(1); ` couldn't solve it. – nop Feb 12 '20 at 11:04
  • You were right, it was a bad calculation. – nop Feb 12 '20 at 11:26

0 Answers0