AI4Finance-Foundation / FinRL

FinRL: Financial Reinforcement Learning. 🔥
https://ai4finance.org
MIT License
9.39k stars 2.28k forks source link

Help with: AttributeError: 'DataFrame' object has no attribute 'account_value #1078

Open mpythonreal opened 10 months ago

mpythonreal commented 10 months ago

I am getting the following error "AttributeError: 'DataFrame' object has no attribute 'account_value'" when running backtesting from https://github.com/llSourcell/ChatGPT_Trading_Bot/blob/main/tradingBot.ipynb

I read this may be an issue with with dataframe no longer support append, and have tried to edit the code using concat, but without success. Any help welcome... I'm a noob so go easy, simplify :) . Any examples including links to similar issues welcome.

backtesting

mmmarchetti commented 10 months ago

As of pandas 2.0, append (previously deprecated) was removed.

You need to use concat instead (for most applications):

df_account_value = pd.concat([df_account_value, temp], ignore_index=True)

mpythonreal commented 10 months ago

@mmmarchetti thank you. I tried and failed, i don't think i'm using the right approach for dataframe. Could anyone recommend how i could change the code? Here is current

df_trade_date = pd.DataFrame({'datadate':unique_trade_date})

df_account_value=pd.DataFrame() for i in range(rebalance_window+validation_window, len(unique_trade_date)+1,rebalance_window): temp = pd.read_csv('results/account_valuetrade{}_{}.csv'.format('ensemble',i)) df_account_value = df_account_value.append(temp,ignore_index=True) sharpe=(252*0.5)df_account_value.account_value.pct_change(1).mean()/df_account_value.account_value.pct_change(1).std() print('Sharpe Ratio: ',sharpe) df_account_value=df_account_value.join(df_trade_date[validation_window:].reset_index(drop=True))

mmmarchetti commented 10 months ago

👋 Hi @mpythonreal,

I've reviewed the code you provided and made the necessary changes to address the append method removal in the latest pandas versions. Here's the updated code:

import pandas as pd

df_trade_date = pd.DataFrame({'datadate':unique_trade_date})
df_account_value = pd.DataFrame()

for i in range(rebalance_window + validation_window, len(unique_trade_date) + 1, rebalance_window):
    temp = pd.read_csv('results/account_value_trade_{}_{}.csv'.format('ensemble', i))

    # Using concat instead of append
    df_account_value = pd.concat([df_account_value, temp], ignore_index=True)

# Ensure the 'account_value' column exists
if 'account_value' not in df_account_value.columns:
    raise ValueError("The 'account_value' column is missing in the CSV or DataFrame.")

sharpe = (252**0.5) * df_account_value.account_value.pct_change(1).mean() / df_account_value.account_value.pct_change(1).std()
print('Sharpe Ratio: ', sharpe)

df_account_value = df_account_value.join(df_trade_date[validation_window:].reset_index(drop=True))

Key changes:

  1. Used pd.concat to concatenate dataframes as append is no longer available.
  2. Added a check to ensure the account_value column exists in the dataframe.

Please replace the code in your Jupyter Notebook with the updated code above and try running it again. If you encounter any further issues or have questions, feel free to ask!

mpythonreal commented 10 months ago

@mmmarchetti much appreciated. Running nicely (and i learned something). Thank you!!