-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkets.py
More file actions
135 lines (113 loc) · 4.4 KB
/
markets.py
File metadata and controls
135 lines (113 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import requests
import requests_cache
import json
import time
import pandas as pd
from nicegui import ui
import plotly.graph_objects as go
#session = CachedSession('demo_cache', cache_control=True)
#session = CachedSession('demo_cache', )
requests_cache.install_cache('cache')
def convert_numbers_to_strings(obj):
if isinstance(obj, float):
return str(obj)
return obj
def fetch_all_markets():
url = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&per_page=250&sparkline=true&price_change_percentage=1h%2C24h%2C7d%2C14d%2C30d%2C200d%2C1y&precision=full"
headers = {
"accept": "application/json",
"x-cg-demo-api-key": "CG-ULEXj9kgbnAK2kGvieVgsDBL"
}
page_num = 1
dfAll = pd.DataFrame();
all_data = []
while True:
params = {"page": page_num}
response = requests.get(url, headers=headers, params=params)
print("request page: ", page_num)
data = json.loads(response.text)
if not data:
break
df = pd.DataFrame(data)
# df = pd.read_json(response.text)
dfAll = pd.concat([df,dfAll])
all_data.extend(data)
page_num += 1
#time.sleep(1) # Respect rate limits (e.g., 10-30 calls per minute)
dfAll.to_pickle("markets.pkl")
print(f"Total coins fetched: {len(all_data)}")
return
def fetch_all_coins():
url = "https://api.coingecko.com/api/v3/coins/list?include_platform=true"
headers = {
"accept": "application/json",
#"x-cg-demo-api-key": "CG-ULEXj9kgbnAK2kGvieVgsDBL"
}
response = requests.get(url, headers=headers)
df_coins = pd.read_json(response.text)
df_coins.to_pickle("coins.pkl")
return
def fetch_coins_simple(coinIds):
url = "https://api.coingecko.com/api/v3/simple/price?vs_currencies=usd&include_market_cap=true&include_24hr_vol=true&include_24hr_change=true&include_last_updated_at=true&precision=full"
headers = {
"accept": "application/json",
#"x-cg-demo-api-key": "CG-ULEXj9kgbnAK2kGvieVgsDBL"
}
url += f"&ids={coinIds}"
response = requests.get(url, headers=headers)
print(response.text)
df_coins = pd.read_json(response.text)
df_coins.to_pickle("coins_simple.pkl")
return
def create_market_table(df):
columns = ['id', 'symbol', 'name', 'current_price', 'market_cap', 'total_volume', 'price_change_percentage_24h']
# Create a ColumnDataSource from the Pandas DataFrame
source = ColumnDataSource(df)
# Define table columns
columns = [TableColumn(field=col, title=col) for col in columns]
# Create DataTable widget
data_table = DataTable(source=source, columns=columns, width=400, height=280, sizing_mode='stretch_both')
return data_table
def create_simple_table(df):
columns = ['usd', 'usd_market_cap', 'usd_24h_vol', 'usd_24h_change', 'last_updated_at']
# Create a ColumnDataSource from the Pandas DataFrame
source = ColumnDataSource(df)
# Define table columns
columns = [TableColumn(field=col, title=col) for col in columns]
# Create DataTable widget
data_table = DataTable(source=source, columns=columns, width=400, height=280, sizing_mode='stretch_both')
return data_table
#fetch_all_markets()
#fetch_all_coins()
df = pd.read_pickle("markets.pkl")
df_coins = pd.read_pickle("coins.pkl")
df = df_coins.merge(df, on = 'id', how = 'left')
df = df[df['platforms'].apply(lambda x: "solana" in x)]
df = df[df['market_cap'].apply(lambda x: x > 0)]
df = df[df['total_volume'].apply(lambda x: x > 10000)]
df = df[df['price_change_percentage_24h'].apply(lambda x: x > 10)]
coinIds = ''
coinTotal = 0
for index, row in df.iterrows():
if (coinTotal > 0):
coinIds += "%2C"
coinIds += f"{row['id']}"
coinTotal+=1
print (f"Total coins: {coinTotal}")
#print(df["sparkline_in_7d"].iloc[0])
for index, row in df.iterrows():
with ui.row():
label = ui.label(row['id'])
values = row["sparkline_in_7d"].get('price')
#df2=pd.DataFrame({'x': , 'y': })
#row.add_slot(ui.plotly(df2.plot('x', 'y')))
fig = go.Figure([go.Scatter(x=list(range(168)), y=values)])
fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
ui.plotly(fig).classes('w-full h-40')
#
row = ui.row()
#row.add_slot(ui.label('label 1'))
#values = df["sparkline_in_7d"].iloc[0].get('price')
#df2=pd.DataFrame({'x': range(1,169), 'y': values })
#row.add_slot(ui.plotly(df2.plot('x', 'y')))
ui.run(show=False)