forked from willcruse/hackathonEG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
122 lines (100 loc) · 4.38 KB
/
main.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
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
import numpy as np
import requests
import pickle
import time
import getData
import statisticalMethods
from sklearn.impute import SimpleImputer
import predictions
def get_returns(t):
r = requests.get("http://egchallenge.tech/marketdata/epoch/" + str(t))
# The data is formatted as a list of dictionaries
# We pass it to the DataFrame constructor to create a DataFrame,
# then select the epoch_return column (returned as a Pandas Series)
return pd.DataFrame(r.json())["epoch_return"]
def save_returns_df(df_to_save):
with open("example_returns_df", "wb") as f:
# pickle.dump saves a python object to a file,
# in a way which can be later restored with pickle.load
pickle.dump(df_to_save, f)
def create_returns_df(target_epoch=3000):
try:
with open("example_returns_df", "rb") as f:
return pickle.load(f)
except FileNotFoundError:
# This will happen the first time we run the program,
# because the file won't exist yet
# So we have to download the data and put it in a DataFrame
ret = {}
for t in range(target_epoch):
if (t % 20 == 0):
print("Downloading returns for epoch " + str(t))
ret[t] = get_returns(t)
# When fed a dictionary { colname_i : column_i },
# the DataFrame constructer will create columns
# with name colname_i and data column_i
ret = pd.DataFrame(ret)
save_returns_df(ret)
return ret
def update_returns_df(input_df, target_epoch=None):
current_epoch = requests.get('http://egchallenge.tech/epoch').json()['current_epoch']
print("Current epoch:", current_epoch)
# If target_epoch is None then we want to bring the dataframe fully up-to-date
if target_epoch is None or current_epoch < target_epoch:
last_epoch_to_get = current_epoch
else:
last_epoch_to_get = target_epoch
last_downloaded_epoch = max(input_df.columns)
while last_downloaded_epoch < last_epoch_to_get:
print("Downloading returns up to epoch ", last_epoch_to_get)
for t in range(last_downloaded_epoch + 1, last_epoch_to_get + 1):
if (t % 20 == 0):
print("Downloading returns for epoch ", t)
input_df[t] = get_returns(t)
# If we had to make a large update, it's possible that the epoch advanced
# in the meantime
# In this case we may want to download the returns for the new epochs as well
last_downloaded_epoch = last_epoch_to_get
current_epoch = requests.get('http://egchallenge.tech/epoch').json()['current_epoch']
print("Current epoch:", current_epoch)
if target_epoch is None or current_epoch < target_epoch:
last_epoch_to_get = current_epoch
# Save the results so we don't have to re-download them next time
save_returns_df(input_df)
timestamp = requests.get('http://egchallenge.tech/epoch').json()['unix_timestamp']
next_epoch_in = max(60.0 - (time.time() - timestamp), 0) + 1.0
return next_epoch_in
dataFrame = create_returns_df()
login_res = requests.post('http://egchallenge.tech/team/login', json={'team_name': 'Keith', 'password': 'hunter2'}).json()
print(login_res)
token = login_res['token']
print(f'token = {token}')
while True:
update_returns_df(dataFrame)
startEpoch = getData.getCurrentEpoch()
epochPrediction = getData.getPredictionEpoch()
dataLatest = getData.getMarketDataLatest()
results = []
dropped = dataFrame.dropna(axis=1)
my_imputer=SimpleImputer()
df = my_imputer.fit_transform(dataFrame)
df = pd.DataFrame(df)
y = statisticalMethods.simpMovingAverage(df, 40)
X = df.columns.values.reshape(-1, 1)
tree = RandomForestRegressor(random_state=1)
tree.fit(X, y)
prediction = tree.predict(np.asarray(X).reshape(-1, 1))
for index, row in df.iterrows():
isTrading = dataLatest[index]['is_trading']
if isTrading:
results.append({
'instrument_id': int(index + 1),
'predicted_return': float(prediction[startEpoch-2][index])
})
print("Results built")
statusCode = predictions.sendPredictions(np.asarray(results).tolist(), token)
print("Predictions sent with status code: " + str(statusCode))
while startEpoch == getData.getCurrentEpoch():
a = 1+1