[Python] I am getting issue with date it is showing same date for the data downloaded in csv...

Discussão em 'Python' iniciado por Stack, Fevereiro 10, 2025.

  1. Stack

    Stack Membro Participativo

    import pandas as pd
    from datetime import datetime,timezone
    import httpx
    import pandas_ta as ta

    class BinanceDataDownloader:
    Base_url = "https://api2.binance.com"

    def download(self, symbol: str, timeframe: str, start_date, end_date, save_to_csv=False, csv_filename="binance_data.csv")-> pd.DataFrame:
    method = "GET"
    end_point = "/api/v3/klines"
    start_timestamp = int(datetime.strptime(start_date, "%Y-%m-%d").replace(tzinfo=timezone.utc).timestamp() * 1000)
    end_timestamp = int(datetime.strptime(end_date, "%Y-%m-%d").replace(tzinfo=timezone.utc).timestamp() * 1000)
    Parameters = {
    "symbol" : symbol,
    "interval" : timeframe,
    "startTime": start_timestamp,
    "endTime" : end_timestamp,
    "limit": 1000
    }
    url = self.Base_url + end_point
    response = httpx.request(method, url, params=Parameters)
    # print(response)
    # print(response.json())
    candle_data = []
    if response.status_code == 200:
    data = response.json()
    for candle in data:
    timestamp = datetime.fromtimestamp(candle[0] / 1000, tz=timezone.utc)
    open_price = float(candle[1])
    high_price = float(candle[2])
    low_price = float(candle[3])
    close_price = float(candle[4])
    volume = float(candle[5])
    candle_data.append([timestamp, open_price, high_price, low_price, close_price, volume,])
    Data = pd.DataFrame(candle_data,columns=["Timestamp", "Open", "High", "Low", "Close", "Volume"])
    Data.set_index('Timestamp', inplace=True)
    if save_to_csv:
    Data.to_csv(csv_filename)
    print(f"Data saved to {csv_filename}")
    return Data
    else:
    print("error")
    return pd.DataFrame()

    if __name__ == "__main__":
    a = BinanceDataDownloader()
    df = a.download("BTCUSDT","1m","2024-01-01", "2024-12-31", save_to_csv=True, csv_filename="BTCUSDT_1m_data_2024.csv")
    # print(df)
    df["ema"] = ta.ema(df["Close"],9)
    print(df)

    Continue reading...

Compartilhe esta Página