1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-10 06:20:57 +08:00

Adding ChangeInstrument op (#1005)

* add ChangeInstrument to ops

Adding Change instrument OP. This op allows one to use  features of a different instrument.

* Update __init__.py

update parse_field to accommodate ChangeInstrument

* Propose test

* Add test case and fix bug

* Update ops.py

* Update ops.py

* simplify the operator further

* implement abstract method

* fix arg bug

* clean test

Co-authored-by: Young <afe.young@gmail.com>
Co-authored-by: you-n-g <you-n-g@users.noreply.github.com>
This commit is contained in:
Chao Wang
2022-07-03 20:45:26 -04:00
committed by GitHub
parent b655f90511
commit 3db22452fb
3 changed files with 128 additions and 1 deletions

View File

@@ -32,6 +32,7 @@ except ValueError:
np.seterr(invalid="ignore")
#################### Element-Wise Operator ####################
@@ -62,6 +63,39 @@ class ElemOperator(ExpressionOps):
return self.feature.get_extended_window_size()
class ChangeInstrument(ElemOperator):
"""Change Instrument Operator
In some case, one may want to change to another instrument when calculating, for example, to
calculate beta of a stock with respect to a market index.
This would require changing the calculation of features from the stock (original instrument) to
the index (reference instrument)
Parameters
----------
instrument: new instrument for which the downstream operations should be performed upon.
i.e., SH000300 (CSI300 index), or ^GPSC (SP500 index).
feature: the feature to be calculated for the new instrument.
Returns
----------
Expression
feature operation output
"""
def __init__(self, instrument, feature):
self.instrument = instrument
self.feature = feature
def __str__(self):
return "{}('{}',{})".format(type(self).__name__, self.instrument, self.feature)
def load(self, instrument, start_index, end_index, *args):
# the first `instrument` is ignored
return super().load(self.instrument, start_index, end_index, *args)
def _load_internal(self, instrument, start_index, end_index, *args):
return self.feature.load(instrument, start_index, end_index, *args)
class NpElemOperator(ElemOperator):
"""Numpy Element-wise Operator
@@ -1535,6 +1569,7 @@ class TResample(ElemOperator):
TOpsList = [TResample]
OpsList = [
ChangeInstrument,
Rolling,
Ref,
Max,