Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Sal/coingecko exploration #47

Open
wants to merge 4 commits into
base: dev-main
Choose a base branch
from
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
50 changes: 44 additions & 6 deletions app/plotly_dash/coingecko_data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""Gets crypto data from coingecko API
(https://www.coingecko.com/api/docs/v3)."""
"""Gets crypto data from coingecko API.

References:
1. https://www.coingecko.com/api/docs/v3
2. https://algotrading101.com/learn/coingecko-api-guide/
"""

import os

Expand All @@ -18,11 +22,11 @@
DAYS = 7 # Gets 7 days worth of historical data. 1 hr granularity.


def save_df(df):
def save_df_locally(df, file_name):
"""Save the data locally for now."""
file_dir = os.path.dirname(os.path.abspath(__file__))
csv_folder = "data"
data_file = os.path.join(file_dir, csv_folder, "coingecko_price_data.csv")
data_file = os.path.join(file_dir, csv_folder, f"{file_name}.csv")
# remove the file everytime we run this
if os.path.exists(data_file):
os.remove(data_file)
Expand All @@ -32,6 +36,37 @@ def save_df(df):
return


def get_coins_info():
"""Gets the info for supported coins on CoinGecko.

Output: List of dictionaries with coin id, coin symbol, and coin name.
ex: [{'id': 'zytara-dollar', 'symbol': 'zusd', 'name': 'Zytara Dollar'},
{'id': 'zyx', 'symbol': 'zyx', 'name': 'ZYX'}]
"""
coin_info_df = pd.DataFrame(CG.get_coins_list())

return coin_info_df


def get_dev_coin_data_for_ui():
"""Stores data locally that will be used to start rendering the
user page.

UI info: Show a list of coin names and for each coin name show a plot with
data.
"""
# let's get data for trending coins
trending_coins = CG.get_search_trending()
data = [
(coin.get("item").get("id"), coin.get("item").get("name"))
for coin in trending_coins.get("coins")
]
df = pd.DataFrame(data, columns=["coin_id", "coin_name"])
# TODO: get coin data

return df


def get_price_data():
"""Gets coin price data."""

Expand Down Expand Up @@ -64,7 +99,7 @@ def get_price_data():
df["hour"] = df["datetime"].dt.hour
df = df.melt(
id_vars=["datetime", "date", "hour"],
var_name="currency_name",
var_name="coin_name",
ignore_index=True,
)
df = df[df["value"].notna()]
Expand All @@ -74,4 +109,7 @@ def get_price_data():

if __name__ == "__main__":
data = get_price_data()
save_df(data)
save_df_locally(data, file_name="coingecko_price_data")
coin_info = get_coins_info()
save_df_locally(coin_info, file_name="coingecko_coin_info")
get_dev_coin_data_for_ui()
4 changes: 2 additions & 2 deletions app/plotly_dash/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def init_dashboard(server):
dash_app.index_string = render_base_html(server)

# Basic Fig
fig = px.line(df, x="datetime", y="value", color="currency_name")
fig = px.line(df, x="datetime", y="value", color="coin_name")
fig.update_layout(
title={
"text": "Coingecko Price Data",
Expand All @@ -54,7 +54,7 @@ def init_dashboard(server):
},
xaxis_title="Date",
yaxis_title="Price (USD)",
legend_title="Currency Name",
legend_title="Coin Name",
)

# Create Dash Layout
Expand Down
Loading