Module: CoingeckoRuby::Client::Prices

Included in:
CoingeckoRuby::Client
Defined in:
lib/coingecko_ruby/client/prices.rb

Instance Method Summary collapse

Instance Method Details

#daily_historical_price(id, currency: 'usd', days: 7, **options) ⇒ Hash

Fetches a coin’s historical price data in daily ranges.

Examples:

Fetch Bitcoin’s daily historical price within the last 14 days.

client.daily_historical_prices('bitcoin', days: 14)

Sample response object (truncated)

{
  "prices" => [
    [1620000000000, 56600.74528738432], # [UNIX timestamp for minutely price data, coin price in given currency]
    [1620086400000, 57200.30029871162],
    [1620172800000, 53464.37021950372],
  ], "market_caps" => [
    [1620000000000, 1057850321948.5465],
    [1620086400000, 1069571255195.5189],
    [1620172800000, 999775008412.3738],
  ], "total_volumes" => [
    [1620000000000, 39072664393.929405],
    [1620086400000, 54132470274.07509],
    [1620172800000, 71296763919.13268],
  ]
}

Parameters:

  • id (String)

    the coin id to fetch.

  • days (Integer) (defaults to: 7)

    the number of days to fetch daily historical prices. Defaults to 7 days.

  • currency (String) (defaults to: 'usd')

    the currency used to display daily historical price. Defaults to USD.

Returns:

  • (Hash)

    returns the coin’s daily historical price data within the number of days given.



231
232
233
# File 'lib/coingecko_ruby/client/prices.rb', line 231

def daily_historical_price(id, currency: 'usd', days: 7, **options)
  get "coins/#{id}/market_chart", vs_currency: currency, days: days, interval: 'daily', **options
end

#exchange_rate(from:, to: 'usd') ⇒ Hash

Fetches the exchange rate for a coin or currency in the given coin or currency.

Examples:

Fetch the exchange rate for BTC-USD.

client.exchange_rate(from: 'bitcoin', to: 'usd')

Sample response object

{
  "bitcoin" => {
    "usd" => 47931 # current price in given currency
  }
}

Fetch the exchange rate for BTC-ETH.

client.get_exchange_rate(from: 'bitcoin', to: 'eth')

Sample response object

{
  "bitcoin" => {
    "eth" => 12.71434 # current price in given currency
  }
}

Parameters:

  • from (String)

    the coin id or currency to be converted.

  • to (String) (defaults to: 'usd')

    the coin id or currency to convert against. Defaults to ‘USD’.

Returns:

  • (Hash)

    returns the coin’s exchange rate against the given coin or currency.



309
310
311
# File 'lib/coingecko_ruby/client/prices.rb', line 309

def exchange_rate(from:, to: 'usd')
  price(from, currency: to)
end

#get_daily_historical_prices(id:, days:, currency: 'usd') ⇒ Object

Deprecated.


236
237
238
# File 'lib/coingecko_ruby/client/prices.rb', line 236

def get_daily_historical_prices(id:, days:, currency: 'usd')
  daily_historical_price(id, currency: currency, days: days)
end

#get_exchange_rate(from:, to: 'usd') ⇒ Object

Deprecated.

Use #exchange_rate instead



314
315
316
# File 'lib/coingecko_ruby/client/prices.rb', line 314

def get_exchange_rate(from:, to: 'usd')
  exchange_rate(from: from, to: to)
end

#get_historical_price_on_date(id:, date:) ⇒ Object

Deprecated.

Use #historical_price instead



129
130
131
# File 'lib/coingecko_ruby/client/prices.rb', line 129

def get_historical_price_on_date(id:, date:)
  historical_price(id, date: date)
end

#get_hourly_historical_prices(id:, days:, currency: 'usd') ⇒ Object

Deprecated.


202
203
204
# File 'lib/coingecko_ruby/client/prices.rb', line 202

def get_hourly_historical_prices(id:, days:, currency: 'usd')
  hourly_historical_price(id, currency: currency, days: days)
end

#get_minutely_historical_prices(id:, currency: 'usd') ⇒ Object

Deprecated.


165
166
167
# File 'lib/coingecko_ruby/client/prices.rb', line 165

def get_minutely_historical_prices(id:, currency: 'usd')
  minutely_historical_price(id, currency: currency)
end

#get_ohlc(id:, days:, currency: 'usd') ⇒ Object

Deprecated.

Use #ohlc instead



263
264
265
# File 'lib/coingecko_ruby/client/prices.rb', line 263

def get_ohlc(id:, days:, currency: 'usd')
  ohlc(id, currency: currency, days: days)
end

#get_price(id:, currency: 'usd', options: {}) ⇒ Object

Deprecated.

Use #price or #prices instead



46
47
48
# File 'lib/coingecko_ruby/client/prices.rb', line 46

def get_price(id:, currency: 'usd', options: {})
  price(id, currency, **options)
end

#historical_price(id, date:, **options) ⇒ Hash

Fetches historical price data for a coin at a given date.

Examples:

Fetch Bitcoin’s price on 30th December, 2017.

client.historical_price('bitcoin', date: '30-12-2017')

Sample response object (truncated)

{
  "id": "bitcoin",
  "symbol": "btc",
  "name": "Bitcoin",
  "localization": {
    "en": "Bitcoin",
    "ja": "ビットコイン",
    "id": "Bitcoin"
  },
  "image": {
    "thumb": "https://assets.coingecko.com/coins/images/1/thumb/bitcoin.png?1547033579",
    "small": "https://assets.coingecko.com/coins/images/1/small/bitcoin.png?1547033579"
  },
  "market_data": {
    "current_price": {
      "btc": 1,
      "eth": 18.483094024188404,
      "eur": 11345.8976447824,
      "gbp": 10079.0677868681,
      "usd": 13620.3618741461,
    },
    "market_cap": {
      "btc": 16772375,
      "eth": 310005384.13394696,
      "eur": 190297650009.907,
      "gbp": 169049904571.772,
      "usd": 228445816988.881,
    },
    "total_volume": {
      "btc": 264345.493482963,
      "eth": 4885922.610916088,
      "eur": 2999236911.91719,
      "gbp": 2664356147.96788,
      "usd": 3600481281.03768,
    }
  },
  "community_data": {
    "facebook_likes": null,
    "twitter_followers": 603664,
    "reddit_average_posts_48h": 2.042,
    "reddit_average_comments_48h": 445.896,
    "reddit_subscribers": 612412,
    "reddit_accounts_active_48h": "14074.0"
  },
  "developer_data": {
    "forks": 13660,
    "stars": 23665,
    "subscribers": 2513,
    "total_issues": 3591,
    "closed_issues": 3022,
    "pull_requests_merged": 5038,
    "pull_request_contributors": 450,
    "code_additions_deletions_4_weeks": {
      "additions": null,
      "deletions": null
    },
    "commit_count_4_weeks": 147
  },
  "public_interest_stats": {
    "alexa_rank": 2912,
    "bing_matches": null
  }
}

Parameters:

  • id (String)

    the coin id to fetch.

  • date (String)

    the date to fetch the historical price, date given must be in the form of ‘DD-MM-YYYY’ (e.g: ‘14-05-2021’). Defaults to today’s date.

Returns:

  • (Hash)

    returns the coin’s historical price data on the given date.



122
123
124
125
126
# File 'lib/coingecko_ruby/client/prices.rb', line 122

def historical_price(id, date:, **options)
  date = Time.now.strftime('%d-%m-%Y') if date.nil?

  get "coins/#{id}/history", date: date, **options
end

#hourly_historical_price(id, currency: 'usd', days: 7, **options) ⇒ Hash

Note:

Hourly historical data is only available within the last 90 days.

Fetches a coin’s historical price data in 1 hour ranges.

Examples:

Fetch Bitcoin’s hourly historical price in USD within the last 7 days.

client.hourly_historical_price('bitcoin', days: 7)

Sample response object (truncated)

{
  "prices" => [
    [1620540153122, 58533.64354969528], # [UNIX timestamp for minutely price data, coin price in given currency]
    [1620543723872, 58384.26782776662],
    [1620547381049, 58022.030773147424],
  ], "market_caps" => [
    [1620540153122, 1094831531450.288],
    [1620543723872, 1092040480555.5005],
    [1620547381049, 1085268316789.8125],
  ], "total_volumes" => [
    [1620540153122, 71975097904.11748],
    [1620543723872, 72192190625.33702],
    [1620547381049, 72064327648.28767],
  ]
}

Parameters:

  • id (String)

    the coin id to fetch.

  • currency (String) (defaults to: 'usd')

    the currency used to display hourly historical price. Defaults to ‘USD’

  • days (Integer) (defaults to: 7)

    the number of days to fetch hourly historical prices (min: 1 day, max: 90 days). Defaults to 7 days

Returns:

  • (Hash)

    returns the coin’s hourly historical price data within the number of days given.



195
196
197
198
199
# File 'lib/coingecko_ruby/client/prices.rb', line 195

def hourly_historical_price(id, currency: 'usd', days: 7, **options)
  return daily_historical_price(id, currrency: currency, days: days) if days > 90

  get "coins/#{id}/market_chart", vs_currency: currency, days: days, **options
end

#minutely_historical_price(id, currency: 'usd', **options) ⇒ Hash

Note:

Minutely historical data is only available within the last 24 hours.

Fetches a coin’s historical price data in 5 - 10 minutes ranges.

Examples:

Fetch Bitcoin’s minutely historical price within the last 24 hours.

client.minutely_historical_price('bitcoin')

Sample response object (truncated)

{
  "prices" => [
    [1621057474114, 49364.605172521166], # [UNIX timestamp for minutely price data, coin price in given currency]
    [1621057731319, 49482.24672699064],
    [1621058047104, 49759.16253913667],
  ],
  "market_caps" => [
    [1621057474114, 923529491095.7441],
    [1621057731319, 923529491095.7441],
    [1621058047104, 925839705121.9677],
  ],
  "total_volumes" => [
    [1621057474114, 55351845301.87241],
    [1621057731319, 55565821585.076706],
    [1621058047104, 57046967935.326035],
  ]
}

Parameters:

  • id (String)

    the coin id to fetch.

  • currency (String) (defaults to: 'usd')

    the currency used to display minutely historical price. Defaults to ‘USD’

Returns:

  • (Hash)

    returns the coin’s minutely historical price data within the last 24 hours.



160
161
162
# File 'lib/coingecko_ruby/client/prices.rb', line 160

def minutely_historical_price(id, currency: 'usd', **options)
  get "coins/#{id}/market_chart", vs_currency: currency, days: 1, **options
end

#ohlc(id, currency: 'usd', days: 7, **options) ⇒ Array<Array<String, Float>>

Fetches a coin’s open, high, low, and close (OHLC) data within the number of days given.

Examples:

Fetch Bitcoin’s OHLC data within the last 7 days.

client.ohlc('bitcoin', days: 7)

Sample response object (truncated)

[
  [1620547200000, 58384.27, 58384.27, 58384.27, 58384.27], # [UNIX timestamp for OHLC data, open, high, low, close]
  [1620561600000, 58022.03, 58214.96, 57943.18, 58048.35],
  [1620576000000, 57956.7, 57956.7, 56636.68, 57302.22],
]

Parameters:

  • id (String)

    the coin id to fetch.

  • days (Integer, String) (defaults to: 7)

    the number of days to fetch daily historical prices. Valid values: 1/7/14/30/90/180/365/‘max’. Defaults to 7 days.

  • currency (String) (defaults to: 'usd')

    the currency to display OHLC data. Defaults to ‘USD’.

Returns:

  • (Array<Array<String, Float>>)

    returns the coin’s OHLC data within the number of days given. If the number of days given is between 1-2 days, the OHLC data is returned in 30-minute intervals. If the number of days given is between 3-30 days, the OHLC data is returned in 4-hour intervals. If the number of days given is more than 30 days, the OHLC data is returned in 4-day intervals.



258
259
260
# File 'lib/coingecko_ruby/client/prices.rb', line 258

def ohlc(id, currency: 'usd', days: 7, **options)
  get "coins/#{id}/ohlc", vs_currency: currency, days: days, **options
end

#price(ids, currency: 'usd', **options) ⇒ Hash

Fetches the current price for a coin in the given coin or currency.

Examples:

Fetch the current price in USD for Bitcoin.

client.price('bitcoin', currency: 'usd')

Sample response object

{
  "bitcoin" => {
    "usd" => 47931 # current price in given currency
  }
}

Fetch the current price, market cap, 24 hour volume, 24 hour price change and last updated at for Bitcoin.

client.price('bitcoin', 'usd', include_market_cap: true, include_24hr_vol: true, include_24hr_change: true, include_last_updated_at: true)

Sample response object

{
  "bitcoin" => {
    "usd" => 48217, # current price in given currency
    "usd_market_cap" => 905542853013.7438, # market cap in given currency
    "usd_24h_vol" => 66724791943.1098, # 24 hour volume in given currency
    "usd_24h_change" => -2.813036070702345, # 24 hour price change percentage
    "last_updated_at" => 1621142197 # last updated at UNIX timestamp for the given coin
  }
}

Parameters:

  • ids (String, Array)

    the coin id or array of ids to fetch.

  • currency (String, Array) (defaults to: 'usd')

    the currency or array of currencies to display the coin’s price. Defaults to ‘USD’

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :include_market_cap (Boolean)

    include market cap data.

  • :include_24hr_vol (Boolean)

    include 24 hour volume data.

  • :include_24hr_change (Boolean)

    include 24 hour price change data.

  • :include_last_updated_at (Boolean)

    include last updated at value for the given coin.

Returns:

  • (Hash)

    returns the given coin id and its current price.



34
35
36
37
38
# File 'lib/coingecko_ruby/client/prices.rb', line 34

def price(ids, currency: 'usd', **options)
  ids = ids.join(',') if ids.is_a? Array
  currency = currency.join(',') if currency.is_a? Array
  get 'simple/price', ids: ids, vs_currencies: currency, **options
end

#prices(ids, currency: 'usd', **options) ⇒ Object

See Also:

  • for {#price}


41
42
43
# File 'lib/coingecko_ruby/client/prices.rb', line 41

def prices(ids, currency: 'usd', **options)
  price(ids, currency: currency, **options)
end

#supported_currenciesArray<String>

Fetches the list of currencies currently supported by CoinGecko’s API.

Examples:

Fetch supported currencies.

client.supported_currencies

Sample response object (truncated)

[
  "btc",
  "eth",
  "yfi",
  "usd",
  "aud",
  "eur",
  "gbp",
]

Returns:

  • (Array<String>)

    returns the list of currencies currently supported by CoinGecko’s API.



283
284
285
# File 'lib/coingecko_ruby/client/prices.rb', line 283

def supported_currencies
  get 'simple/supported_vs_currencies'
end