1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-07 13:00:58 +08:00

Update part of the docs

This commit is contained in:
Jactus
2020-11-24 23:56:16 +08:00
parent 5059bba51e
commit b4671746c2
30 changed files with 902 additions and 113 deletions

View File

@@ -65,10 +65,14 @@ def get_strategy(
topk : int (Default value: 50)
top-N stocks to buy.
margin : int or float(Default value: 0.5)
if isinstance(margin, int):
- if isinstance(margin, int):
sell_limit = margin
else:
- else:
sell_limit = pred_in_a_day.count() * margin
buffer margin, in single score_mode, continue holding stock if it is in nlargest(sell_limit)
sell_limit should be no less than topk
n_drop : int
@@ -204,10 +208,14 @@ def backtest(pred, account=1e9, shift=1, benchmark="SH000905", verbose=True, **k
topk : int (Default value: 50)
top-N stocks to buy.
margin : int or float(Default value: 0.5)
if isinstance(margin, int):
- if isinstance(margin, int):
sell_limit = margin
else:
- else:
sell_limit = pred_in_a_day.count() * margin
buffer margin, in single score_mode, continue holding stock if it is in nlargest(sell_limit)
sell_limit should be no less than topk
n_drop : int

View File

@@ -16,7 +16,7 @@ class LGBModel(ModelFT):
def __init__(self, loss="mse", **kwargs):
if loss not in {"mse", "binary"}:
raise NotImplementedError
self.params = {"objective": loss, 'verbosity': -1}
self.params = {"objective": loss, "verbosity": -1}
self.params.update(kwargs)
self.model = None

View File

@@ -137,7 +137,9 @@ class WeightStrategyBase(BaseStrategy, AdjustTimer):
self.order_generator = order_generator_cls_or_obj
def generate_target_weight_position(self, score, current, trade_date):
"""Parameter:
"""
Parameters:
---------
score : pred score for this trade date, pd.Series, index is stock_id, contain 'score' column
current : current position, use Position() class
trade_exchange : Exchange()
@@ -148,7 +150,9 @@ class WeightStrategyBase(BaseStrategy, AdjustTimer):
raise NotImplementedError()
def generate_order_list(self, score_series, current, trade_exchange, pred_date, trade_date):
"""Parameter
"""
Parameters:
----------
score_series : pd.Seires
stock_id , score
current : Position()
@@ -181,7 +185,9 @@ class WeightStrategyBase(BaseStrategy, AdjustTimer):
class TopkDropoutStrategy(BaseStrategy, ListAdjustTimer):
def __init__(self, topk, n_drop, method="bottom", risk_degree=0.95, thresh=1, hold_thresh=1, **kwargs):
"""Parameter
"""
Parameters:
-----------
topk : int
The number of stocks in the portfolio
n_drop : int
@@ -218,19 +224,21 @@ class TopkDropoutStrategy(BaseStrategy, ListAdjustTimer):
return self.risk_degree
def generate_order_list(self, score_series, current, trade_exchange, pred_date, trade_date):
"""Gnererate order list according to score_series at trade_date.
will not change current.
Parameter
score_series : pd.Seires
stock_id , score
current : Position()
current of account
trade_exchange : Exchange()
exchange
pred_date : pd.Timestamp
predict date
trade_date : pd.Timestamp
trade date
"""
Gnererate order list according to score_series at trade_date, will not change current.
Parameters:
----------
score_series : pd.Series
stock_id , score
current : Position()
current of account
trade_exchange : Exchange()
exchange
pred_date : pd.Timestamp
predict date
trade_date : pd.Timestamp
trade date
"""
if not self.is_adjust(trade_date):
return []

View File

@@ -748,7 +748,8 @@ class DiskDatasetCache(DatasetCache):
The format the cache contains 3 parts(followed by typical filename).
- index : cache/d41366901e25de3ec47297f12e2ba11d.index
- index : cache/d41366901e25de3ec47297f12e2ba11d.index
- The content of the file may be in following format(pandas.Series)
.. code-block:: python
@@ -765,7 +766,9 @@ class DiskDatasetCache(DatasetCache):
- It indicates the `end_index` of the data for `timestamp`
- meta data: cache/d41366901e25de3ec47297f12e2ba11d.meta
- data : cache/d41366901e25de3ec47297f12e2ba11d
- This is a hdf file sorted by datetime
:param cache_path: The path to store the cache

View File

@@ -152,16 +152,19 @@ class InstrumentProvider(abc.ABC):
{`market`=>base market name, `filter_pipe`=>list of filters}
example :
{'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}]}
.. code-block::
{'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}]}
"""
if filter_pipe is None:
filter_pipe = []
@@ -956,6 +959,8 @@ class BaseProvider:
disk_cache=None,
):
"""
Parameters:
-----------
disk_cache : int
whether to skip(0)/use(1)/replace(2) disk_cache

View File

@@ -40,12 +40,15 @@ class DataHandler(Serializable):
Example of the data:
The multi-index of the columns is optional.
feature label
$close $volume Ref($close, 1) Mean($close, 3) $high-$low LABEL0
datetime instrument
2010-01-04 SH600000 81.807068 17145150.0 83.737389 83.016739 2.741058 0.0032
SH600004 13.313329 11800983.0 13.313329 13.317701 0.183632 0.0042
SH600005 37.796539 12231662.0 38.258602 37.919757 0.970325 0.0289
.. code-block::
feature label
$close $volume Ref($close, 1) Mean($close, 3) $high-$low LABEL0
datetime instrument
2010-01-04 SH600000 81.807068 17145150.0 83.737389 83.016739 2.741058 0.0032
SH600004 13.313329 11800983.0 13.313329 13.317701 0.183632 0.0042
SH600005 37.796539 12231662.0 38.258602 37.919757 0.970325 0.0289
"""
@@ -107,7 +110,8 @@ class DataHandler(Serializable):
----------
enable_cache : bool
default value is false
if `enable_cache` == True
- if `enable_cache` == True:
the processed data will be saved on disk, and handler will load the cached data from the disk directly
when we call `init` next time
"""
@@ -145,16 +149,21 @@ class DataHandler(Serializable):
level : Union[str, int]
which index level to select the data
col_set : Union[str, List[str]]
if isinstance(col_set, str):
- if isinstance(col_set, str):
select a set of meaningful columns.(e.g. features, columns)
if isinstance(col_set, List[str]):
- if isinstance(col_set, List[str]):
select several sets of meaningful columns, the returned data has multiple levels
squeeze : bool
whether squeeze columns and index
Returns
-------
pd.DataFrame:
pd.DataFrame.
"""
# Fetch column first will be more friendly to SepDataFrame
df = self._fetch_df_by_col(self._data, col_set)

View File

@@ -161,7 +161,7 @@ class StaticDataLoader(DataLoader):
DataLoader that supports loading data from file or as provided.
"""
def __init__(self, config: dict, join='outer'):
def __init__(self, config: dict, join="outer"):
"""
Parameters
----------
@@ -187,8 +187,9 @@ class StaticDataLoader(DataLoader):
def _maybe_load_raw_data(self):
if self._data is not None:
return
self._data = pd.concat({
fields_group: load_dataset(path_or_obj)
for fields_group, path_or_obj in self.config.items()
}, axis=1, join=self.join)
self._data = pd.concat(
{fields_group: load_dataset(path_or_obj) for fields_group, path_or_obj in self.config.items()},
axis=1,
join=self.join,
)
self._data.sort_index(inplace=True)

View File

@@ -25,8 +25,10 @@ class Model(BaseModel):
"""
Learn model from the base model
** NOTE **: The the attribute names of learned model should **not** start with '_'. So that the model could be
dumped to disk.
.. note::
The the attribute names of learned model should `not` start with '_'. So that the model could be
dumped to disk.
Parameters
----------

View File

@@ -702,7 +702,7 @@ def load_dataset(path_or_obj):
if isinstance(path_or_obj, pd.DataFrame):
return path_or_obj
if not os.path.exists(path_or_obj):
raise ValueError(f'file {path_or_obj} doesn\'t exist')
raise ValueError(f"file {path_or_obj} doesn't exist")
_, extension = os.path.splitext(path_or_obj)
if extension == ".h5":
return pd.read_hdf(path_or_obj)

View File

@@ -162,6 +162,10 @@ class QlibRecorder:
"""
Method for listing all the recorders of experiment with given id or name.
If user doesn't provide the id or name of the experiment, this method will try to retrieve the default experiment and
list all the recorders of the default experiment. If the default experiment doesn't exist, the method will first
create the default experiment, and then create a new recorder under it.
Use case:
---------
```
@@ -382,7 +386,7 @@ class QlibRecorder:
----------
local_path : str
if provided, them save the file or directory to the artifact URI.
artifact_path=None : str
artifact_path : str
the relative path for the artifact to be stored in the URI.
"""
self.get_exp().get_recorder().save_objects(local_path, artifact_path, **kwargs)

View File

@@ -12,7 +12,7 @@ logger = get_module_logger("workflow", "INFO")
class Experiment:
"""
Thie is the `Experiment` class for each experiment being run. The API is designed similar to mlflow.
This is the `Experiment` class for each experiment being run. The API is designed similar to mlflow.
(The link: https://mlflow.org/docs/latest/python_api/mlflow.html)
"""
@@ -111,24 +111,29 @@ class Experiment:
active recorder. The `create` argument determines whether the method will automatically create a new recorder
according to user's specification if the recorder hasn't been created before
If `create` is True:
If R's running:
1) no id or name specified, return the active recorder.
2) if id or name is specified, return the specified recorder. If no such exp found,
create a new recorder with given id or name, and the recorder shoud be running.
If R's not running:
1) no id or name specified, create a new recorder.
2) if id or name is specified, return the specified experiment. If no such exp found,
create a new recorder with given id or name, and the recorder shoud be running.
Else If `create` is False:
If R's running:
1) no id or name specified, return the active recorder.
2) if id or name is specified, return the specified recorder. If no such exp found,
raise Error.
If R's not running:
1) no id or name specified, raise Error.
2) if id or name is specified, return the specified recorder. If no such exp found,
raise Error.
* If `create` is True:
* If R's running:
* no id or name specified, return the active recorder.
* if id or name is specified, return the specified recorder. If no such exp found, create a new recorder with given id or name, and the recorder shoud be running.
* If R's not running:
* no id or name specified, create a new recorder.
* if id or name is specified, return the specified experiment. If no such exp found, create a new recorder with given id or name, and the recorder shoud be running.
* Else If `create` is False:
* If R's running:
* no id or name specified, return the active recorder.
* if id or name is specified, return the specified recorder. If no such exp found, raise Error.
* If R's not running:
* no id or name specified, raise Error.
* if id or name is specified, return the specified recorder. If no such exp found, raise Error.
Parameters
----------
@@ -147,7 +152,8 @@ class Experiment:
def list_recorders(self):
"""
List all the existing recorders of this experiment.
List all the existing recorders of this experiment. Please first get the experiment instance before calling this method.
If user want to use the method `R.list_recorders()`, please refer to the related API document in `QlibRecorder`.
Returns
-------

View File

@@ -94,26 +94,31 @@ class ExpManager:
When user specify experiment id and name, the method will try to return the specific experiment.
When user does not provide recorder id or name, the method will try to return the current active experiment.
The `create` argument determines whether the method will automatically create a new experiment according
to user's specification if the experiment hasn't been created before
to user's specification if the experiment hasn't been created before.
If `create` is True:
If R's running:
1) no id or name specified, return the active experiment.
2) if id or name is specified, return the specified experiment. If no such exp found,
create a new experiment with given id or name, and the experiment is set to be running.
If R's not running:
1) no id or name specified, create a default experiment.
2) if id or name is specified, return the specified experiment. If no such exp found,
create a new experiment with given id or name, and the experiment is set to be running.
Else If `create` is False:
If R's running:
1) no id or name specified, return the active experiment.
2) if id or name is specified, return the specified experiment. If no such exp found,
raise Error.
If R's not running:
1) no id or name specified. If the default experiment exists, return it, otherwise, raise Error.
2) if id or name is specified, return the specified experiment. If no such exp found,
raise Error.
* If `create` is True:
* If R's running:
* no id or name specified, return the active experiment.
* if id or name is specified, return the specified experiment. If no such exp found, create a new experiment with given id or name, and the experiment is set to be running.
* If R's not running:
* no id or name specified, create a default experiment.
* if id or name is specified, return the specified experiment. If no such exp found, create a new experiment with given id or name, and the experiment is set to be running.
* Else If `create` is False:
* If R's running:
* no id or name specified, return the active experiment.
* if id or name is specified, return the specified experiment. If no such exp found, raise Error.
* If R's not running:
* no id or name specified. If the default experiment exists, return it, otherwise, raise Error.
* if id or name is specified, return the specified experiment. If no such exp found, raise Error.
Parameters
----------

View File

@@ -56,7 +56,12 @@ class RecordTemp:
def load(self, name):
"""
Load the stored records.
Load the stored records. Due to the fact that some problems occured when we tried to balancing a clean API
with the Python's inheritance. This method has to be used in a rather ugly way, and we will try to fix them
in the future::
sar = SigAnaRecord(recorder)
ic = sar.load(sar.get_path("ic.pkl"))
Parameters
----------
@@ -102,7 +107,7 @@ class RecordTemp:
class SignalRecord(RecordTemp):
"""
This is the Signal Record class that generates the signal prediction.
This is the Signal Record class that generates the signal prediction. This class inherits the ``RecordTemp`` class.
"""
def __init__(self, model=None, dataset=None, recorder=None, **kwargs):
@@ -145,6 +150,9 @@ class SignalRecord(RecordTemp):
class SigAnaRecord(SignalRecord):
"""
This is the Signal Analysis Record class that generates the analysis results such as IC and IR. This class inherits the ``RecordTemp`` class.
"""
artifact_path = "sig_analysis"
@@ -196,7 +204,7 @@ class SigAnaRecord(SignalRecord):
class PortAnaRecord(SignalRecord):
"""
This is the Portfolio Analysis Record class that generates the results such as those of backtest.
This is the Portfolio Analysis Record class that generates the analysis results such as those of backtest. This class inherits the ``RecordTemp`` class.
"""
artifact_path = "portfolio_analysis"