FinRL icon indicating copy to clipboard operation
FinRL copied to clipboard

Run Error

Open BrotherZhu opened this issue 1 year ago • 2 comments

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 from finrl.config import INDICATORS ImportError: cannot import name 'INDICATORS' from 'finrl.config' (/XXX/env10/lib/python3.10/site-packages/finrl/config/init.py)

Enviroment:

  • OS: macOS Big Sur
  • Python: 3.10.12
  • FinRL: latest

BrotherZhu avatar Aug 01 '23 07:08 BrotherZhu

Could anyone answer this question?

BrotherZhu avatar Aug 07 '23 10:08 BrotherZhu

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:

  1. 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.

  1. 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.

  1. 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!

mmmarchetti avatar Aug 24 '23 12:08 mmmarchetti