From 3bdd54308bdf97a8dcbe21d51ce7c0eea4947be8 Mon Sep 17 00:00:00 2001 From: bxdd Date: Tue, 26 Jan 2021 17:02:30 +0000 Subject: [PATCH] update some little code --- examples/high_freq/highfreq_handler.py | 24 +++++++++++++----------- examples/high_freq/highfreq_processor.py | 1 - examples/high_freq/workflow.py | 22 ++++++++++++---------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/examples/high_freq/highfreq_handler.py b/examples/high_freq/highfreq_handler.py index 09d954364..dc69396da 100644 --- a/examples/high_freq/highfreq_handler.py +++ b/examples/high_freq/highfreq_handler.py @@ -55,8 +55,8 @@ class HighFreqHandler(DataHandlerLP): names = [] template_if = "If(IsNull({1}), {0}, {1})" - #template_paused = "Select(Eq($paused, 0.0), {0})" - template_paused="{0}" + template_paused = "Select(Eq($paused, 0.0), {0})" + # template_paused="{0}" template_fillnan = "FFillNan({0})" simpson_vwap = "($open + 2*$high + 2*$low + $close)/6" fields += [ @@ -128,7 +128,7 @@ class HighFreqHandler(DataHandlerLP): fields += [ "Ref({0}, 240)/Ref(DayLast({0}), 240)".format(template_fillnan.format(template_paused.format("$close"))) ] - + fields += [ "Ref({0}, 240)/Ref(DayLast({1}), 240)".format( template_if.format( @@ -196,17 +196,19 @@ class HighFreqBacktestHandler(DataHandler): names = [] template_if = "If(Eq({1}, np.nan), {0}, {1})" - #template_paused = "Select(Eq($paused, 0.0), {0})" - template_paused="{0}" + template_paused = "Select(Eq($paused, 0.0), {0})" + # template_paused="{0}" template_fillnan = "FFillNan({0})" simpson_vwap = "($open + 2*$high + 2*$low + $close)/6" - #fields += [ + # fields += [ # template_fillnan.format(template_paused.format("$close")), - #] - fields += [template_if.format( - template_fillnan.format(template_paused.format("$close")), - template_paused.format(simpson_vwap), - )] + # ] + fields += [ + template_if.format( + template_fillnan.format(template_paused.format("$close")), + template_paused.format(simpson_vwap), + ) + ] names += ["$vwap_0"] fields += [ "If(IsNull({0}), 0, If(Or(Gt({1}, Mul(1.001, {3})), Lt({1}, Mul(0.999, {2}))), 0, {0}))".format( diff --git a/examples/high_freq/highfreq_processor.py b/examples/high_freq/highfreq_processor.py index bf4a30a5b..d71cd2e85 100644 --- a/examples/high_freq/highfreq_processor.py +++ b/examples/high_freq/highfreq_processor.py @@ -58,7 +58,6 @@ class HighFreqNorm(Processor): # print("start_call_feature_reshape") idx = df_features.index.droplevel("datetime").drop_duplicates() idx.set_names(["instrument", "datetime"], inplace=True) - print(df_values.shape) feat = df_values[:, [0, 1, 2, 3, 4, 10]].reshape(-1, 6 * 240) feat_1 = df_values[:, [5, 6, 7, 8, 9, 11]].reshape(-1, 6 * 240) df_new_features = pd.DataFrame( diff --git a/examples/high_freq/workflow.py b/examples/high_freq/workflow.py index e1736394e..c021ff099 100644 --- a/examples/high_freq/workflow.py +++ b/examples/high_freq/workflow.py @@ -38,12 +38,12 @@ if __name__ == "__main__": MARKET = "all" BENCHMARK = "SH000300" - DROP_LOAD_DATASET = False # flag wether to test [drop and load dataset] + DROP_LOAD_DATASET = False # flag wether to test [drop and load dataset] - #start_time = "2019-01-01 00:00:00" - #end_time = "2019-12-31 15:00:00" - #train_end_time = "2019-05-31 15:00:00" - #test_start_time = "2019-06-01 00:00:00" + # start_time = "2019-01-01 00:00:00" + # end_time = "2019-12-31 15:00:00" + # train_end_time = "2019-05-31 15:00:00" + # test_start_time = "2019-06-01 00:00:00" start_time = "2020-09-14 00:00:00" end_time = "2021-01-18 16:00:00" train_end_time = "2020-11-30 16:00:00" @@ -108,11 +108,12 @@ if __name__ == "__main__": }, } ##=============load the calendar for cache============= - Cal.calendar(freq="1min") - Cal.get_calendar_day(freq="1min") + # unnecessary, but may accelerate + Cal.calendar(freq="1min") # load the calendar for cache + Cal.get_calendar_day(freq="1min") # load the calendar for cache ##=============get data============= - + dataset = init_instance_by_config(task["dataset"]) xtrain, xtest = dataset.prepare(["train", "test"]) print(xtrain, xtest) @@ -124,7 +125,7 @@ if __name__ == "__main__": del xtrain, xtest del backtest_train, backtest_test - + ## example to show how to save the dataset and reload it, and how to use different data if DROP_LOAD_DATASET: ##=============dump dataset============= @@ -147,6 +148,7 @@ if __name__ == "__main__": dataset_backtest.init(init_type=DataHandlerLP.IT_LS) ##=============reinit qlib============= + ## Unless you want to modify the provider_uri and other configurations, reinit is unnecessary qlib.init( provider_uri=provider_uri, custom_ops=[DayFirst, DayLast, FFillNan, Date, Select, IsNull], @@ -158,7 +160,7 @@ if __name__ == "__main__": Cal.calendar(freq="1min") # load the calendar for cache Cal.get_calendar_day(freq="1min") # load the calendar for cache - ##=============test dataset + ##=============test dataset============= xtrain, xtest = dataset.prepare(["train", "test"]) backtest_train, backtest_test = dataset_backtest.prepare(["train", "test"])