-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEvolution.py
71 lines (54 loc) · 2.62 KB
/
Evolution.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
""""
This file will evolve the machine to be with the best settings for the market
"""
from fucck import predict_stocks, get_historical_data
from random import randint
from datetime import datetime, timedelta
NUMBER_RANGE = 35
def random_variables():
epochs = randint(10, 30)
batch_size = randint(24, 64)
units = randint(19, 100)
prediction_days = randint(10, 80)
prediction_day = 1
return [epochs, batch_size, units, prediction_days, prediction_day]
def date_minus_date_time(delta_time):
return (datetime.now() - timedelta(delta_time)).strftime('%Y-%m-%d')
def predicting_value_minus_delta_times(ticker, EBUPP, delta_times):
end_day = str(date_minus_date_time(delta_times))
predicted_end_day = str(date_minus_date_time(delta_times+1))
predict_value = predict_stocks([ticker], units=EBUPP[2], prediction_days=EBUPP[3], epochs=EBUPP[0],
batch_size=EBUPP[1], prediction_day=EBUPP[4], end=predicted_end_day)
real_value = get_historical_data(ticker, end=end_day)['close'][-1]
print(predict_value, real_value, end_day)
return predict_value, real_value
def get_ratio_from_all_days(ticker, EBUPP):
real_value, predict_value = 0, 0
huge_ratio = 0
for i in range(NUMBER_RANGE):
predict_value, real_value = predicting_value_minus_delta_times(ticker, EBUPP, i)
ratio_for_this_delta = min(predict_value / real_value, real_value / predict_value)
huge_ratio += ratio_for_this_delta
return huge_ratio / NUMBER_RANGE, real_value, predict_value
def write_in_file(path='Generated_data.txt', data=None):
file = open(path, 'a')
file.write(data)
file.close()
def get_the_best_of_them(ticker):
last_ratio = 0
for i in range(NUMBER_RANGE):
variables_list = random_variables()
ratio, real_value, predict_value = get_ratio_from_all_days(ticker, variables_list)
if ratio > last_ratio:
data = "".join(['\n', 'epochs - ', str(variables_list[0]),
' batch_size - ', str(variables_list[1]),
' units - ', str(variables_list[2]),
' prediction_days - ', str(variables_list[3]),
' prediction_day - ', str(variables_list[4]),
' ratio - ', str(ratio),
' real value - ', str(real_value),
' predict value - ', str(predict_value)[0:5]
])
print(data)
write_in_file(data=data)
last_ratio = ratio