|
import pandas as pd |
|
import requests |
|
import time |
|
import streamlit as st |
|
|
|
def get_data(): |
|
data_points = 10000 |
|
interval = '1h' |
|
limit = 1500 |
|
start_time = int(time.time() * 1000) - (data_points * 60 * 60 * 1000) |
|
df_list = [] |
|
|
|
for i in range(0, data_points, limit): |
|
params = { |
|
'interval': interval, |
|
'limit': limit, |
|
'start_time': start_time + (i * 60 * 60 * 1000) |
|
} |
|
response = requests.get('https://api.coinex.com/v1/market/kline/BTCUSDT', params=params) |
|
data = response.json() |
|
|
|
if 'data' in data and data['data']: |
|
try: |
|
df = pd.DataFrame(data['data']) |
|
timestamp_key = 'time' |
|
df[timestamp_key] = pd.to_datetime(df[timestamp_key], unit='ms') |
|
df.set_index(timestamp_key, inplace=True) |
|
df.drop(['vol', 'amount'], axis=1, inplace=True) |
|
df_list.append(df) |
|
except KeyError as e: |
|
st.error(f"KeyError: {e}. Please check the keys in the data returned by the API.") |
|
return None |
|
else: |
|
st.error("Data not found in the API response.") |
|
return None |
|
|
|
df = pd.concat(df_list) |
|
return df |
|
|
|
if st.button("Fetch historical data"): |
|
|
|
df = get_data() |
|
|
|
if df is not None: |
|
|
|
df.to_pickle('btcusdt_data.pkl') |
|
st.write("Data fetched successfully and saved to btcusdt_data.pkl") |