FinRL
FinRL copied to clipboard
Run Error
Run "python test.py" went error Ender FinRL-master/finrl run "python test.py" went error:
Traceback (most recent call last):
File "/Users/zhu/Project/Python/OpenAI/FinRL-master/finrl/test.py", line 3, in
Enviroment:
- OS: macOS Big Sur
- Python: 3.10.12
- FinRL: latest
Could anyone answer this question?
Hello @BrotherZhu,
It seems like you're facing a common issue that arises due to the difference between running scripts directly within a project directory versus using them as part of an installed package.
When you run the test.py
script directly from the project's root directory (i.e., FinRL-master/finrl
), the Python interpreter looks for modules in the current directory. This is why you're getting an import error for from finrl.config import INDICATORS
, as it's trying to import from the installed finrl
package rather than the local directory.
To resolve the issue, you have a couple of options:
-
Modify the Imports: As you've pointed out, you can change the imports in
test.py
from:
from finrl.config import INDICATORS
...
from finrl.config_tickers import DOW_30_TICKER
from finrl.meta.env_stock_trading.env_stocktrading import StockTradingEnv
to:
from config import INDICATORS
...
from config_tickers import DOW_30_TICKER
from meta.env_stock_trading.env_stocktrading import StockTradingEnv
This change makes sure the script is using the local directory to look for modules.
-
Set PYTHONPATH: Another approach without modifying the code is to set the
PYTHONPATH
environment variable to the root of your project. This way, Python will know to look in that directory when importing modules. You can set it using:
export PYTHONPATH=/path/to/FinRL-master
Replace /path/to/FinRL-master
with the actual path to the FinRL-master
directory on your system.
After setting the PYTHONPATH
, you should be able to run the script without modifying the imports.
- Set Variables Manually: Another approach is to manually set the configuration variables in the script: Variables from config.py, and remove the imports from the code:
DATA_SAVE_DIR = "datasets"
TRAINED_MODEL_DIR = "trained_models"
TENSORBOARD_LOG_DIR = "tensorboard_log"
RESULTS_DIR = "results"
# date format: '%Y-%m-%d'
TRAIN_START_DATE = "2014-01-06" # bug fix: set Monday right, start date set 2014-01-01 ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 1658 and the array at index 1 has size 1657
TRAIN_END_DATE = "2020-07-31"
TEST_START_DATE = "2020-08-01"
TEST_END_DATE = "2021-10-01"
TRADE_START_DATE = "2021-11-01"
TRADE_END_DATE = "2021-12-01"
# stockstats technical indicator column names
# check https://pypi.org/project/stockstats/ for different names
INDICATORS = [
"macd",
"boll_ub",
"boll_lb",
"rsi_30",
"cci_30",
"dx_30",
"close_30_sma",
"close_60_sma",
]
# Model Parameters
A2C_PARAMS = {"n_steps": 5, "ent_coef": 0.01, "learning_rate": 0.0007}
PPO_PARAMS = {
"n_steps": 2048,
"ent_coef": 0.01,
"learning_rate": 0.00025,
"batch_size": 64,
}
DDPG_PARAMS = {"batch_size": 128, "buffer_size": 50000, "learning_rate": 0.001}
TD3_PARAMS = {"batch_size": 100, "buffer_size": 1000000, "learning_rate": 0.001}
SAC_PARAMS = {
"batch_size": 64,
"buffer_size": 100000,
"learning_rate": 0.0001,
"learning_starts": 100,
"ent_coef": "auto_0.1",
}
ERL_PARAMS = {
"learning_rate": 3e-5,
"batch_size": 2048,
"gamma": 0.985,
"seed": 312,
"net_dimension": 512,
"target_step": 5000,
"eval_gap": 30,
"eval_times": 64, # bug fix:KeyError: 'eval_times' line 68, in get_model model.eval_times = model_kwargs["eval_times"]
}
While the first option might seem like a direct fix, it's worth noting that it can make the codebase inconsistent, especially if others are not facing the same issue or if the code is expected to be used as a package. The second option is more flexible and doesn't require code changes.
I hope this helps resolve your issue. Let us know if you face any more challenges!