dougdellolio / coinbasepro-csharp

The unofficial .NET/C# client library for the Coinbase Pro/GDAX API
MIT License
192 stars 90 forks source link

GetHistoricRatesAsync Precision Issue #286

Closed andrew-benson closed 3 years ago

andrew-benson commented 3 years ago

Hi @dougdellolio , I've been passing to the service a date time with a milliseconds value of 999. The string format value "s" is stripping the millisecond precision out of the date time therefore returning inaccurate data, usually for the Close property.

source

var isoStart = start.ToString("s");
var isoEnd = end.ToString("s");

The query being generated looks something like this. https://api.pro.coinbase.com/products/BTC-GBP/candles?start=2021-05-03T18:45:00&end=2021-05-03T18:59:59&granularity=900

Here's one suggestion

var isoStart = new DateTime(start.Ticks, DateTimeKind.Utc).ToString("o");
var isoEnd = new DateTime(end.Ticks, DateTimeKind.Utc).ToString("o");

produces the expected milliseconds https://api.pro.coinbase.com/products/BTC-GBP/candles?start=2021-05-03T19:00:00.0000000Z&end=2021-05-03T19:15:00.9990000Z&granularity=900

.ToString("o") however produces a bit more precision than we need so you could alternatively use .ToString("yyyy-MM-ddTHH:mm:ss.fffK"):

var isoStart = new DateTime(start.Ticks, DateTimeKind.Utc).ToString("yyyy-MM-ddTHH:mm:ss.fffK");
var isoEnd = new DateTime(end.Ticks, DateTimeKind.Utc).ToString("yyyy-MM-ddTHH:mm:ss.fffK");

DateTime constructor takes milliseconds 0 to 999 so this looks neater in my opinion: https://api.pro.coinbase.com/products/BTC-GBP/candles?start=2021-05-03T19:15:00.000Z&end=2021-05-03T19:29:59.999Z&granularity=900

Kind regards Andrew

dougdellolio commented 3 years ago

hey @andrew-benson. nice catch. this change definitely makes sense. The change can be found here: https://github.com/dougdellolio/coinbasepro-csharp/commit/ccee8f5eea51ad25ac666b43db84071a08a88357 and will go out with the next release.