feat: Added QlibFactorFromReportScenario and improved the report-factor loop. (#161)

* Optimize factor hypothesis prompt

* Optimize the factor feedback prompt.

* Improve the prompts in feedback(factor).

* change some prompts

* Added QlibFactorFromReportScenario and improved the report-factor loop.

* reformat

* reformat

* reformat

* reformat
This commit is contained in:
WinstonLiyt
2024-08-05 14:12:05 +08:00
committed by GitHub
parent c1c3bad15d
commit cf8feecb46
5 changed files with 98 additions and 27 deletions
@@ -3,7 +3,9 @@ from dotenv import load_dotenv
from rdagent.log import rdagent_logger as logger
from rdagent.scenarios.qlib.developer.factor_coder import QlibFactorCoSTEER
from rdagent.scenarios.qlib.experiment.factor_experiment import QlibFactorScenario
from rdagent.scenarios.qlib.experiment.factor_from_report_experiment import (
QlibFactorFromReportScenario,
)
from rdagent.scenarios.qlib.factor_experiment_loader.pdf_loader import (
FactorExperimentLoaderFromPDFfiles,
)
@@ -12,7 +14,7 @@ assert load_dotenv()
def extract_factors_and_implement(report_file_path: str) -> None:
scenario = QlibFactorScenario()
scenario = QlibFactorFromReportScenario()
with logger.tag("extract_factors_and_implement"):
with logger.tag("load_factor_tasks"):
@@ -25,4 +27,4 @@ def extract_factors_and_implement(report_file_path: str) -> None:
if __name__ == "__main__":
extract_factors_and_implement("/home/xuyang1/workspace/report.pdf")
extract_factors_and_implement("workspace/report.pdf")
+7 -1
View File
@@ -35,11 +35,17 @@ class FactorBasePropSetting(BasePropSetting):
# 2) sub task specific:
origin_report_path: str = "data/report_origin"
local_report_path: str = "data/report"
report_result_json_file_path: str = "git_ignore_folder/report_list.json"
report_result_json_file_path: str = "git_ignore_folder/report_list_new.json"
progress_file_path: str = "git_ignore_folder/progress.pkl"
report_extract_result: str = "git_ignore_folder/hypo_exp_cache.pkl"
max_factor_per_report: int = 10000
class FactorFromReportPropSetting(FactorBasePropSetting):
# Override the scen attribute
scen: str = "rdagent.scenarios.qlib.experiment.factor_from_report_experiment.QlibFactorFromReportScenario"
FACTOR_PROP_SETTING = FactorBasePropSetting()
FACTOR_FROM_REPORT_PROP_SETTING = FactorFromReportPropSetting()
MODEL_PROP_SETTING = ModelBasePropSetting()
@@ -6,7 +6,7 @@ from typing import Any, Tuple
import fire
from jinja2 import Environment, StrictUndefined
from rdagent.app.qlib_rd_loop.conf import FACTOR_PROP_SETTING
from rdagent.app.qlib_rd_loop.conf import FACTOR_FROM_REPORT_PROP_SETTING
from rdagent.components.document_reader.document_reader import (
extract_first_page_screenshot_from_pdf,
load_and_process_pdfs_by_langchain,
@@ -20,9 +20,9 @@ from rdagent.core.scenario import Scenario
from rdagent.core.utils import import_class
from rdagent.log import rdagent_logger as logger
from rdagent.oai.llm_utils import APIBackend
from rdagent.scenarios.qlib.experiment.factor_experiment import (
QlibFactorExperiment,
QlibFactorScenario,
from rdagent.scenarios.qlib.experiment.factor_experiment import QlibFactorExperiment
from rdagent.scenarios.qlib.experiment.factor_from_report_experiment import (
QlibFactorFromReportScenario,
)
from rdagent.scenarios.qlib.factor_experiment_loader.pdf_loader import (
FactorExperimentLoaderFromPDFfiles,
@@ -62,7 +62,7 @@ def generate_hypothesis(factor_result: dict, report_content: str) -> str:
def extract_hypothesis_and_exp_from_reports(report_file_path: str) -> Tuple[QlibFactorExperiment, Hypothesis]:
scenario = QlibFactorScenario()
scenario = QlibFactorFromReportScenario()
with logger.tag("extract_factors_and_implement"):
with logger.tag("load_factor_tasks"):
@@ -104,7 +104,7 @@ class FactorReportLoop(LoopBase, metaclass=LoopMeta):
self.summarizer: HypothesisExperiment2Feedback = import_class(PROP_SETTING.summarizer)(scen)
self.trace = Trace(scen=scen)
self.judge_pdf_data_items = json.load(open(FACTOR_PROP_SETTING.report_result_json_file_path, "r"))
self.judge_pdf_data_items = json.load(open(FACTOR_FROM_REPORT_PROP_SETTING.report_result_json_file_path, "r"))
self.pdf_file_index = 0
super().__init__()
@@ -120,8 +120,8 @@ class FactorReportLoop(LoopBase, metaclass=LoopMeta):
if exp is None:
continue
exp.based_experiments = [QlibFactorExperiment(sub_tasks=[])] + [t[1] for t in self.trace.hist if t[2]]
exp.sub_workspace_list = exp.sub_workspace_list[: FACTOR_PROP_SETTING.max_factor_per_report]
exp.sub_tasks = exp.sub_tasks[: FACTOR_PROP_SETTING.max_factor_per_report]
exp.sub_workspace_list = exp.sub_workspace_list[: FACTOR_FROM_REPORT_PROP_SETTING.max_factor_per_report]
exp.sub_tasks = exp.sub_tasks[: FACTOR_FROM_REPORT_PROP_SETTING.max_factor_per_report]
logger.log_object(hypothesis, tag="hypothesis generation")
logger.log_object(exp.sub_tasks, tag="experiment generation")
return hypothesis, exp
@@ -158,7 +158,7 @@ def main(path=None, step_n=None):
"""
if path is None:
model_loop = FactorReportLoop(FACTOR_PROP_SETTING)
model_loop = FactorReportLoop(FACTOR_FROM_REPORT_PROP_SETTING)
else:
model_loop = FactorReportLoop.load(path)
model_loop.run(step_n=step_n)
@@ -43,7 +43,7 @@ class QlibFactorScenario(Scenario):
@property
def rich_style_description(self) -> str:
return """
### Qlib Factor Evolving Automatic R&D Demo
### R&D Agent-Qlib: Automated Quantitative Trading & Iterative Factor Evolution Demo
#### [Overview](#_summary)
@@ -55,20 +55,17 @@ The demo showcases the iterative process of hypothesis generation, knowledge con
- Generate and propose initial hypotheses based on data and domain knowledge.
2. **Factor Creation**
- Develop, define, and write new financial factors.
- Develop, define, and implement new financial factors.
- Test these factors to gather empirical results.
3. **Factor Validation**
- Validate the newly created factors quantitatively.
- Quantitatively validate the newly created factors.
4. **Backtesting with Qlib**
- **Dataset**: CSI300
- **Model**: LGBModel
- **Factors**: Alpha158 +
- **Data Split**:
- **Train**: 2008-01-01 to 2014-12-31
- **Valid**: 2015-01-01 to 2016-12-31
- **Test**: 2017-01-01 to 2020-08-01
| **Dataset** | **Model** | **Factors** |
|------------------|-------------|----------------|
| 📊 CSI300 | 🤖 LGBModel | 🌟 Alpha158 Plus|
5. **Feedback Analysis**
- Analyze backtest results.
@@ -80,12 +77,12 @@ The demo showcases the iterative process of hypothesis generation, knowledge con
#### [Automated R&D](#_rdloops)
- **[R (Research)](#_research)**
- Iteration of ideas and hypotheses.
- Iterative development of ideas and hypotheses.
- Continuous learning and knowledge construction.
- **[D (Development)](#_development)*
- Evolving code generation and model refinement.
- Automated implementation and testing of financial factors.
- **[D (Development)](#_development)**
- Progressive implementation and code generation of factors.
- Automated testing and validation of financial factors.
#### [Objective](#_summary)
@@ -0,0 +1,66 @@
from pathlib import Path
from rdagent.components.coder.factor_coder.factor import (
FactorExperiment,
FactorFBWorkspace,
FactorTask,
)
from rdagent.components.coder.factor_coder.utils import get_data_folder_intro
from rdagent.core.prompts import Prompts
from rdagent.core.scenario import Scenario
from rdagent.scenarios.qlib.experiment.factor_experiment import QlibFactorScenario
from rdagent.scenarios.qlib.experiment.workspace import QlibFBWorkspace
prompt_dict = Prompts(file_path=Path(__file__).parent / "prompts.yaml")
class QlibFactorFromReportScenario(QlibFactorScenario):
@property
def rich_style_description(self) -> str:
return """
### R&D Agent-Qlib: Automated Quantitative Trading & Factor Extraction from Financial Reports Demo
#### [Overview](#_summary)
This demo showcases the process of extracting factors from financial research reports, implementing these factors, and analyzing their performance through Qlib backtesting, continually expanding and refining the factor library.
#### Key Steps
1. **Hypothesis Generation**
- Generate and propose initial hypotheses based on insights from financial reports.
2. **Factor Creation**
- Develop, define, and codify new financial factors derived from the reports.
- Conduct empirical tests to evaluate these factors.
3. **Factor Validation**
- Quantitatively validate the newly created factors.
4. **Backtesting with Qlib**
| **Dataset** | **Model** | **Factors** |
|------------------|-------------|----------------|
| 📊 CSI300 | 🤖 LGBModel | 🌟 Alpha158 Plus|
5. **Feedback Analysis**
- Analyze backtest results.
- Incorporate feedback to refine and enhance the factor hypotheses.
#### [Automated R&D](#_rdloops)
- **[R (Research)](#_research)**
- Iterative development of ideas and hypotheses from financial reports.
- Continuous learning and knowledge construction.
- **[D (Development)](#_development)**
- Progressive factor extraction and code generation.
- Automated implementation and testing of financial factors.
#### [Objective](#_summary)
| Objective | Description |
|-------------------|---------------------------------------------------------------------------------------------------------|
| **Convenience** | Provide a tool for financial and quantitative practitioners or enthusiasts to quickly extract and test factors from research reports. |
| **Efficiency** | Enable rapid identification of factors from a vast number of reports that could enhance the current factor library. |
| **Research Facilitation** | Support further research by continuously expanding and refining the factor library. |
| **Innovation** | Foster innovation in financial analysis by leveraging automated R&D processes to iterate and improve financial factors. |
"""