AI4Finance-Foundation / ElegantRL

Massively Parallel Deep Reinforcement Learning. 🔥
https://ai4finance.org
Other
3.74k stars 850 forks source link

A run error in env file while processing data #49

Closed quark2019 closed 2 years ago

quark2019 commented 3 years ago

There is an error in the file: elegantrl/envs/FinRL/StockTrading.py, line 225, in function: convert_df_to_ary():

tech_items = [item[tech].values.tolist() for tech in tech_indicator_list] AttributeError: 'numpy.float64' object has no attribute 'values'

If remove '.values': --- tech_items = [item[tech].values.tolist() for tech in tech_indicator_list] +++ tech_items = [item[tech].tolist() for tech in tech_indicator_list]

then get a new error: tech_items_flatten = sum(tech_items, []) TypeError: can only concatenate list (not "float") to list

Please have a look, thanks!

base env info: OS: win10 or Ubuntu20.04 Python: 3.8, 3.9 numpy: 1.21, 1.20, 1.19. 1.18 and 1.17

Test case (pick up from elegantrl/envs/FinRL/StockTrading.py::convert_df_to_ary() ):

import pandas as pd import numpy as np

def convert_df_to_ary(df, tech_indicator_list):

tech_ary = list()
price_ary = list() 
for day in range(len(df.index.unique())): 

    item = df.loc[day]                                                                                        
    tech_items = [item[tech].values.tolist() for tech in tech_indicator_list] 
    tech_items_flatten = sum(tech_items, [])
    tech_ary.append(tech_items_flatten)
    price_ary.append(item.close)  # adjusted close price (adjcp)

price_ary = np.array(price_ary) 
tech_ary = np.array(tech_ary) 
print(f'| price_ary.shape: {price_ary.shape}, tech_ary.shape: {tech_ary.shape}') 
return price_ary, tech_ary

for test:

df = pd.read_csv('test.csv') tech_indicator_list = ['close_30_sma', 'close_60_sma']

convert_df_to_ary(df, tech_indicator_list)

Sample data:

$ head test.csv date,open,high,low,close,volume,tic,day,close_30_sma,close_60_sma,turbulence 2012-01-03,14.621429443359375,14.73214340209961,14.60714340209961,12.610315322875977,302220800,AAPL,1,11.903397591908773,12.052827676137289,0.7520241067006971 2012-01-04,14.64285659790039,14.8100004196167,14.617142677307129,12.678085327148438,260022000,AAPL,2,11.942750962575277,12.07513124148051,0.06970371670868289 2012-01-05,14.819643020629883,14.948213577270508,14.738213539123535,12.818838119506836,271269600,AAPL,3,11.992857360839844,12.090065081914267,0.3731384027883833 2012-01-06,14.991786003112793,15.098214149475098,14.972143173217773,12.952840805053711,318292800,AAPL,4,12.039764340718587,12.101365041732787,0.3256607277450752 2012-01-09,15.196429252624512,15.276785850524902,15.048213958740234,12.93229866027832,394024400,AAPL,0,12.095717589060465,12.111351569493612,0.025847654177537545 2012-01-10,15.211071014404297,15.214285850524902,15.053570747375488,12.978597640991211,258196400,AAPL,1,12.15670992533366,12.118920628229777,0.023686138936847546 2012-01-11,15.09571361541748,15.101785659790039,14.975357055664062,12.957439422607422,215084800,AAPL,2,12.20416882832845,12.119201691945394,0.024991091810340274

Yonv1943 commented 3 years ago

Thank you for your attention to us. This issue was also reported last week, and we have fixed it. (Elegant-09-09 version)

In order to make the program run faster in multi-process and GPU env, we made this modification. (update the data from numpy.ndarray to torch.tensor, ) Some codes were not completely modified, which caused this error. With your reminder, it has been resolved now, thank you

Updating the code to the latest version of the bug solved this problem.

quark2019 commented 3 years ago

@Yonv1943 Thanks for your timely reply. I pull the repo to the latest version at last weekend (09-11) and tried the demo code, then get the error above.

I pull the repo again just now (The latest commit message is 'PPO Evaluator Update' by Yonv1943 at 2021-9-12 16:54), the error doesn't fix yet.

Could you please check the issue again, has been fixed? maybe, forget to commit? The error is in elegantrl/envs/FinRL/StockTrading.py

Thanks again.

Yonv1943 commented 3 years ago

The error is in elegantrl/envs/FinRL/StockTrading.py

I will fix this error in this weekend.

quark2019 commented 3 years ago

Hi Yonv1943, I'm trying to fix some errors, here is a patch file below, FYI:


diff --git a/elegantrl/envs/FinRL/StockTrading.py b/elegantrl/envs/FinRL/StockTrading.py index 43bbb33..85d5830 100644 --- a/elegantrl/envs/FinRL/StockTrading.py +++ b/elegantrl/envs/FinRL/StockTrading.py @@ -57,20 +57,20 @@ class StockTradingEnv: return state

 def step(self, actions):

xx - actions = (actions self.max_stock).astype(int) xx + actions = np.array(actions self.max_stock).astype(int)

     for index in np.where(actions < 0)[0]:  # sell_index:
         if price[index] > 0:  # Sell only if current asset is > 0

xx - sell_num_shares = min(self.stocks[index], -actions[index]) xx + sell_num_shares = np.minimum(self.stocks[index], -actions[index]) self.stocks[index] -= sell_num_shares self.amount += price[index] sell_num_shares (1 - self.sell_cost_pct)

     for index in np.where(actions > 0)[0]:  # buy_index:
         if price[index] > 0:  # Buy only if the price is > 0 (no missing data in this particular date)

xx - buy_num_shares = min(self.amount // price[index], actions[index]) xx + buy_num_shares = np.minimum(self.amount // price[index], actions[index])

@@ -218,8 +218,8 @@ class StockTradingEnv: def convert_df_to_ary(df, tech_indicator_list):

xx - for day in range(len(df.index.unique())): xx - item = df.loc[day] xx + for day in df.date.unique(): xx + item = df.loc[df.date == day]