### Qlib RL Quick Start Source: https://qlib.readthedocs.io/en/latest/component/model.html A quick start guide for implementing Reinforcement Learning strategies within Qlib. This section typically provides a minimal working example. ```python # Placeholder for RL quick start code. print('Qlib RL Quick Start guide.') ``` -------------------------------- ### Qlib Initialization Example Source: https://qlib.readthedocs.io/en/latest/component/online.html Demonstrates how to initialize Qlib with custom parameters. Ensure Qlib is installed and configured before running. ```python from qlib.config import C # Initialize Qlib with default parameters # C.set('qlib_data_path', os.path.join(os.getcwd(), 'data')) # qlib.init(config_file='path/to/your/config.yaml', market='cs') # Initialize Qlib with default parameters import qlib qlib.init() ``` -------------------------------- ### Qlib Recorder Start Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Starts a new recording session with QlibRecorder. This is typically the first step before logging any experiment data. ```python recorder.start() print('QlibRecorder started.') ``` -------------------------------- ### Clone Qlib Repository and Install Source: https://qlib.readthedocs.io/en/latest/introduction/quick.html Clone the Qlib repository from GitHub and install it using the setup script. ```bash git clone https://github.com/microsoft/qlib.git && cd qlib python setup.py install ``` -------------------------------- ### Qlib Server Initialization Example Source: https://qlib.readthedocs.io/en/latest/component/online.html Illustrates the basic setup for running Qlib in server mode. This allows for online serving of strategies and data. ```python import qlib from qlib.server.manager import OnlineManager # Initialize Qlib qlib.init() # Initialize the OnlineManager # online_manager = OnlineManager() # online_manager.start() # This is a placeholder as starting the server requires specific configuration and environment setup. print("Placeholder for Qlib server initialization.") ``` -------------------------------- ### Qlib MetaGuideModel Initialization Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Demonstrates initializing a MetaGuideModel, used to guide the meta-learning process in Qlib. It often incorporates task-specific guidance. ```python from qlib.meta.model import MetaGuideModel meta_guide_model = MetaGuideModel(input_dim=128, output_dim=1) print(f'MetaGuideModel initialized with input_dim={meta_guide_model.input_dim}') ``` -------------------------------- ### Install Qlib from Source Source: https://qlib.readthedocs.io/en/latest/start/installation.html Install Qlib from its source code. This involves cloning the repository, installing dependencies like numpy and cython, and then running the setup script. ```bash pip install numpy pip install --upgrade cython git clone https://github.com/microsoft/qlib.git && cd qlib python setup.py install ``` -------------------------------- ### Qlib Recorder Start Experiment Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Starts a specific experiment within the QlibRecorder session. This allows for distinct logging of parameters and metrics for each experiment. ```python recorder.start_exp(experiment_name='my_first_experiment') print('Experiment started.') ``` -------------------------------- ### Qlib Installation Source: https://qlib.readthedocs.io/en/latest/changelog/changelog.html Instructions for installing the Qlib library. ```bash pip install qlib ``` -------------------------------- ### Qlib Recorder Initialization Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Provides a basic example of initializing the QlibRecorder for experiment tracking and management. ```python from qlib.workflow.recorder import QlibRecorder recorder = QlibRecorder() print('QlibRecorder initialized.') ``` -------------------------------- ### Qlib Initialization Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Provides a basic example of initializing Qlib. This typically involves setting the data path and calling the init function. ```python import qlib import os # Set the path to your Qlib data directory qlib_data_path = os.path.expanduser('~/.qlib/qlib_data') qlib.init(provider_uri='{}/qlib_data'.format(qlib_data_path), region='cn') print('Qlib initialized successfully.') ``` -------------------------------- ### Qlib MetaModel Initialization Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Provides an example of initializing a general MetaModel in Qlib, which is a core component for meta-learning. ```python from qlib.meta.model import MetaModel # Assuming input_dim and output_dim are defined based on the task meta_model = MetaModel(input_dim=128, output_dim=1) print(f'MetaModel initialized with input_dim={meta_model.input_dim}') ``` -------------------------------- ### Install Qlib Dependencies and Source Source: https://qlib.readthedocs.io/en/latest/_sources/introduction/quick.rst.txt Installs necessary Python dependencies and clones the Qlib repository from GitHub to build from source. ```bash pip install numpy pip install --upgrade cython git clone https://github.com/microsoft/qlib.git && cd qlib python setup.py install ``` -------------------------------- ### Qlib Recorder Start Experiment Source: https://qlib.readthedocs.io/en/latest/changelog/changelog.html Starts a new experiment recording session. ```python from qlib.workflow.recorder import QlibRecorder recorder = QlibRecorder() # Start a new experiment with a specific name exp_name = 'my_first_experiment' recorder.start_exp(experiment_name=exp_name) print(f"Started experiment: {exp_name}") ``` -------------------------------- ### POST /QlibRecorder/start_exp Source: https://qlib.readthedocs.io/en/latest/component/rl/overall.html Starts a new experiment session within the Qlib recorder framework. ```APIDOC ## POST /QlibRecorder/start_exp ### Description Initializes a new experiment session to track research progress and results. ### Method POST ### Endpoint /QlibRecorder/start_exp ### Parameters #### Request Body - **experiment_name** (string) - Required - The name of the experiment to start. ``` -------------------------------- ### Finetune Model with qlib.workflow.R Source: https://qlib.readthedocs.io/en/latest/reference/api.html Demonstrates a typical workflow for finetuning a model using qlib.workflow.R. It involves starting an experiment to train an initial model, saving it, and then starting a new experiment to finetune the loaded model. ```python # start exp to train init model with R.start(experiment_name="init models"): model.fit(dataset) R.save_objects(init_model=model) rid = R.get_recorder().id # Finetune model based on previous trained model with R.start(experiment_name="finetune model"): recorder = R.get_recorder(recorder_id=rid, experiment_name="init models") model = recorder.load_object("init_model") model.finetune(dataset, num_boost_round=10) ``` -------------------------------- ### Install Qlib Dependencies Source: https://qlib.readthedocs.io/en/latest/introduction/quick.html Install necessary Python dependencies for Qlib before building from source. ```bash pip install numpy pip install --upgrade cython ``` -------------------------------- ### Start Qlib Recorder Experiment Source: https://qlib.readthedocs.io/en/latest/component/model.html Starts a new experiment recording session using QlibRecorder. This should be called before logging any experiment-specific data. ```python recorder.start_exp(experiment_name='my_experiment') print('Experiment started') ``` -------------------------------- ### Qlib DataLoader Example Source: https://qlib.readthedocs.io/en/latest/changelog/changelog.html Demonstrates the usage of QlibDataLoader for data retrieval. ```python from qlib.data.data_loader import QlibDataLoader # Instantiate QlibDataLoader data_loader = QlibDataLoader() # Example: Retrieve features for a specific instrument and time range # Note: This is a conceptual example. Actual usage depends on configured data sources. instrument = 'IF159901' start_time = '2020-01-01' end_time = '2020-01-31' features = ['close', 'open', 'high', 'low'] # data = data_loader.fetch(instruments=instrument, start_time=start_time, end_time=end_time, fields=features) # print(data) ``` -------------------------------- ### Verify Qlib Installation Source: https://qlib.readthedocs.io/en/latest/_sources/start/installation.rst.txt A simple Python snippet to verify that Qlib is correctly installed by checking the library version. ```python import qlib print(qlib.__version__) ``` -------------------------------- ### Start RL Training Source: https://qlib.readthedocs.io/en/latest/component/rl/quickstart.html Use this command to start the RL training process. Ensure you have a `train_config.yml` file specifying your training parameters. ```bash python -m qlib.rl.contrib.train_onpolicy.py --config_path train_config.yml ``` -------------------------------- ### Qlib Serialization Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Provides an example of using Qlib's serialization utilities to save and load Python objects. This is crucial for persisting models and configurations. ```python from qlib.utils.serialization import Serializable class MyData(Serializable): def __init__(self, value): self.value = value def get_serialization_content(self): return self.value @classmethod def from_serialization_content(cls, content): return cls(content) data_instance = MyData(42) # To serialize: data_instance.serialize('my_data.pkl') # To deserialize: loaded_data = MyData.deserialize('my_data.pkl') print('Qlib serialization example demonstrated.') ``` -------------------------------- ### Command Line Interface Examples Source: https://qlib.readthedocs.io/en/latest/advanced/task_management.html Provides examples of how to use the TaskManager module from the command line for common operations like checking task status. ```APIDOC ## Command Line Interface This class can be used as a tool from commandline. Here are several examples. You can view the help of manage module with the following commands: ```bash python -m qlib.workflow.task.manage -h # show manual of manage module CLI python -m qlib.workflow.task.manage wait -h # show manual of the wait command of manage ``` ```bash python -m qlib.workflow.task.manage -t wait python -m qlib.workflow.task.manage -t task_stat ``` ``` -------------------------------- ### Qlib Workflow Configuration Example Source: https://qlib.readthedocs.io/en/latest/component/model.html An example of a Qlib workflow configuration file in YAML format. This defines the dataset, model, and task parameters for a research workflow. ```yaml qlib_init: | from qlib_scripts.workflow_init import init_qlib init_qlib() task: | from qlib.workflow import Task from qlib.model. Prophet import Prophet from qlib.data.dataset import DatasetD task = Task( dataset=DatasetD(market='cs', freq='day'), model=Prophet(), name='prophet_task' ) task.train() task.test() ``` -------------------------------- ### Building Formulaic Alphas in Qlib Example Source: https://qlib.readthedocs.io/en/latest/component/online.html Provides an example of how to build custom formulaic alpha factors within the Qlib framework. This involves defining mathematical expressions based on financial data. ```python from qlib.data import D from qlib.strategy.portfolio import PortfolioStrategy from qlib.backtest.executor import BacktestExecutor from qlib.model.expression import Expression, ExpressionParam # Define a formulaic alpha # Example: Momentum factor (close price change over N days) alpha_expr = Expression("($close - $close.shift(10)) / $close.shift(10)") # You would then use this alpha within a Qlib strategy and backtest it. # This is a simplified example; actual implementation involves more setup. print("Placeholder for building formulaic alphas example.") ``` -------------------------------- ### Building Formulaic Alphas in Qlib Source: https://qlib.readthedocs.io/en/latest/changelog/changelog.html Example demonstrating how to build custom formulaic alphas within Qlib. ```python from qlib.expression import Expression, H, F # Define a simple formulaic alpha: (close - open) / open # alpha_expr = (H.close - H.open) / H.open # More complex example: Momentum factor # alpha_expr = F.ts_delay(H.close, 1) / H.close - 1 # To use this alpha, you would typically integrate it into a Qlib workflow # For example, by defining it in a configuration file or programmatically. print("Formulaic alpha definition example (commented out).") ``` -------------------------------- ### Install and Run Flake8 for Code Style Checking Source: https://qlib.readthedocs.io/en/latest/_sources/developer/code_standard_and_dev_guide.rst.txt Installs flake8 and runs it on the Qlib directory, ignoring specific error codes. Flake8 checks for style guide violations and potential errors in Python code. ```bash flake8 --ignore E501,F541,E402,F401,W503,E741,E266,E203,E302,E731,E262,F523,F821,F811,F841,E713,E265,W291,E712,E722,W293 qlib ``` -------------------------------- ### Qlib Recorder Get URI Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Retrieves the current storage URI for the QlibRecorder. This indicates where experiment records are being saved. ```python current_uri = recorder.get_uri() print(f'Current recorder URI: {current_uri}') ``` -------------------------------- ### Execute Daily Backtest with Qlib Source: https://qlib.readthedocs.io/en/latest/_sources/component/strategy.rst.txt Demonstrates how to initialize Qlib, configure a TopkDropoutStrategy, and perform a daily backtest followed by risk analysis. ```python from pprint import pprint import qlib import pandas as pd from qlib.contrib.evaluate import backtest_daily, risk_analysis from qlib.contrib.strategy import TopkDropoutStrategy qlib.init(provider_uri='') STRATEGY_CONFIG = { "topk": 50, "n_drop": 5, "signal": pred_score, } strategy_obj = TopkDropoutStrategy(**STRATEGY_CONFIG) report_normal, positions_normal = backtest_daily( start_time="2017-01-01", end_time="2020-08-01", strategy=strategy_obj ) analysis = { "excess_return_without_cost": risk_analysis(report_normal["return"] - report_normal["bench"]), "excess_return_with_cost": risk_analysis(report_normal["return"] - report_normal["bench"] - report_normal["cost"]) } analysis_df = pd.concat(analysis) pprint(analysis_df) ``` -------------------------------- ### LocalPITProvider.period_feature Source: https://qlib.readthedocs.io/en/latest/reference/api.html Gets historical period data series for a given instrument and field between specified start and end indices, and current time. ```APIDOC ## LocalPITProvider.period_feature ### Description get the historical periods data series between start_index and end_index ### Method period_feature ### Parameters #### Path Parameters * **instrument** (str) - * **field** (str) - * **start_index** (str) - * **end_index** (str) - * **cur_time** (str) - * **period** (str) - ### Returns ``` -------------------------------- ### Initialize Qlib Model Instance from Configuration (Python) Source: https://qlib.readthedocs.io/en/latest/_sources/component/workflow.rst.txt Demonstrates how to initialize a Qlib model, specifically LGBModel, using a dictionary of keyword arguments. This mirrors the configuration typically found in YAML files for Qlib. ```python from qlib.contrib.model.gbdt import LGBModel kwargs = { "loss": "mse" , "colsample_bytree": 0.8879, "learning_rate": 0.0421, "subsample": 0.8789, "lambda_l1": 205.6999, "lambda_l2": 580.9768, "max_depth": 8, "num_leaves": 210, "num_threads": 20, } LGBModel(kwargs) ``` -------------------------------- ### Qlib Recorder Get Recorder Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Retrieves a specific recorder instance by its ID. This allows direct access to a particular recorder's functionalities. ```python specific_recorder = recorder.get_recorder('recorder_id_xyz') print(f'Retrieved recorder: {specific_recorder}') ``` -------------------------------- ### ExpManager.start_exp Source: https://qlib.readthedocs.io/en/latest/component/recorder.html Starts a new experiment or resumes an existing one. It handles getting or creating an experiment and setting it as active. It also manages the active experiment URI. ```APIDOC ## ExpManager.start_exp ### Description Starts an experiment. This method includes first get_or_create an experiment, and then set it to be active. Maintaining _active_exp_uri is included in start_exp, remaining implementation should be included in _end_exp in subclass. ### Method start_exp ### Parameters * **experiment_id** (str) - id of the active experiment. * **experiment_name** (str) - name of the active experiment. * **recorder_id** (str) - id of the recorder to be started. * **recorder_name** (str) - name of the recorder to be started. * **uri** (str) - the current tracking URI. * **resume** (boolean) - whether to resume the experiment and recorder. ### Return type An active experiment. ``` -------------------------------- ### Qlib Recorder Get Experiment Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Retrieves a specific experiment record by its unique ID. This allows detailed inspection of a past experiment's metadata. ```python experiment_record = recorder.get_exp('some_experiment_id') print(experiment_record) ``` -------------------------------- ### Initialize QLib with Data Provider Source: https://qlib.readthedocs.io/en/latest/start/getdata.html Initializes QLib and specifies the path to the QLib data. Ensure data is downloaded and the path is correct. ```python import qlib qlib.init(provider_uri='~/.qlib/qlib_data/cn_data') ``` -------------------------------- ### Qlib Data Retrieval Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Retrieves historical data for a given instrument and field using Qlib's Data API. Specify the instrument, field, start, and end dates. ```python from qlib.data import D data = D.features(instruments='IF000', fields='close', start_time='2017-01-01', end_time='2017-01-10') print(data) ``` -------------------------------- ### Get Recorder Instance Source: https://qlib.readthedocs.io/en/latest/component/recorder.html Retrieves a recorder instance based on provided IDs or names. If multiple recorders match the query (e.g., by experiment name) and an MLflow backend is used, the recorder with the latest start time will be returned. ```python recorder = R.get_recorder(recorder_id='2e7a4efd66574fa49039e00ffaefa99d', experiment_name='test') ``` -------------------------------- ### Qlib Data Preparation Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Shows how to prepare data for Qlib, converting CSV files into Qlib's native format. This is a crucial step before using Qlib for analysis. ```python from qlib_scripts.data_handler import CSV2Bin # Define input CSV file and output bin file paths csv_path = 'path/to/your/data.csv' bin_path = 'path/to/your/data.bin' # Create a CSV2Bin object and run the conversion c2b = CSV2Bin(csv_path, bin_path) c2b.run() print('Data conversion complete.') ``` -------------------------------- ### Example Qlib Workflow Configuration with Filter Source: https://qlib.readthedocs.io/en/latest/component/data.html This snippet shows how to define and use an ExpressionDFilter within a Qlib workflow configuration file. It specifies filter parameters like rule expression, start and end times, and whether to keep filtered data. ```yaml filter: &filter filter_type: ExpressionDFilter rule_expression: "Ref($close, -2) / Ref($close, -1) > 1" filter_start_time: 2010-01-01 filter_end_time: 2010-01-07 keep: False data_handler_config: &data_handler_config start_time: 2010-01-01 end_time: 2021-01-22 fit_start_time: 2010-01-01 fit_end_time: 2015-12-31 instruments: *market filter_pipe: [*filter] ``` -------------------------------- ### Qlib Online Manager Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Illustrates the initialization and basic usage of the Online Manager in Qlib, which handles online serving functionalities. ```python from qlib.online import OnlineManager manager = OnlineManager() # manager.start() # Example of starting the manager print('OnlineManager initialized.') ``` -------------------------------- ### Install Qlib using pip Source: https://qlib.readthedocs.io/en/latest/start/installation.html Use this command to install Qlib directly from pip. Ensure you have Python 3.8 or earlier installed. ```bash pip install pyqlib ``` -------------------------------- ### Qlib MetaGuideModel Initialization Source: https://qlib.readthedocs.io/en/latest/component/online.html Shows the initialization of `MetaGuideModel`, another specialized meta-model, likely used for guiding the meta-learning process. ```python from qlib_scripts.workflow_tech.meta_model import MetaGuideModel # Initialize MetaGuideModel # meta_guide_model = MetaGuideModel(config_path='path/to/your/meta_guide_model_config.yaml') # This is a placeholder as the actual initialization requires a configuration file. print("Placeholder for MetaGuideModel initialization.") ``` -------------------------------- ### Qlib Recorder Methods Example Source: https://qlib.readthedocs.io/en/latest/component/online.html Demonstrates various methods of the QlibRecorder for managing experiments, including searching, listing, and deleting records. ```python from qlib.workflow.recorder import QlibRecorder recorder = QlibRecorder() # Search for records records = recorder.search_records(experiment_name='my_experiment') print(f"Found {len(records)} records.") # List all experiments experiments = recorder.list_experiments() print(f"Experiments: {experiments}") # List all recorders recorders = recorder.list_recorders() print(f"Recorders: {recorders}") # Get a specific experiment exp = recorder.get_exp(experiment_name='my_experiment') # Delete an experiment # recorder.delete_exp(experiment_name='my_experiment') ``` -------------------------------- ### Install Development Dependencies and Pre-commit Hooks Source: https://qlib.readthedocs.io/en/latest/developer/code_standard_and_dev_guide.html Installs Qlib and its development dependencies in editable mode, and then installs git pre-commit hooks. This ensures code is automatically formatted before committing. ```bash pip install -e .[dev] pre-commit install ``` -------------------------------- ### Command-line Interface Examples Source: https://qlib.readthedocs.io/en/latest/advanced/task_management.html Demonstrates how to use the TaskManager module from the command line to manage tasks. Use '-h' for detailed help. ```bash python -m qlib.workflow.task.manage -h # show manual of manage module CLI python -m qlib.workflow.task.manage wait -h # show manual of the wait command of manage ``` ```bash python -m qlib.workflow.task.manage -t wait python -m qlib.workflow.task.manage -t task_stat ``` -------------------------------- ### Install Qlib with Development Dependencies Source: https://qlib.readthedocs.io/en/latest/_sources/developer/code_standard_and_dev_guide.rst.txt Installs Qlib in editable mode, allowing changes to reflect immediately without reinstallation. It also installs development-related packages like pytest and sphinx. ```bash pip install -e .[dev] ``` -------------------------------- ### Qlib MetaGuideModel Initialization Source: https://qlib.readthedocs.io/en/latest/component/model.html Initializes a MetaGuideModel, which is used to guide the meta-learning process. This model often incorporates task-specific information. ```python from qlib.meta.model import MetaGuideModel meta_guide_model = MetaGuideModel(input_dim=10, output_dim=1) print(meta_guide_model) ``` -------------------------------- ### Initialize Qlib with Experiment Manager Source: https://qlib.readthedocs.io/en/latest/_sources/start/initialization.rst.txt This Python example demonstrates initializing Qlib with an experiment manager, specifically MLflow. It requires specifying the class, module path, and keyword arguments for the experiment manager, including the tracking URI. ```python import qlib from qlib.constant import REG_CN provider_uri = "~/.qlib/qlib_data/cn_data" # target_dir qlib.init(provider_uri=provider_uri, region=REG_CN, exp_manager= { "class": "MLflowExpManager", "module_path": "qlib.workflow.expm", "kwargs": { "uri": "python_execution_path/mlruns", "default_exp_name": "Experiment", } }) ``` -------------------------------- ### Qlib Format Data Example Source: https://qlib.readthedocs.io/en/latest/changelog/changelog.html Example of converting CSV data to Qlib format. ```python from qlib.data.data_handler import DataHandlerLP from qlib.data.data import D import pandas as pd # Assuming you have a CSV file named 'your_data.csv' data_path = 'your_data.csv' qlib_data_path = 'path/to/save/qlib/data' # Convert CSV to Qlib format # This is a simplified example; refer to Qlib documentation for detailed options # Example: Convert daily stock data # You would typically use a more robust script for actual data conversion # For demonstration, let's assume a DataFrame structure # df = pd.read_csv(data_path) # ... process df and save to qlib format ... # Example of loading data after conversion (conceptual) # D.instruments('csi300') # Load instruments # D.features(['close', 'open'], start_time='2017-01-01', end_time='2017-01-31') # Load features ``` -------------------------------- ### Qlib Online Strategy Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Shows how to define and use an Online Strategy within Qlib's online serving framework. Strategies dictate trading decisions in real-time. ```python from qlib.online.strategy import OnlineStrategy class MyOnlineStrategy(OnlineStrategy): def __init__(self, **kwargs): super().__init__(**kwargs) def predict(self, data): # Implement prediction logic here return super().predict(data) strategy = MyOnlineStrategy() print('OnlineStrategy defined.') ``` -------------------------------- ### Initialize Qlib Source: https://qlib.readthedocs.io/en/latest/component/model.html Initializes Qlib with specified parameters. Ensure all required parameters are correctly set before use. ```python from qlib.config import C C.set('qlib_data_path', os.path.join(os.getcwd(), 'data')) init_qlib() ``` -------------------------------- ### Install Qlib in Editable Mode for Development Source: https://qlib.readthedocs.io/en/latest/developer/code_standard_and_dev_guide.html Installs Qlib in editable mode, allowing changes to reflect directly in the environment without reinstallation. The '[dev]' option installs additional packages needed for development. ```bash pip install -e ".[dev]" ``` -------------------------------- ### Install Pre-commit Hooks for Qlib Development Source: https://qlib.readthedocs.io/en/latest/_sources/developer/code_standard_and_dev_guide.rst.txt Installs Qlib in editable mode with development dependencies and then installs git pre-commit hooks. These hooks automatically format code using black and flake8 before each commit. ```bash pip install -e .[dev] pre-commit install ``` -------------------------------- ### Qlib MetaTaskDataset Initialization Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Demonstrates initializing a MetaTaskDataset, which is used to prepare datasets for meta-learning tasks in Qlib. ```python from qlib.meta.task import MetaTaskDataset meta_dataset = MetaTaskDataset(dataset_name='example_dataset') print(f'MetaTaskDataset initialized for dataset: {meta_dataset.dataset_name}') ``` -------------------------------- ### Qlib Server Initialization Source: https://qlib.readthedocs.io/en/latest/component/model.html Initializes the Qlib server for online serving. This setup is required for real-time data and strategy execution. ```python from qlib_server.server import QlibServer server = QlibServer() server.start() ``` -------------------------------- ### Data Handler Initialization with Processors Source: https://qlib.readthedocs.io/en/latest/reference/api.html Illustrates how to initialize DataHandlerLP with different processor lists for inference and learning. Shows examples of processor configurations using class names, kwargs, or instance objects. ```python from qlib.data.dataset.handler import DataLoader, DataLoaderLP from qlib.data.dataset.processor import MinMaxNorm, DropnaFeature # Example 1: Using class names and kwargs infer_processors_1 = [ { "class": "MinMaxNorm", "kwargs": { "fit_start_time": "20080101", "fit_end_time": "20121231" } }, "DropnaFeature" ] # Example 2: Using object instances minmax_norm_processor = MinMaxNorm(fit_start_time="20080101", fit_end_time="20121231") learn_processors_2 = [minmax_norm_processor, DropnaFeature()] # Example initialization (assuming other parameters are set) # handler_lp = DataLoaderLP( # data_loader=..., # infer_processors=infer_processors_1, # learn_processors=learn_processors_2, # process_type='append', # drop_raw=False # ) ``` -------------------------------- ### Get Default Experiment Instance Source: https://qlib.readthedocs.io/en/latest/component/recorder.html Retrieves the default experiment instance. This is a shorthand for getting the experiment when no specific name is provided. ```python exp = R.get_exp() -> a default experiment. ``` -------------------------------- ### Qlib Workflow Configuration Example Source: https://qlib.readthedocs.io/en/latest/component/online.html Illustrates a basic Qlib workflow configuration file. This defines the model, dataset, and recorder settings for a research task. ```yaml import qlib from qlib.config import C # Initialize Qlib qlib.init() # Example of a configuration file for Qlib workflow # This is a conceptual representation and would typically be in a .yaml file config = { 'task': { 'model': { 'class': 'GRU', 'module_path': 'qlib.contrib.model.grudnn', 'kwargs': { 'hidden_size': 256, 'num_layers': 2 } }, 'dataset': { 'class': 'DatasetD', 'module_path': 'qlib.data.dataset.dataset', 'kwargs': { 'handler': { 'class': 'Alpha158', 'module_path': 'qlib.data.dataset.handler.alpha158', 'kwargs': { 'fields_group': 'close', 'feature_group_name': 'features' } }, 'infer_processor': { 'class': 'PosiSharpміну', 'module_path': 'qlib.data.dataset.processor' } } }, 'record': { 'experiment_name': 'example_experiment', 'recorder': { 'class': 'MLflowRecorder', 'module_path': 'qlib.workflow.recorder' } } } } # To use this config, you would typically save it to a YAML file and pass its path to qlib.workflow.init() # For example: # with open('config.yaml', 'w') as f: # yaml.dump(config, f) # qlib.workflow.init(config_file='config.yaml') ``` -------------------------------- ### Qlib MetaTask Initialization Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Shows how to initialize a MetaTask object, which is used in Qlib's meta-learning framework. It requires a task name and dataset name. ```python from qlib.meta.task import MetaTask meta_task = MetaTask(task_name='example_task', dataset_name='example_dataset') print(f'MetaTask initialized: {meta_task.task_name}') ``` -------------------------------- ### DataHandlerLP.setup_data Source: https://qlib.readthedocs.io/en/latest/component/data.html Sets up the data for initialization, particularly useful when running initialization for multiple times. It supports different initialization types and optional caching. ```APIDOC ## DataHandlerLP.setup_data ### Description Sets up the data for initialization, particularly useful when running initialization for multiple times. It supports different initialization types and optional caching. ### Method setup_data ### Parameters - **init_type** (str) - Optional - The type of initialization. Defaults to 'fit_seq'. - **enable_cache** (bool) - Optional - If true, processed data will be saved on disk for faster loading in subsequent calls. - **kwargs** - Additional keyword arguments. ### Request Example ```json { "init_type": "fit_seq", "enable_cache": false } ``` ### Response This method does not explicitly define a return value in the provided documentation, but it sets up the data handler. ``` -------------------------------- ### Qlib Initialization API Source: https://qlib.readthedocs.io/en/latest/component/online.html Details on initializing Qlib and its parameters. ```APIDOC ## Initialization API ### Description Provides details on how to initialize the Qlib library and its associated parameters. ### Method GET ### Endpoint /api/qlib/initialization ### Parameters #### Query Parameters - **config_path** (string) - Optional - Path to the Qlib configuration file. ### Response #### Success Response (200) - **status** (string) - Initialization status message. #### Response Example { "status": "Qlib initialized successfully." } ``` -------------------------------- ### Instrument Provider Filter Example Source: https://qlib.readthedocs.io/en/latest/reference/api.html Example configuration for filtering instruments based on market and dynamic rules. This configuration can be used with the InstrumentProvider. ```python { 'market': 'csi500', 'filter_pipe': [{'filter_type': 'ExpressionDFilter', 'rule_expression': '$open<40', 'filter_start_time': None, 'filter_end_time': None, 'keep': False}, {'filter_type': 'NameDFilter', 'name_rule_re': 'SH[0-9]{4}55', 'filter_start_time': None, 'filter_end_time': None}]} ``` -------------------------------- ### Perform Backtest and Risk Analysis Source: https://qlib.readthedocs.io/en/latest/component/recorder.html Example demonstrating how to perform a backtest using `TopkDropoutStrategy` and analyze the results, including excess returns with and without costs. Requires importing strategy and backtest modules from `qlib.contrib.strategy.strategy` and `qlib.contrib.evaluate`. ```python from qlib.contrib.strategy.strategy import TopkDropoutStrategy from qlib.contrib.evaluate import ( backtest as normal_backtest, risk_analysis, ) # backtest STRATEGY_CONFIG = { "topk": 50, "n_drop": 5, } BACKTEST_CONFIG = { "limit_threshold": 0.095, "account": 100000000, "benchmark": BENCHMARK, "deal_price": "close", "open_cost": 0.0005, "close_cost": 0.0015, "min_cost": 5, } strategy = TopkDropoutStrategy(**STRATEGY_CONFIG) report_normal, positions_normal = normal_backtest(pred_score, strategy=strategy, **BACKTEST_CONFIG) # analysis analysis = dict() analysis["excess_return_without_cost"] = risk_analysis(report_normal["return"] - report_normal["bench"]) analysis["excess_return_with_cost"] = risk_analysis(report_normal["return"] - report_normal["bench"] - report_normal["cost"]) analysis_df = pd.concat(analysis) # type: pd.DataFrame print(analysis_df) ``` -------------------------------- ### Install Compatible python-socketio Version Source: https://qlib.readthedocs.io/en/latest/FAQ/FAQ.html Ensure `python-socketio` versions match between Qlib and Qlib-server to avoid `BadNamespaceError`. Install a specific version using pip. ```bash pip install -U python-socketio== ``` -------------------------------- ### Rebase FeatureStorage with start and end indices Source: https://qlib.readthedocs.io/en/latest/reference/api.html Adjusts the start and end indices of a FeatureStorage. The interval is inclusive. This can be used to truncate or shift the data range. ```python feature: 3 4 4 5 5 6 >>> self.rebase(start_index=4) feature: 4 5 5 6 >>> self.rebase(start_index=3) feature: 3 np.nan 4 5 5 6 >>> self.write([3], index=3) feature: 3 3 4 5 5 6 >>> self.rebase(end_index=4) feature: 3 3 4 5 >>> self.write([6, 7, 8], index=4) feature: 3 3 4 6 5 7 6 8 >>> self.rebase(start_index=4, end_index=5) feature: 4 6 5 7 ``` -------------------------------- ### Qlib Recorder Initialization Source: https://qlib.readthedocs.io/en/latest/changelog/changelog.html Initialize the QlibRecorder to manage experiments. ```python from qlib_scripts.workflow_init import init_qlib # Initialize Qlib and set up the recorder init_qlib(config_path='your_qlib_config.yaml') from qlib.workflow.recorder import QlibRecorder # Create a recorder instance recorder = QlibRecorder() print(recorder) ``` -------------------------------- ### Qlib Online Tool Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Demonstrates the usage of an Online Tool within Qlib's online serving system. Tools provide utility functions for online operations. ```python from qlib.online.tool import OnlineTool tool = OnlineTool() # Example usage: tool.get_latest_data(...) print('OnlineTool initialized.') ``` -------------------------------- ### Query Task by ID Example Source: https://qlib.readthedocs.io/en/latest/advanced/task_management.html Example of querying a specific task in the collection using its MongoDB _id. This can raise a CursorNotFound exception if the cursor is too old. ```bash python -m qlib.workflow.task.manage -t query ‘{“_id”: “615498be837d0053acbc5d58”}’ ``` -------------------------------- ### Qlib MetaTaskModel Initialization Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Shows how to initialize a MetaTaskModel, a specialized meta-model for specific tasks within Qlib's meta-learning framework. ```python from qlib.meta.model import MetaTaskModel meta_task_model = MetaTaskModel(input_dim=128, output_dim=1) print(f'MetaTaskModel initialized with input_dim={meta_task_model.input_dim}') ``` -------------------------------- ### Qlib Workflow Configuration Example (YAML) Source: https://qlib.readthedocs.io/en/latest/_sources/component/workflow.rst.txt A sample YAML configuration file for Qlib's `qrun` command. This file defines parameters for data handling, model training (e.g., LGBModel), dataset segmentation, and recording results like signal and portfolio analysis. ```YAML qlib_init: provider_uri: "~/.qlib/qlib_data/cn_data" region: cn market: &market csi300 benchmark: &benchmark SH000300 data_handler_config: &data_handler_config start_time: 2008-01-01 end_time: 2020-08-01 fit_start_time: 2008-01-01 fit_end_time: 2014-12-31 instruments: *market port_analysis_config: &port_analysis_config strategy: class: TopkDropoutStrategy module_path: qlib.contrib.strategy.strategy kwargs: topk: 50 n_drop: 5 signal: backtest: start_time: 2017-01-01 end_time: 2020-08-01 account: 100000000 benchmark: *benchmark exchange_kwargs: limit_threshold: 0.095 deal_price: close open_cost: 0.0005 close_cost: 0.0015 min_cost: 5 task: model: class: LGBModel module_path: qlib.contrib.model.gbdt kwargs: loss: mse colsample_bytree: 0.8879 learning_rate: 0.0421 subsample: 0.8789 lambda_l1: 205.6999 lambda_l2: 580.9768 max_depth: 8 num_leaves: 210 num_threads: 20 dataset: class: DatasetH module_path: qlib.data.dataset kwargs: handler: class: Alpha158 module_path: qlib.contrib.data.handler kwargs: *data_handler_config segments: train: [2008-01-01, 2014-12-31] valid: [2015-01-01, 2016-12-31] test: [2017-01-01, 2020-08-01] record: - class: SignalRecord module_path: qlib.workflow.record_temp kwargs: {} - class: PortAnaRecord module_path: qlib.workflow.record_temp kwargs: config: *port_analysis_config ``` -------------------------------- ### Begin Task Training Source: https://qlib.readthedocs.io/en/latest/reference/api.html Starts task training by initializing a recorder and saving the task configuration. Use this to begin the training process for a specific task. ```python qlib.model.trainer.begin_task_train(_task_config : dict_, _experiment_name : str_, _recorder_name : str | None = None_) ``` -------------------------------- ### get_longest_back_rolling Source: https://qlib.readthedocs.io/en/latest/reference/api.html Get the longest length of historical data the feature has accessed. This is designed for getting the needed range of the data to calculate the features in specific range at first. ```APIDOC ## get_longest_back_rolling() ### Description Get the longest length of historical data the feature has accessed. This is designed for getting the needed range of the data to calculate the features in specific range at first. However, situations like Ref(Ref($close, -1), 1) can not be handled rightly. So this will only used for detecting the length of historical data needed. ### Returns the longest length of historical data the feature has accessed ### Return type int ``` -------------------------------- ### Install Qlib via pip Source: https://qlib.readthedocs.io/en/latest/_sources/start/installation.rst.txt The standard method to install the Qlib library using the Python package manager. This command fetches the latest stable release from PyPI. ```bash pip install pyqlib ``` -------------------------------- ### Qlib Recorder Initialization Source: https://qlib.readthedocs.io/en/latest/component/online.html Shows how to initialize the QlibRecorder for experiment management. This recorder helps log parameters, metrics, and artifacts. ```python from qlib.workflow.recorder import QlibRecorder # Initialize QlibRecorder recorder = QlibRecorder() # Start a new experiment recorder.start_exp(experiment_name='my_experiment') # Log parameters recorder.log_params({'param1': 10, 'param2': 'value'}) # Log metrics recorder.log_metrics({'metric1': 0.95, 'metric2': 0.05}) # End the experiment recorder.end_exp() ``` -------------------------------- ### Qlib Recorder Log Parameters Example Source: https://qlib.readthedocs.io/en/latest/component/model.html Logs parameters for the current experiment using QlibRecorder. This is essential for tracking experiment configurations. ```python params = {'learning_rate': 0.001, 'epochs': 50} recorder.log_params(params) print('Parameters logged.') ```