Beispiel #1
0
def generate_singal(argv):
    eur = load.load_fx_pairs([argv.target_pair])[0]
    pred, entry_point, gain_point, loss_point = analyze.next_signal(
        eur, argv.forward_days, argv.backward_days, argv.feature_days)
    cond = {0: "Bull", 1: "Bear"}
    res_msg = """
    Current Price: {0}
    Prediction Result: {1}
    Entry Point : {2}
    Gain Point : {3}
    Loss Point : {4}
    """.format(eur.cprice[-1], cond[pred], entry_point, gain_point, loss_point)
    return res_msg
Beispiel #2
0
def test_hcomb():
    fx_pairs = load.load_fx_pairs([
        "EURUSD", "AUDUSD", "CHFJPY", "EURCHF", "EURGBP", "EURJPY", "GBPCHF",
        "GBPJPY", "GBPUSD", "USDCAD", "USDCHF", "USDJPY"
    ])
    eur_X, eur_y = fx_pairs[0].prepare(bk_days, fw_days, feature_days)
    stack_X = [eur_X]
    for i in range(1, len(fx_pairs)):
        pair_X, pair_y = fx_pairs[i].prepare(bk_days, fw_days, feature_days)
        stack_X.append(pair_X)
    X = np.hstack(tuple(stack_X))
    y = eur_y
    model = _train_model("lg", X[:-1000], y[:-1000], X[-1000:], y[-1000:])
    joblib.dump(model, "models/hcomb")
Beispiel #3
0
def test_vcomb():
    # fx_pairs = load.load_fx_pairs(["EURUSD", "AUDUSD", "CHFJPY", "EURCHF", "EURGBP", "EURJPY", "GBPCHF", "GBPJPY",
    #                                "GBPUSD", "USDCAD", "USDCHF", "USDJPY"])
    fx_pairs = load.load_fx_pairs(
        ["EURUSD", "AUDUSD", "CHFJPY", "EURCHF", "EURGBP", "EURJPY"])
    stack_X = []
    stack_y = []
    eur_X, eur_y = fx_pairs[0].prepare(bk_days, fw_days, feature_days)
    for i in range(1, len(fx_pairs)):
        pair_X, pair_y = fx_pairs[i].prepare(bk_days, fw_days, feature_days)
        stack_X.append(pair_X)
        stack_y.extend(pair_y)
    X = np.vstack(tuple(stack_X))
    y = stack_y
    model = _train_model("lg", X, y, eur_X, eur_y)
    joblib.dump(model, "models/vcomb")
Beispiel #4
0
def test_return2():
    eur = load.load_fx_pairs(['EURUSD'])[0]
    eur_X, eur_y = eur.prepare(bk_days, fw_days, feature_days)
    model = joblib.load("models/vcomb")
    preds = model.predict(eur_X)
    fx_td = forex.Fx_Trade(eur, preds, bk_days, fw_days)
    start_idx = feature_days + bk_days
    total_gain = 0
    total_trade = 0
    for i, pred in enumerate(preds[:-fw_days]):
        gain = fx_td.get_profit(start_idx + i, i)
        if gain != 0:
            total_gain += gain
            total_trade += 1
    print("Total Gain : " + str(total_gain))
    print("Total Trade : " + str(total_trade))
    print("Gain Per Trade : " + str(total_gain / total_trade))
Beispiel #5
0
def test_predict():
    eur = load.load_fx_pairs(["EURUSD"])[0]
    gbull_mean, gbull_std, lbull_mean, lbull_std, gbear_mean, gbear_std, lbear_mean, lbear_std = count_gain_loss(
        eur)
    #gbull_mean, lbull_mean, lbull_std, gbear_mean, lbear_mean, lbear_std = count_gain_loss(eur)
    start_idx = eur.num_examples - 1
    end_idx = eur.num_examples

    pred_X = eur.prepare_X(start_idx, end_idx, feature_days)

    model = joblib.load("models/vcomb")
    pred = model.predict(pred_X)
    if pred[0] == 0:
        loss_bound = eur.cprice[-1] - lbull_mean - 2 * lbull_std
        gain_bound = eur.cprice[-1] + gbull_mean
    else:
        loss_bound = eur.cprice[-1] + lbear_mean + 2 * lbear_std
        gain_bound = eur.cprice[-1] - gbear_mean
    print("Predict: " + str(pred))
    print("Gain Bound: " + str(gain_bound))
    print("Loss Bound: " + str(loss_bound))
Beispiel #6
0
def test_tf():
    flat_row = math.ceil(math.ceil(feature_days / 2) / 2)
    flat_col = math.ceil(math.ceil(features / 2) / 2)
    fx_pairs = load.load_fx_pairs(["EURUSD","AUDUSD", "CHFJPY", "EURCHF", "EURGBP", "EURJPY", "GBPCHF", "GBPJPY",
                                   "GBPUSD", "USDCAD", "USDCHF", "USDJPY"])
    stack_X = []
    stack_y = []
    eur_X,eur_y = fx_pairs[0].prepare(bk_days,fw_days,feature_days,True)
    test_X = eur_X.reshape((eur_X.shape[0], feature_days,features,1))
    test_y = np.array(eur_y)
    for i in range(1, len(fx_pairs)):
        pair_X, pair_y = fx_pairs[i].prepare(bk_days,fw_days,feature_days,True)
        stack_X.append(pair_X)
        stack_y.extend(pair_y)
    X = np.vstack(tuple(stack_X))
    X = X.reshape((X.shape[0], feature_days, features,1))
    y = np.array(stack_y)
    train_set = forex.Tensor_Set(X,y)
    sess = tf.InteractiveSession()
    x = tf.placeholder(tf.float32, [None, feature_days,features,1])
    y_ = tf.placeholder(tf.float32, shape=[None, 2])
    W_conv1 = weight_variable([2, 2, 1, 32])
    b_conv1 = bias_variable([32])
    # x_image = tf.reshape(x, [-1, 28, 28, 1])
    #
    # x_t = x_image.eval(feed_dict={x: mnist.train.next_batch(50)[0]})
    h_conv1 = tf.nn.relu(conv2d(x, W_conv1) + b_conv1)
    h_pool1 = max_pool_2x2(h_conv1)
    #
    W_conv2 = weight_variable([2, 2, 32, 64])
    b_conv2 = bias_variable([64])
    #
    h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
    h_pool2 = max_pool_2x2(h_conv2)
    #
    W_fc1 = weight_variable([flat_row*flat_col*64, feature_days*features])
    b_fc1 = bias_variable([feature_days*features])
    #
    h_pool2_flat = tf.reshape(h_pool2, [-1,  flat_row* flat_col * 64])
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
    #
    keep_prob = tf.placeholder(tf.float32)
    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
    #
    W_fc2 = weight_variable([feature_days*features, 2])
    b_fc2 = bias_variable([2])
    #
    y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
    #
    cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_conv, y_))
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
    # train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
    correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    # saver = tf.train.Saver()
    sess.run(tf.global_variables_initializer())
    for i in range(30000):
        batch = train_set.next_batch(1000)
        if i % 100 == 0:
            train_accuracy = accuracy.eval(feed_dict={
                x: batch[0], y_: batch[1], keep_prob: 1.0})
            # saver.save(sess, "Model/test", global_step=i)
            print("step %d, training accuracy %g" % (i, train_accuracy))
        train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.7})

    print("test accuracy %g" % accuracy.eval(feed_dict={
        x: test_X, y_: test_y, keep_prob: 1.0}))
Beispiel #7
0
def test_eur():
    eur_pair = load.load_fx_pairs(["EURUSD"])[0]
    eur_X, eur_y = eur_pair.prepare(bk_days, fw_days, feature_days)
    _train_model("lg", eur_X[:-1000], eur_y[:-1000], eur_X[-1000:],
                 eur_y[-1000:])
