AI4Finance-Foundation / FinRL

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

Help with ElegantRL! Reconstructing an Env for Testing after Training? #914

Open julianzero opened 1 year ago

julianzero commented 1 year ago

Hi everyone,

After going through the code carefully, with ElegantRL, I believe it is necessary to reconstruct a new env for testing/trading after training with an old env.

Training via Elegantrl demands a price array, a tech array, and an turbulence array, three arrays as inputs, while testing/trading demands an instance of env, which already includes env configs that contains the three arrays above. So I believe the three arrays should be different for training and testing.

The tutorial is incorrect! Please confirm or comment if you happen to read this post!

Thanx.

valleysprings commented 1 year ago

yeah, I have noticed that issue for quite a long time.

valleysprings commented 1 year ago

The issue here is tha there is not a strict rule about how to implement a env or a DRL agent, so it will be super confusing to know where to begin using this project. You can use SB3, I think it's correct at this moment.

Yonv1943 commented 1 year ago

I would like to fix the "tutorial is incorrect" you mentioned. Could you please put the link to the code that might be wrong in this issue?

the three arrays should be different for training and testing.

I also agree with you on this - during the training phase as well as the testing phase , env should load arrays for different time periods

So the parameters start_date=TEST_START_DATE and end_date=TEST_END_DATE are passed into the function test():

https://github.com/AI4Finance-Foundation/FinRL/blob/3db015dec04f0eb97d6af589a43bcb75adf82cfb/finrl/test.py#L91-L93

Then we fetch the data from TEST_START_DATE to TEST_END_DATE:

https://github.com/AI4Finance-Foundation/FinRL/blob/3db015dec04f0eb97d6af589a43bcb75adf82cfb/finrl/test.py#L27-L35

And we build the env_instance:

https://github.com/AI4Finance-Foundation/FinRL/blob/3db015dec04f0eb97d6af589a43bcb75adf82cfb/finrl/test.py#L37-L43

Finally, the env_instance for testing is passed into the function DRLAgent_erl.DRL_prediction()

https://github.com/AI4Finance-Foundation/FinRL/blob/3db015dec04f0eb97d6af589a43bcb75adf82cfb/finrl/test.py#L50-L58


The policy network maps state to action in:

https://github.com/AI4Finance-Foundation/FinRL/blob/3db015dec04f0eb97d6af589a43bcb75adf82cfb/finrl/agents/elegantrl/models.py#L110-L115

julianzero commented 1 year ago

env_config = { "price_array": price_array, "tech_array": tech_array, "turbulence_array": turbulence_array, "if_train": False, } env_instance = env(config=env_config)

if drl_lib == "elegantrl": from finrl.agents.elegantrl.models import DRLAgent as DRLAgent_erl episode_total_assets = DRLAgent_erl.DRL_prediction( model_name=model_name, cwd=cwd, net_dimension=net_dimension, environment=env_instance, )

The three arrays passed into training and testing/prediction functions must be different three arrays, three arrays for training and three other ones for testing or prediction... It will take quite significant effort to rewrite this part.

Good luck!