Files
NexQuant/rdagent/scenarios/qlib/experiment/workspace.py
T
Yuante Li d6ce70b551 feat: add RD-Agent-Quant scenario (#838)
* fix model input shape bug and costeer_model bug

* fix a bug

* fix a bug in docker result extraction

* a system-level optimization

* add a filter of stdout

* update

* add stdout to model

* model training_hyperparameters update

* quant scenario

* update some quant settings

* llm choose action

* Thompson Sampling Bandit for action choosing

* refine both scens

* add trace messages for quant scen

* fix some bugs

* fix some bugs

* update

* update

* update

* fix

* fix

* fix

* update for merge

* fix ci

* fix some bugs

* fix ci

* fix ci

* fix ci

* fix ci

* refactor

* default qlib4rdagent local env downloading

* fix ci

* fix ci

* fix a bug

* fix ci

* fix: align all prompts on template (#908)

* use template to render all prompts

* fix CI

---------

Co-authored-by: Xu Yang <xuyang1@microsoft.com>

* add fin_quant in cli

* fix a bug

* fix ci

* fix some bugs

* refactor

* remove the columns in hypothesis if no value generated in this column

* fix a bug

* fix ci

* fix conda env

* add qlib gitignore

* remove existed qlib folder & install torch in qlib conda

* fix workspace ui in feedback

* align model config in coder and runner in docker or conda

* fix CI

* fix CI

---------

Co-authored-by: Xu Yang <peteryang@vip.qq.com>
Co-authored-by: Xu Yang <xuyang1@microsoft.com>
2025-05-29 16:16:51 +08:00

61 lines
2.4 KiB
Python

import re
from pathlib import Path
from typing import Any
import pandas as pd
from rdagent.components.coder.model_coder.conf import MODEL_COSTEER_SETTINGS
from rdagent.core.experiment import FBWorkspace
from rdagent.log import rdagent_logger as logger
from rdagent.utils.env import QlibCondaConf, QlibCondaEnv, QTDockerEnv
class QlibFBWorkspace(FBWorkspace):
def __init__(self, template_folder_path: Path, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.inject_code_from_folder(template_folder_path)
def execute(self, qlib_config_name: str = "conf.yaml", run_env: dict = {}, *args, **kwargs) -> str:
if MODEL_COSTEER_SETTINGS.env_type == "docker":
qtde = QTDockerEnv()
elif MODEL_COSTEER_SETTINGS.env_type == "conda":
qtde = QlibCondaEnv(conf=QlibCondaConf())
else:
logger.error(f"Unknown env_type: {MODEL_COSTEER_SETTINGS.env_type}")
return None, "Unknown environment type"
qtde.prepare()
# Run the Qlib backtest
execute_qlib_log = qtde.run(
local_path=str(self.workspace_path),
entry=f"qrun {qlib_config_name}",
env=run_env,
)
logger.log_object(execute_qlib_log, tag="Qlib_execute_log")
# TODO: We should handle the case when Docker times out.
execute_log = qtde.run(
local_path=str(self.workspace_path),
entry="python read_exp_res.py",
env=run_env,
)
pattern = r"(Epoch\d+: train -[0-9\.]+, valid -[0-9\.]+|best score: -[0-9\.]+ @ \d+ epoch)"
matches = re.findall(pattern, execute_qlib_log)
execute_qlib_log = "\n".join(matches)
quantitative_backtesting_chart_path = self.workspace_path / "ret.pkl"
if quantitative_backtesting_chart_path.exists():
ret_df = pd.read_pickle(quantitative_backtesting_chart_path)
logger.log_object(ret_df, tag="Quantitative Backtesting Chart")
else:
logger.error("No result file found.")
return None, execute_qlib_log
qlib_res_path = self.workspace_path / "qlib_res.csv"
if qlib_res_path.exists():
return pd.read_csv(qlib_res_path, index_col=0).iloc[:, 0], execute_qlib_log
else:
logger.error(f"File {qlib_res_path} does not exist.")
return None, execute_qlib_log