bchavez / Coinbase

:moneybag: A .NET/C# implementation of the Coinbase API.
https://developers.coinbase.com/api/v2
MIT License
170 stars 92 forks source link

Price is not Updating #65

Closed forReason closed 4 years ago

forReason commented 4 years ago

What is the expected behavior?

The exchange rates change

What is the actual behavior?

The exchange rates are staying the same forever

Any possible solutions?

private async Task<decimal> GetSpotPrice(string selector)
        {
            //anonymousClient = new CoinbaseClient();
            Response<Money> buyPriceResponse = await anonymousClient.Data.GetBuyPriceAsync(selector);
            Response<Money> sellPriceonRateResponse = await anonymousClient.Data.GetSellPriceAsync(selector);
            decimal buyPrice = buyPriceResponse.Data.Amount;
            decimal sellPrice = sellPriceonRateResponse.Data.Amount;
            decimal midMarket = (buyPrice + sellPrice) / (decimal) 2;
            return midMarket;
        }

How do you reproduce the issue?

CoinbaseClient anonymousClient = new CoinbaseClient();
private static async void GetSpotPrices(string selector)
{
    while (true) 
    {
        CoinBaseClient coinBaseClient = new CoinBaseClient();
        Task<decimal> getPrice = coinBaseClient.GetSpotPrice(selector);
        decimal price = await Task<decimal>.Run(() =>getPrice);
        getPrice.Dispose();
        Tensor.AddPrice(price);
        Thread.Sleep(1000);
    }
}
private async Task<decimal> GetSpotPrice(string selector)
{
    Response<Money> conversionRateResponse = await anonymousClient.Data.GetSpotPriceAsync(selector);
    decimal conversionRate = conversionRateResponse.Data.Amount;
    return conversionRate;
}

Can you identify the location in the source code where the problem exists?

The Rest Api might cash the prices

If the bug is confirmed, would you be willing to submit a PR?

Yes / No (Help can be provided if you need assistance submitting a PR)

bchavez commented 4 years ago

Hi Julian,

The price does change. There is no caching in the API. You might need to increase the your polling interval to see price changes.

async Task Main()
{
   var client = new CoinbaseClient();

   while (true)
   {
      var price = await client.Data.GetSpotPriceAsync("BTC-USD");
      price.Data.Amount.Dump();

      await Task.Delay(TimeSpan.FromSeconds(30));
   }
}
7096.855
7096.855
7097.235
7097.235
7097.23
7097.235
7098.125
7097.775

Also, see https://developers.coinbase.com/api/v2#get-spot-price

Note that exchange rates fluctuates so the price is only correct for seconds at the time.

The price you get from Coinbase (for consumer customers) is derived from and lags behind the Coinbase Pro exchange price. So, you'll have to account for that in your code.

If you need more real-time market exchange prices, consider using the Coinbase.Pro library that connects directly to the exchange and supports real-time socket feeds.

https://github.com/bchavez/Coinbase.Pro

There are 3 ways:

I hope that helps.

Thanks, Brian