Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add instrument context to inst_processor #959

Merged
merged 2 commits into from
Mar 11, 2022

Conversation

PalanQu
Copy link
Contributor

@PalanQu PalanQu commented Mar 10, 2022

Add instrument context to inst_processor

Description

Many indicator analysis need to use forward adjust stock price, but this price need to be calculated when load the data, so I need to write inst_processor to pre-calculate the forward adjust stock price like following, I want to read the latest adjfactor from source data, but there is no context in call.

class AdjPriceProcessor(InstProcessor):
    def __call__(self, df: pd.DataFrame, *args, **kwargs):
        # TODO: Read the latest adjfactor from source data
        return df

day_fields = ['$close']
day_names = ['close']
labels = ['Ref($close, -2)/Ref($close, -1) - 1'] # label
label_names = ['LABEL']
def feature_config():
    return {
        'feature_day': (
            day_fields,
            day_names,
        ),
        'label': (labels, label_names)
    }

data_loader = QlibDataLoader(
    config=feature_config(),
    freq={
        'label': 'day',
        'feature_day': 'day',
    },
    inst_processor={
        'feature_day': [AdjPriceProcessor()],
        'label': [],
    }
)

df = data_loader.load(instruments=['SH600519', 'SZ300750'], start_time='2020-01-01', end_time='2021-01-01')

Motivation and Context

How Has This Been Tested?

<! --- Put an x in all the boxes that apply: --->

  • [x ] Pass the test by running: pytest qlib/tests/test_all_pipeline.py under upper directory of qlib.
  • If you are adding a new feature, test on your own test scripts.

Screenshots of Test Results (if appropriate):

  1. Pipeline test:
  2. Your own tests:

Types of changes

  • Fix bugs
  • [ x] Add new feature
  • Update documentation

@@ -617,7 +617,7 @@ def inst_calculator(inst, start_time, end_time, freq, column_names, spans=None,
for _processor in inst_processors:
if _processor:
_processor_obj = init_instance_by_config(_processor, accept_types=InstProcessor)
data = _processor_obj(data)
data = _processor_obj(data, instrument=inst)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your advice! It looks more reasonable to add this parameter.

In addition, I think the interface should be changed with it
https://github.com/microsoft/qlib/blob/main/qlib/data/inst_processor.py#L8

@ghost
Copy link

ghost commented Mar 11, 2022

CLA assistant check
All CLA requirements met.

@you-n-g you-n-g merged commit d2f0beb into microsoft:main Mar 11, 2022
@you-n-g
Copy link
Collaborator

you-n-g commented Mar 11, 2022

LGTM! Thanks !

@you-n-g you-n-g added the enhancement New feature or request label Apr 24, 2022
qianyun210603 pushed a commit to qianyun210603/qlib that referenced this pull request Mar 23, 2023
* feat: add context to data loader

* refactor: add instrument to interface of InstProcessor

Co-authored-by: Jiabao Qu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants