File size: 1,974 Bytes
963d0f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e17b99
963d0f9
 
 
 
 
 
 
5110da1
963d0f9
 
 
9e17b99
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
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 = {
            'symbol': 'BTCUSDT',
            'interval': interval,
            'limit': limit,
            'startTime': start_time + (i * 60 * 60 * 1000)
        }
        response = requests.get('https://api.binance.com/api/v3/klines', params=params)
        data = response.json()

        if data:  # Check if data is present
            try:
                df = pd.DataFrame(data, columns=['open_time', 'open', 'high', 'low', 'close', 'volume', 'close_time', 'quote_asset_volume', 'number_of_trades', 'taker_buy_base_asset_volume', 'taker_buy_quote_asset_volume', 'ignore'])
                timestamp_key = 'open_time'  # Adjust this based on the data structure
                df[timestamp_key] = pd.to_datetime(df[timestamp_key], unit='ms')
                df.set_index(timestamp_key, inplace=True)
                df.drop(['volume', 'close_time', 'quote_asset_volume', 'number_of_trades', 'taker_buy_base_asset_volume', 'taker_buy_quote_asset_volume', 'ignore'], 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, ignore_index=True)
    return df

if st.button("Fetch historical data"):
    # Retrieve historical data from Binance API
    df = get_data()

    if df is not None:
        st.write(df)
        # Save historical data to btcusdt_data.pkl
        df.to_pickle('btcusdt_data.pkl')
        st.write("Data fetched successfully and saved to btcusdt_data.pkl")