def day_chart_datas():
    """
    chart how more data lowers error
    note its diff each time
    """
    df = pd.read_csv('amd.csv', index_col='timestamp', parse_dates=True)
    # 1949 / 16 = 120
    r = range(0, len(df), 120)[1:]
    train_errors = []
    test_errors = []
    for amount in r:
        d = DayTrader(df=df[:amount])
        clf, train_acc = d.decision_tree(debug=False)
        test_acc = day_simulations((clf, train_acc), d)
        train_errors.append(1.0 - train_acc)
        test_errors.append(1.0 - test_acc)

    print train_errors
    print test_errors

    plt.xticks(r)
    plt.title('Decision Trees: Amount of Data and Accuracy')
    plt.plot(r, train_errors, label="Training Error")
    plt.plot(r, test_errors, label="Testing Error")
    plt.legend()
    plt.show()
예제 #2
0
def lda():
    s = DayTrader()
    X_trans = s.lda()
    fig, ax = plt.subplots()
    plt.title('Random Projection Reduction to Two Days on Swing Trading')
    ax.set_xlabel('First Price')
    ax.set_ylabel('Second Price')
    print X_trans
    plt.plot(X_trans)
    # plt.scatter(X_trans[:, 0], X_trans[:, 1])
    plt.show()
def ideal_prune():
    """ around 3 to 5"""
    accs = []
    d = DayTrader()
    r = range(0, 35)
    for pruneval in r:
        clf, train_acc = d.decision_tree(debug=False, prune=pruneval)
        test_acc = day_simulations((clf, train_acc), d)
        accs.append(1.0 - test_acc)
    plt.xticks(r)
    plt.title('Decision Trees: Ideal Prune Value')
    plt.plot(r, accs, label="Error")
    plt.legend()
    plt.show()
예제 #4
0
def neural_with_pca():
    """
    old results

    in paper: an average accuracy of 46.28% and daily returns of 1.318%.
    takes about 15 sec to run
    >>>     analysis.day_trials()
    average accuracies:
    0.4574418604651163
    avg daily returns:
    1.3216907197464711 %

    in paper: 53.7% accuracy and 1.23% in daily returns on avg
    takes about 2 min to run
    >>>     analysis.swing_several_sims()
    # Avg accuracies and money:
    0.4830140946873871
    3.0788267587245786
    """
    # analysis.day_trials()
    # analysis.swing_several_sims()
    s = DayTrader()
    r = range(1, 21)
    accuracies = []
    for i in r:
        acc = 0
        for _ in range(2):
            X = s.pca(n_components=i, X=s.train_xs)
            testX = s.pca(n_components=i, X=s.test_xs)
            acc += analysis.day_trials(X=X, testX=testX)[0]
        accuracies.append(acc / 2.)

    title = 'Neural Net After PCA for Day Trading'
    fig, ax = plt.subplots()
    plt.title(title)
    ax.set_xlabel('Num of Components')
    ax.set_ylabel('Accuracy')
    plt.plot(r, accuracies)
    plt.show()
def day_trials(outer_trials=2,
               inner_trials=5,
               kernel='poly',
               degree=5,
               neighbors=5,
               prune_val=5,
               X=None,
               testX=None):
    allmoneys = []
    allboosts = []
    for _ in range(outer_trials):
        d = DayTrader()
        boosts, moneys = [], []
        for _ in range(0, inner_trials):
            # print "*******BOOSTING"
            # acc, money = day_simulations(d.svm(debug=False, kernel=kernel, degree=degree), d, debug=False)
            # acc, money = day_simulations(d.knn(debug=False, neighbors=neighbors), d, debug=False)
            acc, money = day_simulations(d.neural_network(debug=False,
                                                          X=X,
                                                          testX=testX),
                                         d,
                                         debug=False)
            boosts.append(acc)
            moneys.append(money)
        # print "*************\n\naverage accuracies for :"
        # print np.mean(boosts)
        # print "avg money:", np.mean(moneys)
        # print "avg daily returns:"
        dr = (abs(np.mean(moneys))**(1.0 / len(d.test_xs)) - 1) * 100
        if np.mean(moneys) < 0: dr *= -1
        allmoneys.append(dr)
        allboosts.append(np.mean(boosts))
        # print dr,"%"
    print "*************\n\naverage accuracies for :"
    print np.mean(allboosts)
    print "avg daily returns:"
    print np.mean(allmoneys), "%"
    return np.mean(allboosts), np.mean(allmoneys)
def export_data(swing=True):
    if swing:
        s = SwingTrader()
    else:
        s = DayTrader()
    lines = []
    for i in range(len(s.xs)):
        lines.append(s.xs[i] + [s.ys[i]])

    print "len is ", len(s.xs)

    with open('amd-out.txt', 'w') as f:
        for line in lines:
            for item in line:
                f.write("%s " % item)
            f.write("\n")
def export_day_trader():
    s = DayTrader()

    # limit to last 7, not last 20, data points
    trimmed_xs, trimmed_ys = [], []

    lines = []
    for i in range(len(s.xs)):
        trimmed_xs = s.xs[i][-5:]
        y = [s.ys[i]]
        lines.append(trimmed_xs + y)
        print trimmed_xs + y

    # print lines
    print "len is ", len(s.xs)

    with open('amd-out-trimmed.txt', 'w') as f:
        for line in lines:
            for item in line:
                f.write("%s " % item)
            f.write("\n")
예제 #8
0
def test_em():
    SwingTrader().expectation_max()
    DayTrader().expectation_max()
예제 #9
0
def test_k_means():
    # s = SwingTrader()
    # s.k_means_clustering(n_clusters=2)
    d = DayTrader()
    d.k_means_clustering(n_clusters=2)
예제 #10
0
def kmeans_rca():
    s = DayTrader()
    X_trans = s.rca(n_components=2)
    plot_kmeans(s, X_trans,True, 2)
예제 #11
0
def kmeans_ica():
    s = DayTrader()
    X_trans = s.ica(n_components=4)
    plot_kmeans(s, X_trans, False, 4)
예제 #12
0
def kmeans_nodimred():
    s = DayTrader()
    # X_trans = s.pca(n_components=3)
    fig, ax = plt.subplots()
    plot_kmeans(s, s.xs, True, 3)