Update app.py
Browse files
app.py
CHANGED
@@ -19,15 +19,21 @@ def get_data():
|
|
19 |
response = requests.get('https://api.coinex.com/v1/market/kline/BTCUSDT', params=params)
|
20 |
data = response.json()
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
df = pd.concat(df_list)
|
33 |
return df
|
@@ -39,4 +45,4 @@ if st.button("Fetch historical data"):
|
|
39 |
if df is not None:
|
40 |
# Save historical data to btcusdt_data.pkl
|
41 |
df.to_pickle('btcusdt_data.pkl')
|
42 |
-
st.write("Data fetched successfully and saved to btcusdt_data.pkl")
|
|
|
19 |
response = requests.get('https://api.coinex.com/v1/market/kline/BTCUSDT', params=params)
|
20 |
data = response.json()
|
21 |
|
22 |
+
if 'data' in data:
|
23 |
+
keys = data['data'][0].keys()
|
24 |
+
st.write(keys) # Print out the keys in the first data point
|
25 |
+
|
26 |
+
try:
|
27 |
+
df = pd.DataFrame(data['data'])
|
28 |
+
# Adjust the key based on the actual keys in the data dictionary
|
29 |
+
timestamp_key = 'time' if 'time' in keys else 'open_time'
|
30 |
+
df[timestamp_key] = pd.to_datetime(df[timestamp_key], unit='ms')
|
31 |
+
df.set_index(timestamp_key, inplace=True)
|
32 |
+
df.drop(['vol', 'amount'], axis=1, inplace=True)
|
33 |
+
df_list.append(df)
|
34 |
+
except KeyError as e:
|
35 |
+
st.error(f"KeyError: {e}. Please check the keys in the data returned by the API.")
|
36 |
+
return None
|
37 |
|
38 |
df = pd.concat(df_list)
|
39 |
return df
|
|
|
45 |
if df is not None:
|
46 |
# Save historical data to btcusdt_data.pkl
|
47 |
df.to_pickle('btcusdt_data.pkl')
|
48 |
+
st.write("Data fetched successfully and saved to btcusdt_data.pkl")
|