Skip to content

Commit

Permalink
Added: method for fetching live market data.
Browse files Browse the repository at this point in the history
  • Loading branch information
im-n1 committed Jan 18, 2023
1 parent d61f14b commit 7309690
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
23 changes: 22 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Retrieves historical data.
.. code-block:: python
k = Karpet(date(2019, 1, 1), date(2019, 5, 1))
df = k.fetch_crypto_historical_data(symbol="ETH") # Dataframe with historical data.
df = k.fetch_crypto_historical_data(id="ethereum") # Dataframe with historical data.
df.head()
price market_cap total_volume
Expand Down Expand Up @@ -220,9 +220,30 @@ Lists all coins/tokes with some basic info.
"id": 1,
}
fetch_crypto_live_data()
~~~~~~~~~~~~~~~~~~~~~~~~
Retrieves live market data.

.. code-block:: python
k = Karpet()
df = k.fetch_crypto_live_data(id="ethereum") # Dataframe with live data.
df.head()
open high low close
2023-01-16 20:00:00 1593.01 1595.05 1593.01 1594.28
2023-01-16 20:30:00 1593.37 1593.37 1589.03 1589.35
2023-01-16 21:00:00 1592.68 1593.66 1584.71 1587.87
2023-01-16 21:30:00 1587.28 1587.28 1583.13 1583.13
2023-01-16 22:00:00 1573.99 1580.11 1573.99 1579.97
Changelog
---------

0.4.8
~~~~~
- new ``fetch_crypto_live_data()``

0.4.7.
~~~~~~
- dependencies updated
Expand Down
47 changes: 45 additions & 2 deletions karpet/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def get_quick_search_data(self):

def fetch_crypto_historical_data(self, symbol=None, id=None):
"""
Retrieve basic historical information for a specific cryptocurrency from coingecko.com.
Retrieve basic historical information (by days) for a specific
cryptocurrency from coingecko.com.
Coin ID can be retreived by get_coin_ids() method.
Output dataframe has following columns:
Expand All @@ -86,7 +87,7 @@ def fetch_crypto_historical_data(self, symbol=None, id=None):
Index is datetime64[ns].
:param str symbol: Coin symbol - i.e. BTC, ETH, ...
:param str id: Coin ID (baed on coingecko.com).
:param str id: Coin ID (based on coingecko.com).
:raises Exception: If data couldn't be download form the internet.
:return: Dataframe with historical data.
:rtype: pd.DataFrame
Expand Down Expand Up @@ -134,6 +135,48 @@ def fetch_crypto_historical_data(self, symbol=None, id=None):

return df

def fetch_crypto_live_data(self, symbol=None, id=None):
"""
Retrieve OHLC price data for past 24 hours for a specific
cryptocurrency from coingecko.com.
Coin ID can be retreived by get_coin_ids() method.
Output dataframe has following columns:
- open
- high
- low
- close
Index is datetime64[ns].
:param str symbol: Coin symbol - i.e. BTC, ETH, ...
:param str id: Coin ID (based on coingecko.com).
:raises Exception: If data couldn't be download form the internet.
:return: Dataframe with OHLC data.
:rtype: pd.DataFrame
"""

id = self._get_coin_id_from_params(symbol, id)

# Fetch data.
data = []
url = f"https://api.coingecko.com/api/v3/coins/{id}/ohlc?vs_currency=usd&days=1"

# Fetch and check the response.
data = self._get_json(url)

if not data:
raise Exception("Couldn't download necessary data from the internet.")

# Assembly the dataframe.
data = np.array(data)
return pd.DataFrame.from_records(
data[:, 1:],
index=pd.to_datetime(data[:, 0], unit="ms"),
columns=["open", "high", "low", "close"],
)

def fetch_crypto_exchanges(self, symbol=None):
"""
Fetches all exchanges where the given symbol
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "karpet"
version = "0.4.7"
version = "0.4.8"
description = "Library for fetching coin/token historical data, trends and more."
authors = ["n1 <[email protected]>"]
readme = "README.rst"
Expand Down
9 changes: 9 additions & 0 deletions test_karpet.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,12 @@ def test_get_coin_ids():

k = Karpet()
assert k.get_coin_ids("BTC") == ["bitcoin"]


def test_fetch_crypto_live_data():

k = Karpet()
df = k.fetch_crypto_live_data(id="ethereum")

assert 0 < len(df)
assert list(df.columns) == ["open", "high", "low", "close"]

0 comments on commit 7309690

Please sign in to comment.