Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: __init__() got an unexpected keyword argument 'initial_amount' #518

Closed
shintafiaa opened this issue Mar 14, 2022 · 16 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@shintafiaa
Copy link

I got a bug on cell number [18]. Line code number 14 is red underlined.

here is the code which is red underlined:
e_train_gym = StockTradingEnv(df = train, **env_kwargs)

here is the error warning:
TypeError Traceback (most recent call last)
in ()
12 }
13
---> 14 e_train_gym = StockTradingEnv(df = train, **env_kwargs)

TypeError: init() got an unexpected keyword argument 'initial_amount'

How to solve this bug? thank's before.

@YangletLiu YangletLiu added the bug Something isn't working label Mar 14, 2022
@ObaidaAlhaasan
Copy link
Contributor

Hi team,

I got the same error and when I commented out the initial_amount line of code I got

TypeError                                 Traceback (most recent call last)
[<ipython-input-52-6f99a2a2ef8c>](https://localhost:8080/#) in <module>()
     12 }
     13 
---> 14 e_train_gym = StockTradingEnv(df = train, **env_kwargs)

TypeError: __init__() missing 1 required positional argument: 'initial_list'

And in the source code, I saw that the Initial_amount is commented out, is it as simple as un-comment that line? If so I would be happy to submit a PR
Commented out initial_amount line of code

@ObaidaAlhaasan
Copy link
Contributor

I see, there is a PR to revert the change that commented initial_amount
PR LINK

@NorthKoreaBestKorea
Copy link

Trying to get started and cant run the initial notebook because of this error.

@mkabjian
Copy link

mkabjian commented Mar 17, 2022

I ran into the same problem. It seems 'initial_list' is now used instead of 'initial_amount'. 'buy_cost_pct' and 'sell_cost_pct' are now lists instead of floats. I'm guessing this was done so that initial amounts and slippage could be varied by instrument.

Not sure if this the correct solution but the following code seemed to work:

Create the lists:
init_list = [1000000] * 1 + [0] * stock_dimension
buy_cost_list = sell_cost_list = [0.001] * stock_dimension

Update the environment args:
env_kwargs = { "hmax": 100, "initial_list": init_list, "buy_cost_pct": buy_cost_list, "sell_cost_pct": sell_cost_list, "state_space": state_space, "stock_dim": stock_dimension, "tech_indicator_list": config.TECHNICAL_INDICATORS_LIST, "action_space": stock_dimension, "reward_scaling": 1e-4 }

Again, I have no idea if this is the correct solution but everything seemed to run after these fixes.

@dpaiton
Copy link

dpaiton commented Mar 17, 2022

This was also discussed a while back here: #495

It was closed, but there is still a problem to be addressed.

@ytliu74
Copy link
Contributor

ytliu74 commented Mar 22, 2022

I have submit a PR which has been merged and fix this bug in tutorials/1-Introduction/FinRL_StockTrading_NeurIPS_2018.ipynb

@EvanHong99
Copy link

EvanHong99 commented Mar 24, 2022

I ran into the same problem. It seems 'initial_list' is now used instead of 'initial_amount'. 'buy_cost_pct' and 'sell_cost_pct' are now lists instead of floats. I'm guessing this was done so that initial amounts and slippage could be varied by instrument.

Not sure if this the correct solution but the following code seemed to work:

Create the lists: init_list = [1000000] * 1 + [0] * stock_dimension buy_cost_list = sell_cost_list = [0.001] * stock_dimension

Update the environment args: env_kwargs = { "hmax": 100, "initial_list": init_list, "buy_cost_pct": buy_cost_list, "sell_cost_pct": sell_cost_list, "state_space": state_space, "stock_dim": stock_dimension, "tech_indicator_list": config.TECHNICAL_INDICATORS_LIST, "action_space": stock_dimension, "reward_scaling": 1e-4 }

Again, I have no idea if this is the correct solution but everything seemed to run after these fixes.

It works! But you need to change TECHNICAL_INDICATORS_LIST into INDICATORS.

I can't fix this bug because I couldn't understand the operation in the source code.

With your help I commented the meaning of self.state:

    # for multiple stock
    # 将所有东西转换成一个list表示的state
    state = (
        [self.initial_amount] # 初始空余资金
        + self.data.close.values.tolist() #data[self.day]当天股票价格
        + self.initial_list[1:] #初始各股票持有share
        + sum(
            [
                self.data[tech].values.tolist()
                for tech in self.tech_indicator_list
            ], #当天各股票因子list
            [],
        )
    ) # append initial stocks_share to initial state, instead of all zero 

@zhumingpassional zhumingpassional self-assigned this Mar 28, 2022
@zhumingpassional
Copy link
Collaborator

zhumingpassional commented Mar 28, 2022

