-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.py
More file actions
107 lines (91 loc) · 3.36 KB
/
bot.py
File metadata and controls
107 lines (91 loc) · 3.36 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
import os
import sys
from copy import deepcopy
import keras
import numpy as np
import tensorflow as tf
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from tradingview_feed import TvDatafeed, Interval
def predict(models, bot_data):
for (i, model) in enumerate(models):
predicted_data = deepcopy(bot_data)
for _ in range(3):
prediction = model.predict([predicted_data])
predicted_data.append(prediction[0])
predicted_data = predicted_data[1:]
cur_price = bot_data[-1][1]
future_price = prediction[0][1]
print(f'{i+1}th model result: ', end="")
if future_price > cur_price:
print('Buy')
else:
print('Sell')
if __name__ == '__main__':
print('Welcome to the TradingHelper bot!')
print('Starting to load the models...')
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
nbeats_model = keras.models.load_model('./model_dumps/nbeats.keras')
nhits_model = keras.models.load_model('./model_dumps/nhits.keras')
rnn_model = keras.models.load_model('./model_dumps/rnn.keras')
lstm_model = keras.models.load_model('./model_dumps/lstm.keras')
transformer_model = keras.models.load_model('./model_dumps/transformer.keras')
print('Loaded the pretrained models successfully.')
try:
bot_option = sys.argv[1]
except IndexError:
bot_option = None
if (bot_option is None) or (bot_option != 'predict'):
print('Currently TradingHelper bot will only help you predict prices.')
print('Requested option is not available, please try again.')
exit(1)
print('Please enter the name of desired models to infer from:')
models_names = input().split()
models = []
for model in models_names:
if model.lower() == 'transformer':
models.append(transformer_model)
elif model.lower() == 'lstm':
models.append(lstm_model)
elif model.lower() == 'rnn':
models.append(rnn_model)
elif model.lower() == 'nbeats':
models.append(nbeats_model)
elif model.lower() == 'nhits':
models.append(nhits_model)
else:
print(f'Currently the requested "{model}" model is not supported by the bot')
print('Please enter the index you want prediction of:')
index = input()
market_data = {
'FX': ['EURUSD', 'XAUUSD', 'GBPUSD', 'USDCAD'],
'CRYPTO': ['BTCUSD', 'ETHUSD'],
'NASDAQ': ['AAPL', 'AMZN'],
}
market = None
for key in market_data:
if index in market_data.get(key):
market = key
break
if market is None:
print('Please enter a valid index to infer from')
exit(1)
# download data for the index
tv = TvDatafeed()
# print(tv.get_hist("CRUDEOIL", "MCX", fut_contract=1))
# print(tv.get_hist("NIFTY", "NSE", fut_contract=1))
data_path = f'./data/{market}_{index}_5min.csv'
tv.get_hist(
index,
market,
interval=Interval.in_5_minute,
n_bars=10,
extended_session=False,
).to_csv(data_path)
data = pd.read_csv(data_path)
# print(data)
bot_data = []
for i in range(10):
bot_data.append([data['open'][i], data['high'][0], data['low'][0], data['close'][i], data['volume'][i]])
if bot_option == 'predict':
predict(models, bot_data)