Login
Finding Previous Candles on Ethereum Using Binance Net: Problem with DateTime.AddSeconds
As an active cryptocurrency trader, you are probably not new to working with data streams from various exchanges. However, when it comes to searching for previous candles on Ethereum using the Binance.Net API wrapper, there is a common problem that can make your code unusable. In this article, we will look at why DateTime.AddSeconds
does not work as intended and propose a solution to overcome this limitation.
Problem: DateTime.AddSeconds
When using the DateTime.AddSeconds
method, it adds seconds to the specified date/time value. This can be a problem in some scenarios, especially when dealing with candlestick data from exchanges that store their prices in UTC (Coordinated Universal Time).
In Ethereum, candles are usually stored as UTC timestamps, which means that any adjustments to the timestamp using DateTime.AddSeconds
will not result in the correct display of the previous candle.
Solution: Use DateTime.FromTicks
or DateTime.ParseExact
To find the previous candle, you need to convert the timestamp to a DateTime object. However, since Binance Net uses ticks to return its APIs (instead of seconds), you’ll have to use one of two approaches:
DateTime.FromTicks
: This method allows you to parse UTC timestamps stored as ticks (int64 values) and return a DateTime object.
var previousCandle = expectation Binance.Net-api.Client.Candles.GetPrevious(orders, 0);
var previousDate = new DateTime(previousCandle.DateUTC.SecondsEpokhi / TimeInterval.TicksVSecond);
DateTime.ParseExact
: If you have a UTC timestamp as a string in the formatYYYY-MM-DDTHH:MM:SSZ
, you can use this method to parse it into a DateTime object.
var previousCandle = expectation Binance.Net-api.Client.Candles.GetPrevious(orders, 0);
string previous_time_mark = previous_candle.Time_mark;
DateTime? previousDate = DateTime.ParseExact(previousTimestamp, "yyyy-MM-ddTHH:mm:ssZZ", null);
Using DateTime.FromTicks
To get the most out of the above solution, use the following code snippet as a starting point:
var orders = await Binance.Net-api.Client.Order.GetOrdersAsync(ordersSymbol, orderType, DateTime.Now, new OrderInfo { Side = Side.Buy });
var previousCandle = await Binance.Net-api.Client.Candles.GetPrevious(orders, 0);
var previousDate = new DateTime(previousCandle.DateUTC.SecondsEpokhi / TimeInterval.TicksVSecond);
// We use the data of the previous candle
Using DateTime.ParseExact
To parse UTC timestamps as strings in a specific format (for example, “YYYY-MM-DDTHH:MM:SSZ”), you can use the following code snippet:
var orders = await Binance.Net-api.Client.Order.GetOrdersAsync(ordersSymbol, orderType, DateTime.Now, new OrderInfo { Side = Side.Buy });
string previousTimestamp = orders[0].CandleTime;
DateTime? previousDate = DateTime.ParseExact(previousTimestamp, "yyyy-MM-ddTHH:mm:ssZ", null);
In conclusion, it should be noted that when working with Binance Net and searching for previous candles on Ethereum, the DateTime.AddSeconds
method is not reliable due to the mismatch between ticks and seconds. Using DateTime.FromTicks
or DateTime.ParseExact
, you can bypass this limitation and get the data of the previous candle exactly.
Do not forget to ensure the accuracy of analysis and conversion of timestamps when working with APIs that store prices in UTC format.