developpers updated the code today: initial_amount exists in args, and add the num_stock_shares, which should be initialized as [0] * stock_dimension

@PierrickLozach
Copy link

I updated the code to this:

init_list = [1000000] * 1 + [0] * stock_dimension
buy_cost_list = sell_cost_list = [0.001] * stock_dimension
num_stock_shares = [0] * stock_dimension

env_kwargs = { 
    "hmax": 100, 
    "initial_amount": init_list, 
    "num_stock_shares": num_stock_shares,
    "buy_cost_pct": buy_cost_list, 
    "sell_cost_pct": sell_cost_list, 
    "state_space": state_space, 
    "stock_dim": stock_dimension, 
    "tech_indicator_list": config.INDICATORS, 
    "action_space": stock_dimension, 
    "reward_scaling": 1e-4 
}
e_train_gym = StockTradingEnv(df = train, **env_kwargs)

It runs fine however the next step (Environment for Training) now returns this error:

ValueError                                Traceback (most recent call last)
[<ipython-input-27-575def09184a>](https://localhost:8080/#) in <module>()
----> 1 env_train, _ = e_train_gym.get_sb_env()
      2 print(type(env_train))

2 frames
[/usr/local/lib/python3.7/dist-packages/finrl/finrl_meta/env_stock_trading/env_stocktrading.py](https://localhost:8080/#) in get_sb_env(self)
    504     def get_sb_env(self):
    505         e = DummyVecEnv([lambda: self])
--> 506         obs = e.reset()
    507         return e, obs

[/usr/local/lib/python3.7/dist-packages/stable_baselines3/common/vec_env/dummy_vec_env.py](https://localhost:8080/#) in reset(self)
     60         for env_idx in range(self.num_envs):
     61             obs = self.envs[env_idx].reset()
---> 62             self._save_obs(env_idx, obs)
     63         return self._obs_from_buf()
     64 

[/usr/local/lib/python3.7/dist-packages/stable_baselines3/common/vec_env/dummy_vec_env.py](https://localhost:8080/#) in _save_obs(self, env_idx, obs)
     90         for key in self.keys:
     91             if key is None:
---> 92                 self.buf_obs[key][env_idx] = obs
     93             else:
     94                 self.buf_obs[key][env_idx] = obs[key]

ValueError: setting an array element with a sequence. The requested array would exceed the maximum number of dimension of 1.

@zhumingpassional
Copy link
Collaborator

zhumingpassional commented Mar 28, 2022

@PierrickI3 "initial_amount" should be set 1000000

@PierrickLozach
Copy link

That works. Thanks!

@zhumingpassional
Copy link
Collaborator

zhumingpassional commented Mar 29, 2022

@PierrickI3 Could you please submit a PR for this problem if you have enough time?

@PierrickLozach
Copy link

Not sure how I can do that.

Here is the final version of my Trade Data Split step which works:

buy_cost_list = sell_cost_list = [0.001] * stock_dimension
num_stock_shares = [0] * stock_dimension

env_kwargs = { 
    "hmax": 100, 
    "initial_amount": 1000000, 
    "num_stock_shares": num_stock_shares,
    "buy_cost_pct": buy_cost_list, 
    "sell_cost_pct": sell_cost_list, 
    "state_space": state_space, 
    "stock_dim": stock_dimension, 
    "tech_indicator_list": config.INDICATORS, 
    "action_space": stock_dimension, 
    "reward_scaling": 1e-4 
}
e_train_gym = StockTradingEnv(df = train, **env_kwargs)

@zhumingpassional
Copy link
Collaborator

zhumingpassional commented Mar 29, 2022

@PierrickI3 Fixed. Thanks.

@flyingsandwich1
Copy link

Not sure how I can do that.

Here is the final version of my Trade Data Split step which works:

buy_cost_list = sell_cost_list = [0.001] * stock_dimension
num_stock_shares = [0] * stock_dimension

env_kwargs = { 
    "hmax": 100, 
    "initial_amount": 1000000, 
    "num_stock_shares": num_stock_shares,
    "buy_cost_pct": buy_cost_list, 
    "sell_cost_pct": sell_cost_list, 
    "state_space": state_space, 
    "stock_dim": stock_dimension, 
    "tech_indicator_list": config.INDICATORS, 
    "action_space": stock_dimension, 
    "reward_scaling": 1e-4 
}
e_train_gym = StockTradingEnv(df = train, **env_kwargs)

Where did you put this in the code? I'm having issues where it says:

Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\Trader\MultipleStockTrader.py", line 453, in
buy_cost_list = sell_cost_list = [0.001] * stock_dim
NameError: name 'stock_dim' is not defined

Would really appreciate the help! I'm a beginner :)

@zhumingpassional
Copy link
Collaborator

@flyingsandwich1 which notebook did you run? Have you revised anything?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests