-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
General handler for open source data preprocessing #1302
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9174dfe
feat(data): :sparkles: add a general highfreq data handler for open s…
Arthur-Null 6072eea
fix: position of parameter init
49f66dd
style(data): :lipstick: rename open to general
Arthur-Null ba483a7
style(data): :lipstick: lint
Arthur-Null 4b3cb9b
style: :lipstick: delete useless comment & fix inheritance relation
Arthur-Null e995f58
style: :lipstick: lint
Arthur-Null fef972d
style: :lipstick: remove duplicated function
Arthur-Null File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -110,6 +110,115 @@ def get_normalized_price_feature(price_field, shift=0): | |||
return fields, names | ||||
|
||||
|
||||
class HighFreqGeneralHandler(DataHandlerLP): | ||||
def __init__( | ||||
self, | ||||
instruments="csi300", | ||||
start_time=None, | ||||
end_time=None, | ||||
infer_processors=[], | ||||
learn_processors=[], | ||||
fit_start_time=None, | ||||
fit_end_time=None, | ||||
drop_raw=True, | ||||
day_length=240, | ||||
): | ||||
self.day_length = day_length | ||||
|
||||
def check_transform_proc(proc_l): | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can't this function be used here qlib/qlib/contrib/data/handler.py Line 11 in a53f59c
|
||||
new_l = [] | ||||
for p in proc_l: | ||||
p["kwargs"].update( | ||||
{ | ||||
"fit_start_time": fit_start_time, | ||||
"fit_end_time": fit_end_time, | ||||
} | ||||
) | ||||
new_l.append(p) | ||||
return new_l | ||||
|
||||
infer_processors = check_transform_proc(infer_processors) | ||||
learn_processors = check_transform_proc(learn_processors) | ||||
|
||||
data_loader = { | ||||
"class": "QlibDataLoader", | ||||
"kwargs": { | ||||
"config": self.get_feature_config(), | ||||
"swap_level": False, | ||||
"freq": "1min", | ||||
}, | ||||
} | ||||
super().__init__( | ||||
instruments=instruments, | ||||
start_time=start_time, | ||||
end_time=end_time, | ||||
data_loader=data_loader, | ||||
infer_processors=infer_processors, | ||||
learn_processors=learn_processors, | ||||
drop_raw=drop_raw, | ||||
) | ||||
|
||||
def get_feature_config(self): | ||||
fields = [] | ||||
names = [] | ||||
|
||||
template_if = "If(IsNull({1}), {0}, {1})" | ||||
template_paused = f"Cut({{0}}, {self.day_length * 2}, None)" | ||||
|
||||
def get_normalized_price_feature(price_field, shift=0): | ||||
# norm with the close price of 237th minute of yesterday. | ||||
if shift == 0: | ||||
template_norm = f"{{0}}/DayLast(Ref({{1}}, {self.day_length * 2}))" | ||||
else: | ||||
template_norm = f"Ref({{0}}, " + str(shift) + f")/DayLast(Ref({{1}}, {self.day_length}))" | ||||
|
||||
template_fillnan = "FFillNan({0})" | ||||
# calculate -> ffill -> remove paused | ||||
feature_ops = template_paused.format( | ||||
template_fillnan.format( | ||||
template_norm.format(template_if.format("$close", price_field), template_fillnan.format("$close")) | ||||
) | ||||
) | ||||
return feature_ops | ||||
|
||||
fields += [get_normalized_price_feature("$open", 0)] | ||||
fields += [get_normalized_price_feature("$high", 0)] | ||||
fields += [get_normalized_price_feature("$low", 0)] | ||||
fields += [get_normalized_price_feature("$close", 0)] | ||||
fields += [get_normalized_price_feature("$vwap", 0)] | ||||
names += ["$open", "$high", "$low", "$close", "$vwap"] | ||||
|
||||
fields += [get_normalized_price_feature("$open", self.day_length)] | ||||
fields += [get_normalized_price_feature("$high", self.day_length)] | ||||
fields += [get_normalized_price_feature("$low", self.day_length)] | ||||
fields += [get_normalized_price_feature("$close", self.day_length)] | ||||
fields += [get_normalized_price_feature("$vwap", self.day_length)] | ||||
names += ["$open_1", "$high_1", "$low_1", "$close_1", "$vwap_1"] | ||||
|
||||
# calculate and fill nan with 0 | ||||
fields += [ | ||||
template_paused.format( | ||||
"If(IsNull({0}), 0, {0})".format( | ||||
f"{{0}}/Ref(DayLast(Mean({{0}}, {self.day_length * 30})), {self.day_length})".format("$volume") | ||||
) | ||||
) | ||||
] | ||||
names += ["$volume"] | ||||
|
||||
fields += [ | ||||
template_paused.format( | ||||
"If(IsNull({0}), 0, {0})".format( | ||||
f"Ref({{0}}, {self.day_length})/Ref(DayLast(Mean({{0}}, {self.day_length * 30})), {self.day_length})".format( | ||||
"$volume" | ||||
) | ||||
) | ||||
) | ||||
] | ||||
names += ["$volume_1"] | ||||
|
||||
return fields, names | ||||
|
||||
|
||||
class HighFreqBacktestHandler(DataHandler): | ||||
def __init__( | ||||
self, | ||||
|
@@ -163,6 +272,53 @@ def get_feature_config(self): | |||
return fields, names | ||||
|
||||
|
||||
class HighFreqGeneralBacktestHandler(DataHandler): | ||||
def __init__( | ||||
self, | ||||
instruments="csi300", | ||||
start_time=None, | ||||
end_time=None, | ||||
day_length=240, | ||||
): | ||||
self.day_length = day_length | ||||
data_loader = { | ||||
"class": "QlibDataLoader", | ||||
"kwargs": { | ||||
"config": self.get_feature_config(), | ||||
"swap_level": False, | ||||
"freq": "1min", | ||||
}, | ||||
} | ||||
super().__init__( | ||||
instruments=instruments, | ||||
start_time=start_time, | ||||
end_time=end_time, | ||||
data_loader=data_loader, | ||||
) | ||||
|
||||
def get_feature_config(self): | ||||
fields = [] | ||||
names = [] | ||||
|
||||
template_paused = f"Cut({{0}}, {self.day_length * 2}, None)" | ||||
template_fillnan = "FFillNan({0})" | ||||
template_if = "If(IsNull({1}), {0}, {1})" | ||||
fields += [ | ||||
template_paused.format(template_fillnan.format("$close")), | ||||
] | ||||
names += ["$close0"] | ||||
|
||||
fields += [ | ||||
template_paused.format(template_if.format(template_fillnan.format("$close"), "$vwap")), | ||||
] | ||||
names += ["$vwap0"] | ||||
|
||||
fields += [template_paused.format("If(IsNull({0}), 0, {0})".format("$volume"))] | ||||
names += ["$volume0"] | ||||
|
||||
return fields, names | ||||
|
||||
|
||||
class HighFreqOrderHandler(DataHandlerLP): | ||||
def __init__( | ||||
self, | ||||
|
@@ -356,7 +512,6 @@ def get_feature_config(self): | |||
|
||||
template_if = "If(IsNull({1}), {0}, {1})" | ||||
template_paused = "Select(Gt($hx_paused_num, 1.001), {0})" | ||||
# template_paused = "{0}" | ||||
template_fillnan = "FFillNan({0})" | ||||
fields += [ | ||||
template_fillnan.format(template_paused.format("$close")), | ||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function duplicates with the previous one in this file. Pleases shared them