Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0125-preview· openaiHugging Face Suzhen/CodeChat

i have historical data of some cryptocurrencies containing OHLCV data Give me the proper…

  1. User

    i have historical data of some cryptocurrencies containing OHLCV data

    Give me the proper python code to calculate different ranges of rsi indicators for it (12 days,15 days,30days,...) and ad them to file as new column

  2. Assistant

    To accomplish this, you'll first need to have Python installed along with the pandas library for data manipulation and the ta library for technical analysis calculations, which includes the Relative Strength Index (RSI) calculation among many others.

    If you haven't installed these libraries yet, you can do so using pip:

    pip install pandas ta
    

    Below is a Python script that demonstrates how to load historical cryptocurrency data with OHLCV (Open, High, Low, Close, Volume) format from a file, calculate RSI for different ranges (e.g., 12 days, 15 days, 30 days), and then append these RSI values as new columns to your dataset. Finally, it saves the updated dataset to a new file. This example assumes that your data is in a CSV format.

    NOTE: You need to replace 'your_data_file.csv' with the actual path to your CSV file.

    import pandas as pd
    import ta
    
    # Load your historical cryptocurrency data
    df = pd.read_csv('your_data_file.csv')
    
    # Ensure your date column (if exists) is parsed as datetime type
    # This is important for time series analysis; replace 'date_column' with your actual column name
    # df['date_column'] = pd.to_datetime(df['date_column'])
    
    # Calculating RSI for different days and adding them as new columns
    # Replace 'close' with the name of your actual close price column, if different
    days = [12, 15, 30]  # You can add or remove the days as per your requirements
    for day in days:
        df[f'RSI_{day}'] = ta.momentum.rsi(df['Close'], window=day)
    
    # Save the updated DataFrame to a new CSV file
    df.to_csv('updated_crypto_data_with_rsi.csv', index=False)  # Set index=False to avoid writing row indices
    

    Explanation:

    • pandas is used for data manipulation, like reading from CSV files and adding new columns.
    • ta (Technical Analysis library) is used for computing the RSI easily with its built-in rsi function.
    • The days list represents different RSI periods you're interested in. You can customize this list based on your needs (e.g., add 45, 60 days).
    • df[f'RSI_{day}'] dynamically creates new column names based on the RSI period and assigns the computed RSI values to these columns.
    • Finally, the updated DataFrame is saved back to a new CSV file, preserving all your original data along with the newly computed RSI columns.

    Remember to adapt column references like 'Close' to match the actual column names of your dataset.