From 0192f28bf41e3b5487b51c74fad3aaee4104949e Mon Sep 17 00:00:00 2001 From: bxdd Date: Fri, 5 Feb 2021 05:01:00 +0000 Subject: [PATCH] add docstring & fix code --- examples/highfreq/highfreq_ops.py | 118 +++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 3 deletions(-) diff --git a/examples/highfreq/highfreq_ops.py b/examples/highfreq/highfreq_ops.py index 8ce391de6..461c34e6f 100644 --- a/examples/highfreq/highfreq_ops.py +++ b/examples/highfreq/highfreq_ops.py @@ -8,6 +8,20 @@ from qlib.data.data import Cal def get_calendar_day(freq="day", future=False): + """Load High-Freq Calendar Date Using Memcache. + + Parameters + ---------- + freq : str + frequency of read calendar file. + future : bool + whether including future trading day. + + Returns + ------- + _calendar: + array of date. + """ flag = f"{freq}_future_{future}_day" if flag in H["c"]: _calendar = H["c"][flag] @@ -18,6 +32,19 @@ def get_calendar_day(freq="day", future=False): class DayLast(ElemOperator): + """DayLast Operator + + Parameters + ---------- + feature : Expression + feature instance + + Returns + ---------- + feature: + a series of that each value equals the last value of its day + """ + def _load_internal(self, instrument, start_index, end_index, freq): _calendar = get_calendar_day(freq=freq) series = self.feature.load(instrument, start_index, end_index, freq) @@ -25,18 +52,57 @@ class DayLast(ElemOperator): class FFillNan(ElemOperator): + """FFillNan Operator + + Parameters + ---------- + feature : Expression + feature instance + + Returns + ---------- + feature: + a forward fill nan feature + """ + def _load_internal(self, instrument, start_index, end_index, freq): series = self.feature.load(instrument, start_index, end_index, freq) return series.fillna(method="ffill") class BFillNan(ElemOperator): + """BFillNan Operator + + Parameters + ---------- + feature : Expression + feature instance + + Returns + ---------- + feature: + a backfoward fill nan feature + """ + def _load_internal(self, instrument, start_index, end_index, freq): series = self.feature.load(instrument, start_index, end_index, freq) return series.fillna(method="bfill") class Date(ElemOperator): + """Date Operator + + Parameters + ---------- + feature : Expression + feature instance + + Returns + ---------- + feature: + a series of that each value is the date corresponding to feature.index + """ + def _load_internal(self, instrument, start_index, end_index, freq): _calendar = get_calendar_day(freq=freq) series = self.feature.load(instrument, start_index, end_index, freq) @@ -44,6 +110,22 @@ class Date(ElemOperator): class Select(PairOperator): + """Select Operator + + Parameters + ---------- + feature_left : Expression + feature instance, select condition + feature_right : Expression + feature instance, select value + + Returns + ---------- + feature: + value(feature_right) that meets the condition(feature_left) + + """ + def _load_internal(self, instrument, start_index, end_index, freq): series_condition = self.feature_left.load(instrument, start_index, end_index, freq) series_feature = self.feature_right.load(instrument, start_index, end_index, freq) @@ -51,16 +133,46 @@ class Select(PairOperator): class IsNull(ElemOperator): + """IsNull Operator + + Parameters + ---------- + feature : Expression + feature instance + + Returns + ---------- + feature: + A series indicating whether the feature is nan + """ + def _load_internal(self, instrument, start_index, end_index, freq): series = self.feature.load(instrument, start_index, end_index, freq) return series.isnull() class Cut(ElemOperator): + """Cut Operator + + Parameters + ---------- + feature : Expression + feature instance + l : int + l > 0, delete the first l elements of feature (default is None, which means 0) + r : int + r < 0, delete the last -r elements of feature (default is None, which means 0) + Returns + ---------- + feature: + A series with the first l and last -r elements deleted from the feature. + Note: It is deleted from the raw data, not the sliced data + """ + def __init__(self, feature, l=None, r=None): self.l = l self.r = r - if (self.l != None and self.l <= 0) or (self.r != None and self.r >= 0): + if (not self.l is None and self.l <= 0) or (not self.r is None and self.r >= 0): raise ValueError("Cut operator l shoud > 0 and r should < 0") super(Cut, self).__init__(feature) @@ -70,8 +182,8 @@ class Cut(ElemOperator): return series.iloc[self.l : self.r] def get_extended_window_size(self): - ll = 0 if self.l == None else self.l - rr = 0 if self.r == None else abs(self.r) + ll = 0 if self.l is None else self.l + rr = 0 if self.r is None else abs(self.r) lft_etd, rght_etd = self.feature.get_extended_window_size() lft_etd = lft_etd + ll rght_etd = rght_etd + rr