Finding Previous Candles on Ethereum Using Binance Net: DateTime.AddSeconds
As an avid cryptocurrency trader, working with data feeds from various exchanges is probably nothing new for you. However, when finding previous candles on Ethereum using the Binance.Net API wrapper, you often encounter a problem that can render your code unusable. In this article, we will explore why DateTime.AddSeconds is not working as intended and provide a solution to overcome this limitation.
The Problem: DateTime.AddSeconds
When you use the DateTime.AddSeconds method, seconds are added to the specified date/time value. This can be problematic in certain scenarios, especially when working with candle data from exchanges that store their prices in UTC (Coordinated Universal Time).
On Ethereum, candles are usually stored as UTC timestamps, meaning that any adjustments made to the timestamp via DateTime.AddSeconds will not result in a correct representation of the previous candle.
The 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 (instead of seconds) for its API returns, you need 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 = await Binance.NetApi.Customer.Candles.GetPrevious(orders, 0);
var previousDate = new DateTime(previousCandle.DateUTC.SecondsSinceEpoch / TimeSpan.TicksPerSecond);
DateTime.ParseExact
: If you have the UTC timestamp in the formatYYYY-MM-DDTHH:MM:SSZ
, you can use this method to parse it into a DateTime object.
var previousCandle = awaitBinance.NetApi.Customer.Candles.GetPrevious(orders, 0);
String previousTimestamp = previousCandle.Timestamp;
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(orderSymbol, orderType, DateTime.Now, new OrderInfo { Page = Page.Buy });
var previousCandle = await Binance.NetApi.Customer.Candles.GetPrevious(orders, 0);
var previousDate = new DateTime(previousCandle.DateUTC.SecondsSinceEpoch / TimeSpan.TicksPerSecond);
//Use the previous candle data
Using DateTime.ParseExact
To parse UTC timestamps as strings in a specific format (e.g. “YYYY-MM-DDTHH:MM:SSZ”), you can use the following code snippet:
var orders = await Binance.Net-api.Client.Order.GetOrdersAsync(orderSymbol, orderType, DateTime.Now, new OrderInfo { Page = Page.Buy });
String previousTimestamp = Orders[0].CandleTime;
DateTime? previousDate = DateTime.ParseExact(previousTimestamp, "yyyy-MM-ddTHH:mm:ssZ", null);
To summarize, when working with Binance Net and looking for previous candles on Ethereum, DateTime.AddSeconds is not a reliable method due to the discrepancy between ticks and seconds. By using DateTime.FromTicks or DateTime.ParseExact you can bypass this limitation and get the previous candle data accurately.
Remember to ensure accurate timestamp parsing and conversion when working with APIs that store their prices in UTC.