Beispiel #8
0
def test_return():
    eur = load.load_fx_pairs(['EURUSD'])[0]
    gbull_mean, gbull_std, lbull_mean, lbull_std, gbear_mean, gbear_std, lbear_mean, lbear_std = count_gain_loss(
        eur)
    eur_X, eur_y = eur.prepare(bk_days, fw_days, feature_days)
    model = joblib.load("models/vcomb")
    preds = model.predict(eur_X)
    start_idx = feature_days + bk_days
    emabk = talib.EMA(eur.cprice, timeperiod=bk_days)
    emafw = talib.EMA(eur.cprice, timeperiod=fw_days)
    total_gain = 0
    total_trade = 0
    incorrect = 0
    for i, pred in enumerate(preds[:-fw_days]):
        # bull_gain_bound = emabk[start_idx + i] + gbull_mean
        # bull_loss_bound = emabk[start_idx + i] - lbull_mean - 2*lbull_std
        # bear_gain_bound = emabk[start_idx + i] - gbear_mean
        # bear_loss_bound = emabk[start_idx + i] + lbear_mean + 2*lbear_std
        bk_up_diff = eur.hprice[start_idx + i - bk_days:start_idx +
                                i] - eur.cprice[start_idx + i -
                                                bk_days:start_idx + i]
        bk_dw_diff = eur.cprice[start_idx + i - bk_days:start_idx +
                                i] - eur.lprice[start_idx + i -
                                                bk_days:start_idx + i]
        bk_up_mean = np.mean(bk_up_diff)
        bk_dw_mean = np.mean(bk_dw_diff)
        bk_up_std = np.std(bk_up_diff)
        bk_dw_std = np.std(bk_dw_diff)
        bull_gain_bound = eur.cprice[start_idx + i] + 0.006
        bull_loss_bound = eur.cprice[start_idx +
                                     i] - lbull_mean - 2 * lbull_std
        bear_gain_bound = eur.cprice[start_idx + i] - 0.006
        bear_loss_bound = eur.cprice[start_idx +
                                     i] + lbear_mean + 2 * lbear_std
        # bk_up_bound = eur.cprice[start_idx + i] + bk_up_mean + 2*bk_up_std
        # bk_dw_bound = eur.cprice[start_idx + i] - bk_dw_mean - 2*bk_dw_std
        max_price = max(eur.hprice[start_idx + i + 1:start_idx + i + fw_days +
                                   1])
        min_price = min(eur.lprice[start_idx + i + 1:start_idx + i + fw_days +
                                   1])
        max_idx = np.argmax(eur.hprice[start_idx + i + 1:start_idx + i +
                                       fw_days + 1])
        min_idx = np.argmin(eur.lprice[start_idx + i + 1:start_idx + i +
                                       fw_days + 1])

        bk_std = np.std(eur.cprice[start_idx + i - bk_days:start_idx + i])
        bk_mean = np.mean(eur.cprice[start_idx + i - bk_days:start_idx + i])
        # bk_diff = abs(eur.cprice[start_idx + i] - eur.cprice[start_idx + i - 1])
        bk_diff = abs(eur.cprice[start_idx + i] - bk_mean)

        print("Close Price Sequence: " +
              str(eur.cprice[start_idx + i:start_idx + i + fw_days + 1]))
        print("High Price Sequence: " +
              str(eur.hprice[start_idx + i:start_idx + i + fw_days + 1]))
        print("Low Price Sequence: " +
              str(eur.lprice[start_idx + i:start_idx + i + fw_days + 1]))
        if pred == 0 and eur.cprice[
                start_idx +
                i] < bull_gain_bound and bk_diff >= 2 * bk_std:  #and abs(eur.cprice[start_idx + i] - emabk[start_idx + i])*10000 < 30:
            total_trade += 1
            meet_gt, gt_price, gt_idx = first_gt_index(
                bull_gain_bound,
                eur.hprice[start_idx + i + 1:start_idx + i + fw_days + 1])
            meet_ls, ls_price, ls_idx = first_ls_index(
                bull_loss_bound,
                eur.lprice[start_idx + i + 1:start_idx + i + fw_days + 1])
            reverse_idx = np.argmax(preds[i:i + fw_days])
            # reverse_idx = 1000
            if reverse_idx == 0: reverse_idx = 1000
            if meet_gt and (gt_idx < ls_idx) and gt_idx <= reverse_idx:
                # if max_price >= bull_gain_bound and (min_price > bull_loss_bound or max_idx < min_idx) and max_idx <=reverse_idx:
                gain = (bull_gain_bound - eur.cprice[start_idx + i]) * 10000
                total_gain += gain
                print("Bull Gain at day {0} (".format(gt_idx + 1) +
                      str(eur.cprice[start_idx + i]) + ") : " + str(gain))
            # elif min_price <= bull_loss_bound:
            elif meet_ls and ls_idx <= reverse_idx:
                loss = (eur.cprice[start_idx + i] - bull_loss_bound) * 10000
                total_gain -= loss
                print("Bull Loss at day {0} (".format(ls_idx + 1) +
                      str(eur.cprice[start_idx + i]) + ") : " + str(-loss))
            elif reverse_idx != 0 and reverse_idx != 1000:
                rev = (eur.cprice[start_idx + i + reverse_idx] -
                       eur.cprice[start_idx + i]) * 10000
                total_gain += rev
                print("Bull Reverse at day {0} (".format(reverse_idx) +
                      str(eur.cprice[start_idx + i]) + ") : " + str(rev))
            else:
                even = (eur.cprice[start_idx + i + fw_days] -
                        eur.cprice[start_idx + i]) * 10000
                total_gain += even
                print("Bull Even (" + str(eur.cprice[start_idx + i]) + ") : " +
                      str(even))
            if emafw[start_idx + i + fw_days] > emabk[start_idx + i]:
                print("Prediction: Correct")
            else:
                incorrect += 1
                print("Prediction: Incorrect")
            print("EMA Back: " + str(emabk[start_idx + i]))
            print("EMA Forword: " + str(emafw[start_idx + i + fw_days]))
            print("Loss Bound : " + str(bull_loss_bound))
            print("Gain Bound : " + str(bull_gain_bound))
            print(
                "================================================================================"
            )
        elif pred == 1 and eur.cprice[
                start_idx +
                i] > bear_gain_bound and bk_diff >= 2 * bk_std:  #and abs(eur.cprice[start_idx + i] - emabk[start_idx + i])*10000 < 30:
            total_trade += 1
            meet_gt, gt_price, gt_idx = first_gt_index(
                bear_loss_bound,
                eur.hprice[start_idx + i + 1:start_idx + i + fw_days + 1])
            meet_ls, ls_price, ls_idx = first_ls_index(
                bear_gain_bound,
                eur.lprice[start_idx + i + 1:start_idx + i + fw_days + 1])
            reverse_idx = np.argmin(preds[i:i + fw_days])
            # reverse_idx = 1000
            if reverse_idx == 0: reverse_idx = 1000
            if meet_ls and (ls_idx < gt_idx) and ls_idx <= reverse_idx:
                # if min_price <= bear_gain_bound and (max_price < bear_loss_bound or min_idx < max_idx) and min_idx <= reverse_idx:
                gain = (eur.cprice[start_idx + i] - bear_gain_bound) * 10000
                total_gain += gain
                print("Bear Gain at day {0} (".format(ls_idx + 1) +
                      str(eur.cprice[start_idx + i]) + ") : " + str(gain))
            elif meet_gt and gt_idx <= reverse_idx:
                # elif max_price >= bear_loss_bound:
                loss = (bear_loss_bound - eur.cprice[start_idx + i]) * 10000
                total_gain -= loss
                print("Bear Loss at day {0} (".format(gt_idx + 1) +
                      str(eur.cprice[start_idx + i]) + ") : " + str(-loss))
            elif reverse_idx != 0 and reverse_idx != 1000:
                rev = (eur.cprice[start_idx + i] -
                       eur.cprice[start_idx + i + reverse_idx]) * 10000
                total_gain += rev
                print("Bear Reverse at day {0} (".format(reverse_idx) +
                      str(eur.cprice[start_idx + i]) + ") : " + str(rev))
            else:
                even = (eur.cprice[start_idx + i] -
                        eur.cprice[start_idx + i + fw_days]) * 10000
                total_gain += even
                print("Bear Even (" + str(eur.cprice[start_idx + i]) + ") : " +
                      str(even))
            if emafw[start_idx + i + fw_days] < emabk[start_idx + i]:
                print("Prediction: Correct")
            else:
                incorrect += 1
                print("Prediction: Incorrect")
            print("EMA Back: " + str(emabk[start_idx + i]))
            print("EMA Forword: " + str(emafw[start_idx + i + fw_days]))
            print("Loss Bound : " + str(bear_loss_bound))
            print("Gain Bound : " + str(bear_gain_bound))
            print(
                "================================================================================"
            )
        else:
            print(
                "================================================================================"
            )
            continue
    print("Total Gain : " + str(total_gain))
    print("Total Trade : " + str(total_trade))
    print("Predict Accuracy :" + str((total_trade - incorrect) / total_trade))
    print("Gain Per Trade : " + str(total_gain / total_trade))