Ethereum: Finding previous candle doesn’t work because of DateTime.AddSeconds

Avatar de admin

Finding Past Candles on Ethereum Using Binance Net: DateTime.AddSeconds Issue

As an avid cryptocurrency trader, you are probably no stranger to working with data feeds from various exchanges. However, when it comes to finding past candles on Ethereum using the Binance.Net API wrapper, a common issue arises that can render your code unusable. In this article, we will explore why DateTime.AddSeconds does not work as intended and provide a solution to overcome this limitation.

Issue: DateTime.AddSeconds

When you use the DateTime.AddSeconds method, it adds seconds to the specified date/time value. This can be problematic in certain scenarios, especially when working with candle data from exchanges that record their prices in UTC (Coordinated Universal Time).

In Ethereum, candles are typically stored as UTC timestamps, which means that adjustments to the timestamp via the DateTime.AddSeconds function will not result in a correct representation 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 for its API returns (instead of seconds), you need to use one of two approaches:

  • DateTime.FromTicks: This method allows you to parse UTC timestamps (int64 values) stored as ticks and return a DateTime object.

var previousCandle = await Binance.Net-api.Client.Candles.GetPrevious(orders, 0);

var previousDate = new DateTime(previousCandle.DateUTC.SecondsSinceEpoch / TimeSpan.TicksPerSecond);

  • DateTime.ParseExact

    : If you have a UTC timestamp as a string in the format YYYY-MM-DDTHH:MM:SSZ, you can use this method to parse it into a DateTime object.

var previousCandle = await Binance.Net-api.Client.Candles.GetPrevious(orders, 0);

string previousTimestamp = previousCandle.Timestamp;

Date and time? previousDate = DateTime.ParseExact(previousTimestamp, "yyyy-MM-ddTHH:mm:ssZZ", null);

Using DateTime.FromTicks

Use the following code snippet as a starting point to get the most out of the above solution:

var orders = await Binance.Net-api.Client.Order.GetOrdersAsync(ordersymbol, 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.SecondsSinceEpoch / TimeSpan.TicksPerSecond);

// Use previous candle data

Using DateTime.ParseExact

Ethereum: Finding previous candle doesn't work because of DateTime.AddSeconds

You can parse UTC timestamps as strings in a specific format (e.g. «YYYY-MM-DDTHH:MM:SSZ») using the following code snippet:

var orders = await Binance.Net-api.Client.Order.GetOrdersAsync(ordersymbol, ordertype, datetime. now, new OrderInfo { Side = Side.Buy });

string previousTimestamp = orders[0].CandleTime;

Date and time? previousDate = DateTime.ParseExact(previousTimestamp, "yyyy-MM-ddTHH:mm:ssZ", null);

In summary, when working with Binance Net and searching for past candles on Ethereum, DateTime.AddSeconds is not a reliable method due to the difference between ticks and seconds. By using either DateTime.FromTicks or DateTime.ParseExact, you can overcome this limitation and accurately retrieve past candle data.

Remember to ensure accurate timestamp parsing and conversion when working with APIs that store their prices in UTC.

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *