Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""Crypto Real-time Price Standard Model."""

from datetime import datetime
from typing import List, Optional, Set, Union

from openbb_core.provider.abstract.data import Data
from openbb_core.provider.abstract.query_params import QueryParams
from openbb_core.provider.utils.descriptions import (
DATA_DESCRIPTIONS,
QUERY_DESCRIPTIONS,
)
from pydantic import Field, field_validator


class CryptoPriceQueryParams(QueryParams):
"""Crypto Real-time Price Query."""

symbol: str = Field(
description=QUERY_DESCRIPTIONS.get("symbol", "")
+ " Can use coin IDs (bitcoin, ethereum) or symbols (btc, eth). Multiple symbols supported."
)

@field_validator("symbol", mode="before", check_fields=False)
@classmethod
def validate_symbol(cls, v: Union[str, List[str], Set[str]]):
"""Convert field to lowercase for coin IDs."""
if isinstance(v, str):
return v.lower().strip()
return ",".join([symbol.lower().strip() for symbol in list(v)])


class CryptoPriceData(Data):
"""Crypto Real-time Price Data."""

symbol: str = Field(description=DATA_DESCRIPTIONS.get("symbol", "") + " (Crypto)")
name: Optional[str] = Field(
default=None, description="Name of the cryptocurrency."
)
price: float = Field(
description="Current price of the cryptocurrency.",
json_schema_extra={"x-unit_measurement": "currency"},
)
market_cap: Optional[float] = Field(
default=None,
description="Market capitalization.",
json_schema_extra={"x-unit_measurement": "currency"},
)
market_cap_rank: Optional[int] = Field(
default=None,
description="Market cap rank.",
)
volume_24h: Optional[float] = Field(
default=None,
description="24-hour trading volume.",
json_schema_extra={"x-unit_measurement": "currency"},
)
change_24h: Optional[float] = Field(
default=None,
description="24-hour price change in percentage.",
json_schema_extra={"x-unit_measurement": "percent", "x-frontend_multiply": 100},
)
last_updated: Optional[datetime] = Field(
default=None,
description="Last updated timestamp.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,31 @@ async def historical(
) -> OBBject:
"""Get historical price data for cryptocurrency pair(s) within a provider."""
return await OBBject.from_query(Query(**locals()))


@router.command(
model="CryptoPrice",
examples=[
APIEx(parameters={"symbol": "bitcoin", "provider": "coingecko"}),
APIEx(parameters={"symbol": "bitcoin,ethereum", "vs_currency": "eur", "provider": "coingecko"}),
APIEx(
description="Get real-time prices for multiple cryptocurrencies with market data.",
parameters={
"symbol": "bitcoin,ethereum,cardano",
"vs_currency": "usd",
"include_market_cap": True,
"include_24hr_vol": True,
"include_24hr_change": True,
"provider": "coingecko",
},
),
],
)
async def quote(
cc: CommandContext,
provider_choices: ProviderChoices,
standard_params: StandardParams,
extra_params: ExtraParams,
) -> OBBject:
"""Get real-time price data for cryptocurrency(s) from CoinGecko."""
return await OBBject.from_query(Query(**locals()))
127 changes: 127 additions & 0 deletions openbb_platform/providers/coingecko/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# OpenBB CoinGecko Provider

This extension integrates the [CoinGecko](https://www.coingecko.com) data provider into the OpenBB Platform, providing comprehensive real-time and historical cryptocurrency data.

## Installation

To install the extension:

```bash
pip install openbb-coingecko
```

## Coverage

The following endpoints are covered by this extension:

- `obb.crypto.price.historical` - Historical cryptocurrency price data
- `obb.crypto.price.quote` - Real-time cryptocurrency prices
- `obb.crypto.search` - Search for cryptocurrencies

## Features

### Real-time Data
- Current cryptocurrency prices
- Market capitalization data
- 24-hour trading volume
- 24-hour price changes
- Last updated timestamps

### Historical Data
- Historical price charts
- Market cap history
- Volume data over time
- Flexible date ranges
- Multiple interval options

### Search Functionality
- Search cryptocurrencies by name or symbol
- Get comprehensive coin information
- Access to CoinGecko coin IDs for API calls

## API Key Setup

While CoinGecko offers a free tier, we recommend using an API key for production use:

1. Visit [CoinGecko API Pricing](https://www.coingecko.com/en/api/pricing)
2. Sign up for a Pro API plan
3. Visit your [Developer Dashboard](https://www.coingecko.com/en/developers/dashboard)
4. Copy your API key
5. Add it to your OpenBB credentials as `coingecko_api_key`

## Usage Examples

### Real-time Prices

```python
from openbb import obb

# Get current Bitcoin price
result = obb.crypto.price.quote(symbol="bitcoin", provider="coingecko")

# Get multiple cryptocurrencies with market data
result = obb.crypto.price.quote(
symbol="bitcoin,ethereum,cardano",
vs_currency="usd",
include_market_cap=True,
include_24hr_vol=True,
include_24hr_change=True,
provider="coingecko"
)
```

### Historical Data

```python
# Get Bitcoin historical data for the last 30 days
result = obb.crypto.price.historical(
symbol="bitcoin",
interval="30d",
vs_currency="usd",
provider="coingecko"
)

# Get historical data with specific date range
result = obb.crypto.price.historical(
symbol="ethereum",
start_date="2024-01-01",
end_date="2024-01-31",
vs_currency="eur",
provider="coingecko"
)
```

### Search Cryptocurrencies

```python
# Search for cryptocurrencies
result = obb.crypto.search(query="bitcoin", provider="coingecko")

# Get all available cryptocurrencies
result = obb.crypto.search(provider="coingecko")
```

## Supported Currencies

The provider supports pricing in multiple fiat and cryptocurrency currencies:

**Fiat:** USD, EUR, JPY, GBP, AUD, CAD, CHF, CNY, HKD, INR, KRW, MXN, NOK, NZD, PHP, PLN, RUB, SEK, SGD, THB, TRY, TWD, ZAR

**Crypto:** BTC, ETH, LTC, BCH, BNB, EOS, XRP, XLM, LINK, DOT, YFI

## Rate Limits

- **Free API:** 10-50 calls/minute
- **Pro API:** 500+ calls/minute (depending on plan)

For production use, we recommend upgrading to a Pro plan to avoid rate limiting.

## Data Sources

CoinGecko aggregates data from over 400+ exchanges worldwide, providing comprehensive and reliable cryptocurrency market data. The platform tracks 10,000+ different crypto-assets and is one of the most trusted sources in the cryptocurrency space.

## Support

For issues related to this provider, please visit the [OpenBB Platform repository](https://github.com/OpenBB-finance/OpenBB).

For CoinGecko API-specific questions, refer to the [CoinGecko API Documentation](https://docs.coingecko.com/reference/introduction).
34 changes: 34 additions & 0 deletions openbb_platform/providers/coingecko/openbb_coingecko/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""CoinGecko Provider Module."""

