Exemple #1
0
def one_day_prediction_svm_18(ETFtable,
                              ETF_list,
                              window,
                              interval,
                              kernel='rbf',
                              gamma=1,
                              C=1.0,
                              coef0=0):
    ETF_score = []
    for ETF_code in ETF_list:
        ETF_target = load_ETF(ETFtable, ETF_code)
        ETF_target_rate = get_change_rate(ETF_target)
        get_rise_fall(ETF_target_rate, interval)
        X_train, y_train, X_test, y_test = load_data(ETF_target_rate, window)

        # Model
        clf = svm.SVC(kernel=kernel, gamma=gamma, C=C, coef0=coef0)
        clf.fit(X_train, y_train)

        y_predict_test = clf.predict(X_test)
        rbf_test = sum(y_predict_test == y_test) * 0.5 / (y_test.shape[0] *
                                                          1.0)

        y_predict_train = clf.predict(X_train)
        rbf_train = sum(y_predict_train == y_train) * 0.5 / (y_train.shape[0] *
                                                             1.0)
        #         print('Average train one day score is ',sum(y_predict_train == y_train)*0.5/(y_train.shape[0]*1.0))
        #         print('Average test one day score is ',sum(y_predict_test == y_test)*0.5/(y_test.shape[0]*1.0))

        print(f'{ETF_code} average one day score is ,', rbf_test)
        ETF_score.append(rbf_test)

    print('Total ETF one day rise_fall score is', sum(ETF_score))
    print('Average ETF one day rise_fall score is', sum(ETF_score) / 18.0)
Exemple #2
0
def one_day_prediction_regression_18(ETFtable, ETF_list, window, lamb,
                                     interval):
    ETF_score = []
    for ETF_code in ETF_list:
        ETF_target = load_ETF(ETFtable, ETF_code)

        #price data
        ETF_target_N = normalize_data(ETF_target)
        X_train_p, y_train_p, X_test_p, y_test_p = load_data(
            ETF_target_N, window)

        #rate data
        ETF_target_rate = get_change_rate(ETF_target)
        get_rise_fall(ETF_target_rate, interval)
        X_train_r, y_train_r, X_test_r, y_test_r = load_data(
            ETF_target_rate, window)

        ### Model
        lr = LinearRegressionReg()
        lr.fit(X_train_p, y_train_p, lamb)

        ### score
        p = lr.predict(X_test_p)
        y_predict_de = denormalize(ETF_target, p, 'close').reshape(
            (y_test_p.shape[0]))

        y_predict_rise_fall = []
        for day, price in enumerate(y_predict_de):
            if (day != 0):
                y_predict_rise_fall.append(
                    sign(y_predict_de[day] - y_predict_de[day - 1]))
        y_predict_rise_fall = np.array(y_predict_rise_fall).astype(np.float64)

        if (y_test_r.shape[0] > y_predict_rise_fall.shape[0]):
            y_test_r_short = y_test_r[y_test_r.shape[0] -
                                      y_predict_rise_fall.shape[0]:]
            score = (sum(y_test_r_short == y_predict_rise_fall) *
                     0.5) / (y_test_r_short.shape[0] * 1.0)
        else:
            score = (sum(y_test_r == y_predict_rise_fall) *
                     0.5) / (y_test_r.shape[0] * 1.0)

        print(f'{ETF_code} average one day score is', score)
        ETF_score.append(score)

    print('Total ETF one day rise_fall score is', sum(ETF_score))
    print('Average ETF one day rise_fall score is', sum(ETF_score) / 18.0)
    return ETF_score
Exemple #3
0
def five_day_prediction_18(ETFtable, ETF_list, window, lamb):
    ETF_score = []
    ETF_week_score = []
    for ETF_code in ETF_list:
        ETF_target = load_ETF(ETFtable, ETF_code)
        ETF_target_N = normalize_data(ETF_target)

        fiveday_score_array, fiveday_score = week_score(
            ETF_target, ETF_target_N, window, lamb)
        print(f'{ETF_code} average Fiveday_score is ', fiveday_score)
        ETF_score.append(fiveday_score)
        ETF_week_score.append(fiveday_score_array)

    print('Total ETF five day price score is', sum(ETF_score))
    print('Average ETF five day price score is', sum(ETF_score) / 18.0)
    return ETF_week_score
Exemple #4
0
def predict_ETF_close(ETFtable, ETF_list, window, lamb):
    ETF_week_close = []
    ETF_week_score = []
    ETF_lastday_close = []
    for ETF_code in ETF_list:
        ETF_target = load_ETF(ETFtable, ETF_code)
        ETF_target_N = normalize_data(ETF_target)
        close_price, lastday_close = get_fiveday_close(ETF_target_N,
                                                       ETF_target, window,
                                                       lamb)

        ETF_week_close.append(close_price)
        ETF_lastday_close.append(lastday_close)

    ETF_week_close = np.array(ETF_week_close)
    ETF_lastday_close = np.array(ETF_lastday_close)

    return ETF_week_close, ETF_lastday_close
Exemple #5
0
def one_day_prediction_18(ETFtable, ETF_list, window, lamb):
    ETF_score = []
    ETF_model = []
    for ETF_code in ETF_list:
        ETF_target = load_ETF(ETFtable, ETF_code)
        ETF_target_N = normalize_data(ETF_target)
        X_train, y_train, X_test, y_test = load_data(ETF_target_N, window)

        ### Model
        lr = LinearRegressionReg()
        lr.fit(X_train, y_train, lamb)

        ### score
        score = lr.score(X_test, y_test, ETF_target)
        print(f'{ETF_code} average one day score is', score)
        ETF_score.append(score)
        ETF_model.append(lr)
    ETF_features = ETF_target.columns
    print('Total ETF one day price score is', sum(ETF_score))
    print('Average ETF one day price score is', sum(ETF_score) / 18.0)
    return [ETF_score, ETF_model, ETF_features]