def start(date_now, result): if Date().is_workday(date_now): for ts_code in ['399300.SZ']: try: start_date, end_date = date_now - datetime.timedelta(days=30), date_now security_point_data = SecurityData().get_index_point_data(ts_code, start_date, end_date) buy_flag = calc_bs_data(security_point_data) if buy_flag is True: previous_work_day = Date().get_previous_workday(end_date) previous_2_work_day = Date().get_previous_workday(previous_work_day) next_0_workday = date_now next_1_workday = Date().get_next_workday(date_now) next_2_workday = Date().get_next_workday(next_1_workday) next_3_workday = Date().get_next_workday(next_2_workday) next_4_workday = Date().get_next_workday(next_3_workday) next_5_workday = Date().get_next_workday(next_4_workday) security_point_data = SecurityData().get_qfq_security_point_data(ts_code, previous_2_work_day, end_date + datetime.timedelta(days=20)) security_data = security_point_data result_now = pd.DataFrame( [security_data.loc[previous_2_work_day], security_data.loc[previous_work_day], security_data.loc[next_0_workday], security_data.loc[next_1_workday], security_data.loc[next_2_workday], security_data.loc[next_3_workday], security_data.loc[next_4_workday], security_data.loc[next_5_workday]], index=[['previous_2_day', 'previous_1_day', 'next_0_day', 'next_1_day', 'next_2_day', 'next_3_day', 'next_4_day', 'next_5_day'], [1, 1, 1, 1, 1, 1, 1, 1]]) result_now = result_now.unstack(level=0) result.append(result_now) except Exception as e: pass return result
def start(date_now=None): date_now = datetime.datetime.now() if date_now is None else date_now date_now = datetime.datetime(date_now.year, date_now.month, date_now.day) if Date().is_workday(date_now): start_date, end_date = date_now - datetime.timedelta( days=365), date_now ts_codes = MarketInfo().get_future_main_code_filter_by_manual(date_now) for ts_code in ts_codes: try: security_point_data = SecurityData( ).get_future_security_point_data(ts_code, start_date, end_date) buy_flag = buy(security_point_data) if buy_flag is True: Result().insert_strategy_result_data( ts_code, "future_bs_when_trend_start", "B", date_now) except Exception as e: pass try: security_point_data = SecurityData( ).get_future_security_point_data(ts_code, start_date, end_date) buy_flag = sell(security_point_data) if buy_flag is True: Result().insert_strategy_result_data( ts_code, "future_bs_when_trend_start", "S", date_now) except Exception as e: pass
def start(date_now=None): date_now = datetime.datetime.now() if date_now is None else date_now date_now = datetime.datetime(date_now.year, date_now.month, date_now.day) if Date().is_workday(date_now): start_date, end_date = date_now - datetime.timedelta(days=50), date_now # ts_codes = Security().get_efficient_ts_code_by_column_name_list(["normal_status", "tactics_5_status"]) # for ts_code in ts_codes: for ts_code in [ '600481.SH', '601699.SH', '002061.SZ', '002424.SZ', '600436.SH', '002796.SZ', '600810.SH', '000510.SZ', '601600.SH', '600218.SH', '002414.SZ', '002302.SZ', '000877.SZ', '002451.SZ', '600355.SH', '600760.SH', '600679.SH', '000656.SZ', '600148.SH', '000935.SZ', '002208.SZ', '600516.SH', '002602.SZ', '002696.SZ', '002050.SZ', '000557.SZ', '002460.SZ', '600230.SH', '002877.SZ', '002243.SZ', '600235.SH', '000403.SZ', '000576.SZ', '600997.SH', '600581.SH', '600690.SH', '600507.SH', '600019.SH', '000513.SZ', '002346.SZ', '600966.SH', '000612.SZ', '000966.SZ', '600438.SH', '002382.SZ', '600388.SH', '603993.SH', '601636.SH', '000898.SZ', '601028.SH', '000739.SZ', '002258.SZ', '002760.SZ', '603288.SH', '600782.SH', '600720.SH', '002708.SZ', '603027.SH', '000785.SZ', '600141.SH', '000001.SZ', '600783.SH', '600519.SH', '002072.SZ', '600280.SH', '600874.SH', '603859.SH', '601003.SH', '601918.SH', '000732.SZ', '600083.SH', '601933.SH', '600425.SH', '002034.SZ', '600346.SH', '000528.SZ', '601360.SH', '002679.SZ', '601318.SH', '002124.SZ', '603444.SH', '002606.SZ', '002157.SZ', '600596.SH', '600761.SH', '002408.SZ', '600815.SH', '603180.SH', '000725.SZ', '600641.SH', '000830.SZ', '600128.SH', '002056.SZ', '600196.SH', '600104.SH', '600080.SH', '002545.SZ', '601012.SH', '600776.SH', '000717.SZ', '000690.SZ', '600053.SH', '601100.SH', '002415.SZ' ]: try: security_point_data = SecurityData().get_security_point_data( ts_code, start_date, end_date) buy_flag = buy(security_point_data) if buy_flag is True: Result().insert_strategy_result_data( ts_code, "sma_sloop", "B", date_now) except Exception as e: pass ts_codes = Result().get_hold_data('sma_sloop') for ts_code in ts_codes: try: security_point_data = SecurityData().get_security_point_data( ts_code, start_date, end_date) sell_flag = sell(security_point_data) if sell_flag is True: Result().insert_strategy_result_data( ts_code, "sma_sloop", "S", date_now) except Exception as e: pass
def start(): security_data = SecurityData().get_index_point_data( '399300.SZ', datetime.datetime(2016, 5, 3), datetime.datetime.now()) hs300(security_data) security_data = SecurityData().get_security_point_data( '600030.SH', datetime.datetime(2016, 5, 3), datetime.datetime.now()) zx_security(security_data)
def start(all_ts_code, date): """失败 准确率太低""" for ts_code in all_ts_code: print(ts_code) qfq_security_point_data = SecurityData().get_qfq_security_point_data(ts_code, datetime.datetime(2016, 5, 4), datetime.datetime(2019, 1, 31)) qfq_security_point_data.sort_index(ascending=False, inplace=True) qfq_security_point_data.index.rename("date", inplace=True) security_daily_basic = SecurityData().get_security_daily_basic_data(ts_code, datetime.datetime(2016, 5, 4), datetime.datetime(2019, 1, 31)) security_daily_basic.sort_index(ascending=False, inplace=True) security_daily_basic.index.rename("date", inplace=True) target_name_list = ["next_2_day", "next_3_day", "next_4_day", "next_5_day", "next_6_day", "next_7_day", "next_8_day", "next_9_day", "next_10_day"] target_data_list = [] for window_num in range(3, 12): target_data_now = qfq_security_point_data["close"].rolling(window=window_num).apply(lambda x: x[0] > x[-1], raw=True) target_data_now = target_data_now.iloc[12:] target_data_list.append(target_data_now.values) percent_series = qfq_security_point_data["open"] / 100 data = pd.DataFrame({ "open": qfq_security_point_data["open"] / percent_series, "close": qfq_security_point_data["close"] / percent_series, "high": qfq_security_point_data["high"] / percent_series, "low": qfq_security_point_data["low"] / percent_series, "turnover_rate_f": security_daily_basic["turnover_rate_f"] }, index=qfq_security_point_data["trade_date"]) data = data.iloc[12:] data = data.fillna(0) data = data.values for day, target_data in zip(target_name_list, target_data_list): print("day is {0}".format(day)) depth_list = [] training_score_list = [] test_score_list = [] for max_depth in range(1, 31): X_train, X_test, y_train, y_test = train_test_split(data, target_data, random_state=0) forest = RandomForestClassifier(n_estimators=100, random_state=0, max_depth=max_depth) forest.fit(X_train, y_train) print("Accuracy on training set: {:.3f}".format(forest.score(X_train, y_train))) print("Accuracy on test set: {:.3f}".format(forest.score(X_test, y_test))) if forest.score(X_test, y_test) > 0.7: training_score_list.append(forest.score(X_train, y_train)) test_score_list.append(forest.score(X_test, y_test)) depth_list.append(max_depth) if test_score_list: # plt.figure(figsize=(22, 15)) plt.figure(figsize=(6, 6)) plt.locator_params(nbins=30, axis='x') plt.plot(depth_list, training_score_list, ls="-", lw=2, label="training figure") plt.plot(depth_list, test_score_list, ls="-", lw=2, label="test figure") # ls: 折线图线条风格, lw: 线条宽度, label: 标签文本 plt.title(day) plt.legend() # 用以显示label的内容 # plt.show() plt.savefig('./' + ts_code + 'day_' + str(day) + '.png') print("\n\n\n\n\n") aa = 1
def start(date_now, result): if Date().is_workday(date_now): ts_codes = Security().get_efficient_ts_code_by_column_name_list( ["normal_status", "tactics_5_status"]) # ts_codes = ['002062.SZ', '002565.SZ'] for ts_code in ts_codes: try: start_date, end_date = date_now - datetime.timedelta( days=30), date_now security_point_data = SecurityData( ).get_qfq_security_point_data(ts_code, start_date, end_date) buy_flag = calc_bs_data(security_point_data) if buy_flag is True: previous_work_day = Date().get_previous_workday(end_date) previous_2_work_day = Date().get_previous_workday( previous_work_day) security_point_data = SecurityData( ).get_qfq_security_point_data( ts_code, previous_2_work_day, end_date + datetime.timedelta(days=20)) result_now = pd.DataFrame([ security_point_data.loc[previous_2_work_day], security_point_data.loc[previous_work_day], security_point_data.iloc[2], security_point_data.iloc[3], security_point_data.iloc[4], security_point_data.iloc[5], security_point_data.iloc[6], security_point_data.iloc[7] ], index=[[ 'previous_2_day', 'previous_1_day', 'next_0_day', 'next_1_day', 'next_2_day', 'next_3_day', 'next_4_day', 'next_5_day' ], [1, 1, 1, 1, 1, 1, 1, 1]]) result_now = result_now.unstack(level=0) result.append(result_now) except Exception as e: pass return result
def start(date_now=None): date_now = datetime.datetime.now() if date_now is None else date_now date_now = datetime.datetime(date_now.year, date_now.month, date_now.day) if Date().is_workday(date_now): start_date, end_date = date_now - datetime.timedelta(days=50), date_now for ts_code in ['600406.SH', '600298.SH', '000001.SZ', '600346.SH', '600104.SH', '601088.SH', '600028.SH', '600340.SH', '600031.SH', '600967.SH', '002216.SZ', '600498.SH', '600176.SH', '002487.SZ', '600857.SH', '603959.SH', '002242.SZ', '603160.SH']: try: security_point_data = SecurityData().get_security_point_data(ts_code, start_date, end_date) buy_flag = buy(security_point_data) if buy_flag is True: Result().insert_strategy_result_data(ts_code, "buy_when_fall", "B", date_now) except Exception as e: pass
def start(date_now=None): date_now = datetime.datetime.now() if date_now is None else date_now date_now = datetime.datetime(date_now.year, date_now.month, date_now.day) if Date().is_workday(date_now): ts_codes = Security().get_efficient_ts_code_by_column_name_list( ["normal_status", "tactics_5_status"]) for ts_code in ts_codes: try: start_date, end_date = date_now - datetime.timedelta( days=90), date_now security_point_data = SecurityData().get_security_point_data( ts_code, start_date, end_date) buy_flag = calc_bs_data(security_point_data) if buy_flag is True: Result().insert_strategy_result_data( ts_code, "bbond", "B", date_now) except Exception as e: pass
def start(date_now=None): date_now = datetime.datetime.now() if date_now is None else date_now date_now = datetime.datetime(date_now.year, date_now.month, date_now.day) if Date().is_workday(date_now): ts_codes = Security().get_efficient_ts_code_by_column_name_list( ["normal_status", "tactics_5_status", "tactics_7_status"]) start_date, end_date = date_now - datetime.timedelta( days=465), date_now for ts_code in ts_codes: # for ts_code in '000592.SZ', '603800.SH', '603700.SH', '002547.SZ', '000709.SZ', '002006.SZ', '002375.SZ', '002009.SZ', '002873.SZ', '002883.SZ', '002911.SZ', '002862.SZ', '601005.SH': try: security_point_data = SecurityData().get_security_point_data( ts_code, start_date, end_date) holder_number_data = MarketData().get_holder_number_data( ts_code, start_date, end_date) buy_flag = buy(security_point_data, holder_number_data) if buy_flag is True: Result().insert_strategy_result_data( ts_code, "buy_when_holder_number_fall", "B", date_now) except Exception as e: pass
def start(date_now, result): if Date().is_workday(date_now): ts_codes = Security().get_efficient_ts_code_by_column_name_list( ["normal_status", "tactics_5_status"]) # ts_codes = ['002062.SZ', '002565.SZ'] for ts_code in ts_codes: try: start_date, end_date = date_now - datetime.timedelta( days=90), date_now security_point_data = SecurityData( ).get_qfq_security_point_data(ts_code, start_date, end_date) buy_flag = calc_bs_data(security_point_data) if buy_flag is True: previous_work_day = Date().get_previous_workday(end_date) previous_2_work_day = Date().get_previous_workday( previous_work_day) next_0_workday = date_now next_1_workday = Date().get_next_workday(date_now) next_2_workday = Date().get_next_workday(next_1_workday) next_3_workday = Date().get_next_workday(next_2_workday) next_4_workday = Date().get_next_workday(next_3_workday) next_5_workday = Date().get_next_workday(next_4_workday) security_point_data = SecurityData( ).get_qfq_security_point_data( ts_code, previous_2_work_day, end_date + datetime.timedelta(days=20)) security_daily_basic_data = SecurityData( ).get_security_daily_basic_data( ts_code, previous_2_work_day, end_date + datetime.timedelta(days=20)) security_data = pd.merge(security_point_data, security_daily_basic_data, on=["ts_code", "trade_date"]) security_data.drop(['close_y'], axis=1, inplace=True) security_data.rename(index=str, columns={"close_x": "close"}, inplace=True) security_data.set_index( security_daily_basic_data["trade_date"], inplace=True) security_data = security_data.sort_index() result_now = pd.DataFrame([ security_data.loc[previous_2_work_day], security_data.loc[previous_work_day], security_data.loc[next_0_workday], security_data.loc[next_1_workday], security_data.loc[next_2_workday], security_data.loc[next_3_workday], security_data.loc[next_4_workday], security_data.loc[next_5_workday] ], index=[[ 'previous_2_day', 'previous_1_day', 'next_0_day', 'next_1_day', 'next_2_day', 'next_3_day', 'next_4_day', 'next_5_day' ], [1, 1, 1, 1, 1, 1, 1, 1]]) result_now = result_now.unstack(level=0) result.append(result_now) except Exception as e: pass return result
import datetime from util.util_data.security_data import SecurityData ts_code = "600030.SH" security_point_data = SecurityData().get_security_point_data(ts_code, datetime.datetime(2016, 1, 1), datetime.datetime(2019, 3, 22)) security_daily_basic_data = SecurityData().get_security_daily_basic_data(ts_code, datetime.datetime(2016, 1, 1), datetime.datetime(2019, 3, 22)) security_point_data["turnover_rate"] = security_daily_basic_data["turnover_rate_f"] security_point_data.drop("pre_close", axis=1, inplace=True) security_point_data.drop("vol", axis=1, inplace=True) security_point_data.drop("amount", axis=1, inplace=True) security_point_data.drop("change", axis=1, inplace=True) security_point_data.drop("ts_code", axis=1, inplace=True) security_point_data.to_csv('./' + ts_code + '.csv', index=False, columns=['trade_date', 'open', 'low', 'high', 'close', 'turnover_rate', 'pct_chg'], header=False)
def idea_07(data): """ 随机森林尝试 # https://www.cnblogs.com/zichun-zeng/p/4761602.html 模型保存 """ max_depth_map_list = [ { "next_2_day": 1, "next_3_day": 1, "next_4_day": 1, "next_5_day": 1, }, { "next_2_day": 4, "next_3_day": 1, "next_4_day": 4, "next_5_day": 1, }, { "next_2_day": 1, "next_3_day": 1, "next_4_day": 1, "next_5_day": 1, }, { "next_2_day": 1, "next_3_day": 1, "next_4_day": 1, "next_5_day": 3, } ] data_low_low = data[ (data["open"]["next_1_day"] < data["close"]["next_0_day"]) & (data["close"]["next_1_day"] < data["close"]["next_0_day"]) & ( data["open"]["next_1_day"] >= data["close"]["next_1_day"])] data_low_high = data[ (data["open"]["next_1_day"] < data["close"]["next_0_day"]) & (data["close"]["next_1_day"] > data["close"]["next_0_day"])] data_high_low = data[ (data["open"]["next_1_day"] > data["close"]["next_0_day"]) & (data["close"]["next_1_day"] < data["close"]["next_0_day"])] data_high_high = data[ (data["open"]["next_1_day"] > data["close"]["next_0_day"]) & (data["close"]["next_1_day"] > data["close"]["next_0_day"])] for point_tendency_type_str, data, max_depth_map in zip(["data_low_low", "data_low_high", "data_high_low", "data_high_high"], [data_low_low, data_low_high, data_high_low, data_high_high], max_depth_map_list): print(point_tendency_type_str) percent_series = data["open"]["next_0_day"] / 100 target_day_list = ["next_2_day", "next_3_day", "next_4_day", "next_5_day"] target_data_list = [] for day in target_day_list: target_data_list.append([day, (data["close"][day] > data["high"]["next_1_day"]).values]) data = pd.DataFrame({ "open": data["open"]["next_0_day"] / percent_series, "close": data["close"]["next_0_day"] / percent_series, "high": data["high"]["next_0_day"] / percent_series, "low": data["low"]["next_0_day"] / percent_series, "turnover_rate_f": data["turnover_rate_f"]["next_0_day"], "pct_chg": data["turnover_rate_f"]["next_0_day"], }).values forest_list = [] for day, target_data in target_data_list: max_depth = max_depth_map[day] X_train, X_test, y_train, y_test = train_test_split(data, target_data, random_state=0) forest = RandomForestClassifier(n_estimators=100, random_state=0, max_depth=max_depth) forest.fit(X_train, y_train) forest_list.append([day, forest, X_train, X_test, y_train, y_test]) for day, forest, X_train, X_test, y_train, y_test in forest_list: if forest.score(X_train, y_train) > 0.71 and forest.score(X_test, y_test) > 0.71: print("day is {0}".format(day)) print("Accuracy on training set: {:.3f}".format(forest.score(X_train, y_train))) print("Accuracy on test set: {:.3f}".format(forest.score(X_test, y_test))) # all_ts_code = get_result_ts_code_list() date_now = datetime.datetime.now() date = datetime.datetime(date_now.year, date_now.month, date_now.day) date = datetime.datetime(date_now.year, date_now.month, 24) all_ts_code = Result().get_strategy_result_data("bbond", 'B', date) for ts_code in all_ts_code: security_point_data = SecurityData().get_security_point_data(ts_code, date, date) security_daily_basic_data = SecurityData().get_security_daily_basic_data(ts_code, date, date) security_data = pd.merge(security_point_data, security_daily_basic_data, on=["ts_code", "trade_date"]) security_data.drop(['close_y'], axis=1, inplace=True) security_data.rename(index=str, columns={"close_x": "close"}, inplace=True) security_data.set_index(security_daily_basic_data["trade_date"], inplace=True) security_data = security_data.sort_index() if security_data.empty is False: security_data = security_data.loc[date] else: continue predict_data = [security_data["open"], security_data["close"], security_data["high"], security_data["low"]] predict_data = np.array([predict_data]) / (predict_data[0] / 100) predict_data = np.array([[predict_data[0][0], predict_data[0][1], predict_data[0][2], predict_data[0][3], security_data["turnover_rate_f"], security_data["pct_chg"]]]) predict_result_now = forest.predict(predict_data) if predict_result_now == [True]: print(ts_code, predict_result_now) # print(ts_code, predict_result_now) print("\n\n") print("\n\n\n")
def start(): security_data = SecurityData().get_index_point_data('399300.SZ', datetime.datetime(2016, 5, 3), datetime.datetime(2019, 1, 31)) # idea_07(security_data) idea_08(security_data)
def idea_07(data): """ 随机森林尝试 """ max_depth_map_list = [ { "next_2_day": 1, "next_3_day": 1, "next_4_day": 1, "next_5_day": 1, }, { "next_2_day": 1, "next_3_day": 1, "next_4_day": 1, "next_5_day": 1, }, { "next_2_day": 1, "next_3_day": 1, "next_4_day": 1, "next_5_day": 1, }, { "next_2_day": 4, "next_3_day": 1, "next_4_day": 1, "next_5_day": 1, } ] data_low_low = data[ (result["open"]["next_1_day"] < result["close"]["next_0_day"]) & (result["close"]["next_1_day"] < result["close"]["next_0_day"]) & ( result["open"]["next_1_day"] >= result["close"]["next_1_day"])] data_low_high = data[ (result["open"]["next_1_day"] < result["close"]["next_0_day"]) & (result["close"]["next_1_day"] > result["close"]["next_0_day"])] data_high_low = data[ (result["open"]["next_1_day"] > result["close"]["next_0_day"]) & (result["close"]["next_1_day"] < result["close"]["next_0_day"])] data_high_high = data[ (result["open"]["next_1_day"] > result["close"]["next_0_day"]) & (result["close"]["next_1_day"] > result["close"]["next_0_day"])] for point_tendency_type_str, data, max_depth_map in zip(["data_low_low", "data_low_high", "data_high_low", "data_high_high"], [data_low_low, data_low_high, data_high_low, data_high_high], max_depth_map_list): print(point_tendency_type_str) percent_series = data["open"]["next_0_day"] / 100 target_day_list = ["next_2_day", "next_3_day", "next_4_day", "next_5_day"] target_data_list = [] for day in target_day_list: target_data_list.append([day, (data["close"][day] > data["high"]["next_1_day"]).values]) data = pd.DataFrame({ "open": data["open"]["next_0_day"] / percent_series, "close": data["close"]["next_0_day"] / percent_series, "high": data["high"]["next_0_day"] / percent_series, "low": data["low"]["next_0_day"] / percent_series }).values forest_list = [] for day, target_data in target_data_list: max_depth = max_depth_map[day] X_train, X_test, y_train, y_test = train_test_split(data, target_data, random_state=0) forest = RandomForestClassifier(n_estimators=100, random_state=0, max_depth=max_depth) forest.fit(X_train, y_train) forest_list.append([day, forest, X_train, X_test, y_train, y_test]) for day, forest, X_train, X_test, y_train, y_test in forest_list: if forest.score(X_train, y_train) > 0.71 and forest.score(X_test, y_test) > 0.71: print("day is {0}".format(day)) print("Accuracy on training set: {:.3f}".format(forest.score(X_train, y_train))) print("Accuracy on test set: {:.3f}".format(forest.score(X_test, y_test))) # all_ts_code = get_result_ts_code_list() date_now = datetime.datetime.now() date = datetime.datetime(date_now.year, date_now.month, date_now.day) all_ts_code = Result().get_strategy_result_data("a_p_5_p_10", 'B', date) for ts_code in all_ts_code: security_point_data = SecurityData().get_security_point_data(ts_code, date, date) if security_point_data.empty is False: security_point_data = security_point_data.loc[date] else: continue predict_data = [security_point_data["open"], security_point_data["close"], security_point_data["high"], security_point_data["low"]] predict_data = np.array([predict_data]) / (predict_data[0] / 100) predict_result_now = forest.predict(predict_data) if predict_result_now == [True]: print(ts_code, predict_result_now) # print(ts_code, predict_result_now) print("\n\n") print("\n\n\n")