from openbb_core.provider.abstract.provider import Provider
from openbb_coingecko.models.crypto_historical import CoinGeckoCryptoHistoricalFetcher
from openbb_coingecko.models.crypto_price import CoinGeckoCryptoPriceFetcher
from openbb_coingecko.models.crypto_search import CoinGeckoCryptoSearchFetcher

coingecko_provider = Provider(
name="coingecko",
website="https://www.coingecko.com",
description=(
"CoinGecko is the world's largest independent cryptocurrency data aggregator "
"with over 10,000+ different crypto-assets tracked across more than 400+ "
"exchanges worldwide. CoinGecko provides real-time pricing, market data, "
"and comprehensive cryptocurrency information."
),
credentials=["coingecko_api_key"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"coingecko_api_key" -> "api_key"

Entering like this creates a duplication of the provider name.

OpenBBError:
[Error] -> Missing credential 'coingecko_coingecko_api_key'. Check https://www.coingecko.com to get it. Known more about how to set provider credentials at https://docs.openbb.co/platform/getting_started/api_keys.

fetcher_dict={
"CryptoHistorical": CoinGeckoCryptoHistoricalFetcher,
"CryptoPrice": CoinGeckoCryptoPriceFetcher,
"CryptoSearch": CoinGeckoCryptoSearchFetcher,
},
repr_name="CoinGecko",
instructions=(
"To get a CoinGecko API key:\n"
"1. Visit https://www.coingecko.com/en/api/pricing\n"
"2. Sign up for a Pro API plan (required for API key access)\n"
"3. Once subscribed, visit your developer dashboard at "
"https://www.coingecko.com/en/developers/dashboard\n"
"4. Copy your API key and add it to your OpenBB credentials as 'coingecko_api_key'\n\n"
"Note: CoinGecko offers a free tier with limited requests, "
"but an API key is recommended for production use."
),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""CoinGecko provider models."""
Loading
Loading