[Python] Retrieve received/paid funding rate using Kraken API

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

  1. Stack

    Stack Membro Participativo

    I am trying to retrieve the historical funding payout using krakenex, however it seems like the url is not working. Looking at the documentation at Kraken, I don't see any API calls on their REST api where I can retrieve this.

    Current Code

    import pandas as pd
    import datetime

    k = krakenex.API(API_KEY, API_SECRET)

    def get_funding_rates(start_time=None, end_time=None):
    """Retrieves funding rate history from Kraken.

    Args:
    start_time: Optional start time (datetime object or timestamp). Defaults to 7 days ago.
    end_time: Optional end time (datetime object or timestamp). Defaults to now.

    Returns:
    pandas.DataFrame: DataFrame containing funding rate history, or None if error.
    """

    if end_time is None:
    end_time = datetime.datetime.utcnow() # Now
    if start_time is None:
    start_time = end_time - datetime.timedelta(days=7) # 7 days ago

    if isinstance(start_time, datetime.datetime):
    start_time = int(start_time.timestamp())
    if isinstance(end_time, datetime.datetime):
    end_time = int(end_time.timestamp())

    try:
    params = {"start": start_time, "end": end_time}
    result = k.query_private('Ledger', params) # Use Ledger API

    if result['error']:
    print(f"Kraken API Error: {result['error']}")
    return None

    ledger_entries = result['result']
    funding_entries = []

    for key, entry in ledger_entries.items():
    if entry['type'] == 'funding': # Filter for funding transactions
    funding_entries.append(entry)

    df = pd.DataFrame(funding_entries)

    if not df.empty:
    # Convert timestamp to datetime
    df['time'] = pd.to_datetime(df['time'], unit='s')
    df = df.set_index('time') #set time as index
    df = df.sort_index() #sort by time
    return df
    else:
    print("No funding data found for the specified period.")
    return pd.DataFrame() # Return empty DataFrame

    except Exception as e:
    print(f"An error occurred: {e}")
    return None```

    The error message ```An error occurred: 404 Client Error: Not Found for url: https://api.kraken.com/0/private/Ledg```

    Continue reading...

Compartilhe esta Página