forked from shenrunzhang/forex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot2.py
More file actions
129 lines (98 loc) · 3.5 KB
/
plot2.py
File metadata and controls
129 lines (98 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import numpy as np
import matplotlib.pyplot as plt
from pandas import read_csv
import pandas as pd
import math
import tensorflow as tf
from tensorflow import keras
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense
from tensorflow.python.keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from Threshold import get_threshold
MODEL = 'prob_model.h5'
DATA = r'C:\Users\Shen\Documents\forex\technical_data_eurusd2.csv'
dataframe = read_csv(DATA, engine='python')
dataset = dataframe.values
dataset = dataset.astype('float32')
# cut off first 20 values
dataset = dataset[20:]
dataset = dataset[:, 1:]
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
train_size = int(len(dataset) * 0.8)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size, :], dataset[train_size:len(dataset), :]
# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1):
dataX, dataY = [], []
for i in range(len(dataset)-look_back-1):
a = dataset[i:(i+look_back), :]
dataX.append(a)
dataY.append(dataset[i + look_back, 0])
return np.array(dataX), np.array(dataY)
look_back = 5
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
# reshape input to be [samples, time steps, features]
trainX = np.reshape(trainX, (trainX.shape[0], look_back, 9))
testX = np.reshape(testX, (testX.shape[0], look_back, 9))
model = tf.keras.models.load_model(MODEL)
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
trainPredict = np.squeeze(trainPredict)
testPredict = np.squeeze(testPredict)
# Inverse scale the data
def inverse_transform(arr):
extended = np.zeros((len(arr), 9))
extended[:, 0] = arr
return scaler.inverse_transform(extended)[:, 0]
trainPredict = inverse_transform(trainPredict)
testPredict = inverse_transform(testPredict)
trainY = inverse_transform(trainY)
testY = inverse_transform(testY)
prob = model.predict_proba(testX)
print("testY", testY)
print("test predict", testPredict)
print("probab", prob)
# # threshold and make decisions
# threshold = get_threshold(dataframe["Close"])
# def decision(diff):
# if diff > threshold:
# return "I"
# if -diff > threshold:
# return "D"
# else:
# return "N"
# def union(a, b):
# if a == "I" and b == "I":
# return "C"
# if a == "D" and b == "D":
# return "C"
# if a == "N" or b == "N":
# return "N"
# else:
# return "F"
# table = pd.DataFrame([testY, testPredict]).transpose()
# table.columns = ["actual", "predict"]
# table["p-a"] = table.predict - table.actual
# table["decision"] = table["p-a"].apply(decision)
# table["correct"] = table["actual"].diff(
# ).shift(-1).apply(lambda x: "I" if x > 0 else "D" if x < 0 else "N")
# table["union"] = table.apply(lambda x: union(
# x["decision"], x["correct"]), axis=1)
# counts = table["union"].value_counts()
# frequency = table["union"].value_counts(normalize=True)
# print(pd.DataFrame({"counts": counts, "frequency": frequency}))
# shift predictions up by one
testPredict = np.delete(testPredict, -1)
testY = np.delete(testY, 0)
# Plot results
plt.plot(testPredict, color="blue")
plt.plot(testY, color="red")
plt.plot(prob, color="green")
plt.show()
testScore = np.sqrt(mean_squared_error(testY, testPredict))
print('Test Score: %.2f RMSE' % (testScore))