-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
134 lines (105 loc) · 3.97 KB
/
run.py
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
129
130
131
132
133
134
from argparse import ArgumentParser, Namespace, BooleanOptionalAction
from src import download_data, create_model, load_data, prepare_data, split_dataset, plot_history
from keras.models import load_model
from os.path import exists
from os import mkdir
from datetime import datetime
def get_args() -> Namespace:
parser = ArgumentParser(description='Advance CNN LSTM Model for Cryptocurrency Forecasting')
parser.add_argument(
'action', choices=['download', 'train', 'predict']
)
parser.add_argument(
'--coins', default='BTCUSDT,ETHUSDT,XRPUSDT', help='List of coins for input of model, separated by comma',
)
parser.add_argument(
'--timeframe', default='d', help='Timeframe of input data: d for 1 day, h for hour, m for minute', choices=['d', 'h', 'm']
)
parser.add_argument(
'--target', default='close', help='Feature to train on'
)
parser.add_argument(
'--data-dir', default='./data', help="Where is training data located"
)
parser.add_argument(
'--exchange', default='binance', help="From what exchange to download data"
)
parser.add_argument(
'--classify',
help="Should model try to predict next value or will market direction",
action=BooleanOptionalAction,
default=False,
)
return parser.parse_args()
def main():
args = get_args()
action = args.action
match action:
case 'download':
download(args)
case 'train':
train(args)
case 'predict':
predict(args)
def download(args: Namespace):
coins = args.coins.split(',')
timeframe = args.timeframe
exchange = args.exchange
data_dir = args.data_dir
if not exists(data_dir):
mkdir(data_dir)
for symbol in coins:
download_data(symbol, timeframe, exchange, data_dir)
return
def train(args: Namespace):
kwargs = args.__dict__
symbols = kwargs.pop('coins').split(',')
timeframe = kwargs.pop('timeframe')
exchange = kwargs.pop('exchange')
data_dir = kwargs.pop('data_dir')
classify = kwargs.get('classify')
df = load_data(symbols, exchange, timeframe, data_dir, **kwargs)
df = prepare_data(df, symbols[0], True, classify=classify)
train_x, train_y, test_x, test_y = split_dataset(df, classify=classify)
model = create_model(len(symbols), split_ratio=0.85, **kwargs)
history = model.fit(
x = [i for i in train_x],
y = train_y,
epochs=100,
validation_data=(
[i for i in test_x],
test_y
)
).history
plot_history(history, 'acc' if classify else 'loss')
model.save('classify-model.keras' if classify else 'regression-model.keras')
def predict(args: Namespace):
print("Downloading latest data")
download(args)
kwargs = args.__dict__
symbols = kwargs.pop('coins').split(',')
timeframe = kwargs.pop('timeframe')
exchange = kwargs.pop('exchange')
data_dir = kwargs.pop('data_dir')
lookback = kwargs.get('lookback', 30)
classify = kwargs.get('classify')
df = load_data(symbols, exchange, timeframe, data_dir, **kwargs)
df = prepare_data(df, symbols[0], False, )
unix = (df.iloc[-1].name + (df.iloc[-1].name - df.iloc[-2].name)) / 1000
unix = datetime.fromtimestamp(unix)
df = df.iloc[-lookback:].to_numpy()
df = df.reshape(len(symbols), 1, lookback, 1)
model = load_model('classify-model.keras' if classify else 'regression-model.keras')
prediction = model.predict([i for i in df], verbose=False)[0]
if classify:
buy_signal, sell_signal = prediction
if buy_signal == sell_signal:
prediction = 'hold'
else:
prediction = 'buy' if buy_signal > sell_signal else 'sell'
else:
prediction = prediction[0] * 100
prediction = f'{prediction:.4f}%'
print(f"Prediction for {symbols[0]} is {prediction}, prediction time: {unix}")
if __name__ == "__main__":
main()