AI4Finance-Foundation / FinRL

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

Single stock trading #31

Closed jzamalloa1 closed 3 years ago

jzamalloa1 commented 3 years ago

When running FinRL_single_stock_trading.ipynb all cells run fine until:

env_train = env_setup.create_env_training(data = train, env_class = SingleStockEnv)

I get the following error:

AttributeError Traceback (most recent call last)

in ----> 1 env_train = env_setup.create_env_training(data = train, 2 env_class = SingleStockEnv) /mnt/c/Users/XXX/Documents/PROJECT/STOCK_TRADING/FinRL-Library/finrl/env/environment.py in create_env_training(self, data, env_class, turbulence_threshold) 54 55 def create_env_training(self, data, env_class, turbulence_threshold=150): ---> 56 env_train = DummyVecEnv([lambda: env_class(df = data, 57 stock_dim = self.stock_dim, 58 hmax = self.hmax, ~/.local/lib/python3.8/site-packages/stable_baselines3/common/vec_env/dummy_vec_env.py in __init__(self, env_fns) 23 24 def __init__(self, env_fns: List[Callable[[], gym.Env]]): ---> 25 self.envs = [fn() for fn in env_fns] 26 env = self.envs[0] 27 VecEnv.__init__(self, len(env_fns), env.observation_space, env.action_space) ~/.local/lib/python3.8/site-packages/stable_baselines3/common/vec_env/dummy_vec_env.py in (.0) 23 24 def __init__(self, env_fns: List[Callable[[], gym.Env]]): ---> 25 self.envs = [fn() for fn in env_fns] 26 env = self.envs[0] 27 VecEnv.__init__(self, len(env_fns), env.observation_space, env.action_space) /mnt/c/Users/XXX/Documents/PROJECT/STOCK_TRADING/FinRL-Library/finrl/env/environment.py in () 54 55 def create_env_training(self, data, env_class, turbulence_threshold=150): ---> 56 env_train = DummyVecEnv([lambda: env_class(df = data, 57 stock_dim = self.stock_dim, 58 hmax = self.hmax, in __init__(self, df, stock_dim, hmax, initial_amount, transaction_cost_pct, reward_scaling, state_space, action_space, tech_indicator_list, turbulence_threshold, day) 100 [self.data.high] + \ 101 [self.data.low] +\ --> 102 [self.data.daily_return] 103 # initialize reward 104 self.reward = 0 ~/.local/lib/python3.8/site-packages/pandas/core/generic.py in __getattr__(self, name) 5137 if self._info_axis._can_hold_identifiers_and_holds_name(name): 5138 return self[name] -> 5139 return object.__getattribute__(self, name) 5140 5141 def __setattr__(self, name: str, value) -> None: AttributeError: 'Series' object has no attribute 'daily_return'
JinChengneng commented 3 years ago

I have met the same problem. The error log is shown below.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-22-1960ecceda46> in <module>()
      1 env_train = env_setup.create_env_training(data = train,
----> 2                                           env_class = SingleStockEnv)

/usr/local/lib/python3.6/dist-packages/finrl/env/environment.py in create_env_training(self, data, env_class, turbulence_threshold)
     54 
     55     def create_env_training(self, data, env_class, turbulence_threshold=150):
---> 56         env_train = DummyVecEnv([lambda: env_class(df = data,
     57                                                     stock_dim = self.stock_dim,
     58                                                     hmax = self.hmax,

/usr/local/lib/python3.6/dist-packages/stable_baselines3/common/vec_env/dummy_vec_env.py in __init__(self, env_fns)
     23 
     24     def __init__(self, env_fns: List[Callable[[], gym.Env]]):
---> 25         self.envs = [fn() for fn in env_fns]
     26         env = self.envs[0]
     27         VecEnv.__init__(self, len(env_fns), env.observation_space, env.action_space)

/usr/local/lib/python3.6/dist-packages/stable_baselines3/common/vec_env/dummy_vec_env.py in <listcomp>(.0)
     23 
     24     def __init__(self, env_fns: List[Callable[[], gym.Env]]):
---> 25         self.envs = [fn() for fn in env_fns]
     26         env = self.envs[0]
     27         VecEnv.__init__(self, len(env_fns), env.observation_space, env.action_space)

/usr/local/lib/python3.6/dist-packages/finrl/env/environment.py in <lambda>()
     63                                                     action_space = self.action_space,
     64                                                     tech_indicator_list = self.tech_indicator_list,
---> 65                                                     turbulence_threshold = turbulence_threshold)])
     66         return env_train
     67 

<ipython-input-17-33a3af6cb4c6> in __init__(self, df, stock_dim, hmax, initial_amount, transaction_cost_pct, reward_scaling, state_space, action_space, tech_indicator_list, turbulence_threshold, day)
     93         self.turbulence_threshold = turbulence_threshold
     94         # initalize state: inital amount + close price + shares + technical indicators + other features
---> 95         self.state = [self.initial_amount] +                       [self.data.close] +                       [0]*self.stock_dim  +                       sum([[self.data[tech]] for tech in self.tech_indicator_list ], [])+                       [self.data.open] +                       [self.data.high] +                       [self.data.low] +                      [self.data.daily_return]
     96         # initialize reward
     97         self.reward = 0

/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py in __getattr__(self, name)
   5139             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   5140                 return self[name]
-> 5141             return object.__getattribute__(self, name)
   5142 
   5143     def __setattr__(self, name: str, value) -> None:

AttributeError: 'Series' object has no attribute 'daily_return'
JinChengneng commented 3 years ago

I guess this issue occurred since the column daily_return in DataFrame data_df missed. I would appreciate it if you could check it.

BruceYanghy commented 3 years ago

I fixed it. Please try again. Please do runtime, and factory reset runtime, and run everything again.

JinChengneng commented 3 years ago

It works well now. Thanks for your effort.

zyzhang1130 commented 3 years ago

Sorry I have a relevant question regarding 'daily_return': why it cannot be found before doing feature engineering? Does that mean 'daily_return' is a technical indicator? If so why it is not included in the tech_indicator_list? Thanks

TekpreXyz commented 3 years ago

This code is incredible awesome! thank you for! some idea to try it on binance future testnet? or to implement ccxt? https://github.com/ccxt/ccxt https://binance-docs.github.io/apidocs/testnet/en/#change-log

BruceYanghy commented 3 years ago

Sorry I have a relevant question regarding 'daily_return': why it cannot be found before doing feature engineering? Does that mean 'daily_return' is a technical indicator? If so why it is not included in the tech_indicator_list? Thanks

good question, I found this 'daily_return' is actually hardcoded into the env file for single stock trading, that's not good, I will work on improvements.

BruceYanghy commented 3 years ago

This code is incredible awesome! thank you for! some idea to try it on binance future testnet? or to implement ccxt? https://github.com/ccxt/ccxt https://binance-docs.github.io/apidocs/testnet/en/#change-log

Great library. We are actually doing research papers on cryptocurrencies, if you are an expert, let's work together.

TekpreXyz commented 3 years ago

i am not an expert but i would gladly help you in some way

TekpreXyz commented 3 years ago

I'd like to implement some stuff inFinRL:

Autoregressive Integrated Moving Average: https://towardsdatascience.com/aifortrading-2edd6fac689d

Add to dataframe all the indicator of TA lib together: https://github.com/bukosabino/ta/blob/master/examples_to_use/all_features_example.py https://github.com/bukosabino/ta

Use of Ray agent for crypto https://www.tensortrade.org/en/latest/agents/overview.html#ray

Implement Tensorflow2.4

BruceYanghy commented 3 years ago

I'd like to implement some stuff inFinRL:

Autoregressive Integrated Moving Average: https://towardsdatascience.com/aifortrading-2edd6fac689d

Add to dataframe all the indicator of TA lib together: https://github.com/bukosabino/ta/blob/master/examples_to_use/all_features_example.py https://github.com/bukosabino/ta

Use of Ray agent for crypto https://www.tensortrade.org/en/latest/agents/overview.html#ray

Implement Tensorflow2.4

https://join.slack.com/t/ai4financeworkspace/shared_invite/zt-jyaottie-hHqU6TdvuhMHHAMXaLw_~w

Please join our open source community.