-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.py
154 lines (138 loc) · 4.53 KB
/
scheduler.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import time
import math
from collections import deque
import threading
import socketio
from time_series.garch import (
forecast_garch,
load_garch_model,
train_garch_model,
)
from time_series.arima import (
forecast_arima,
load_arima_model,
train_arima_model,
)
from time_series.data import fetch_data
sio = socketio.Client(engineio_logger=True)
with open('./metadata/TICKER.txt', 'r') as file:
ticker = file.read().strip()
port = 5000
host = f'http://127.0.0.1:{port}'
data_queue = None
sentiment = None
decision_queue = deque(maxlen=10)
garch_model = None
garch_pred_queue = deque(maxlen=10)
arima_model = None
arima_pred_queue = deque(maxlen=10)
holding = True
trades = []
total_pnl = 0
def initialize_ticker(ticker: str):
global garch_model, arima_model, data_queue, sentiment, holding
holding = False
data = fetch_data(ticker)
garch_model = load_garch_model(ticker)
arima_model = load_arima_model(ticker)
data_queue = deque(data[-10:], maxlen=10)
with open('./metadata/OVERALL_SENTIMENT.txt', 'r') as file:
sentiment = file.read().strip()
def make_stock_decision(
current_price: float,
arima_prediction: float,
garch_prediction: float,
sentiment_score: str,
threshold: float = 0.01
):
global holding
average_prediction = (arima_prediction + garch_prediction) / 2
print(average_prediction, current_price)
if sentiment_score == 'positive':
if average_prediction > current_price * (1 + threshold) and not holding:
holding = True
return 'buy'
elif holding and average_prediction < current_price * (1 - threshold):
holding = False
return 'sell'
else:
return 'hold'
elif sentiment_score == 'neutral':
if average_prediction > current_price and not holding:
holding = True
return 'buy'
elif holding and average_prediction < current_price:
holding = False
return 'sell'
else:
return 'hold'
elif sentiment_score == 'negative':
if holding:
holding = False
return 'sell'
else:
return 'hold'
else:
raise ValueError("Invalid sentiment score. It must be 'positive', 'neutral', or 'negative'.")
def scheduled_job(ticker: str):
global data_queue, pred_queue, decision_queue
new_data = fetch_data(ticker)
data_queue.append(new_data[-1])
garch_model = train_garch_model(new_data)
garch_pred = forecast_garch(data_queue, garch_model)
garch_pred_queue.append(garch_pred)
arima_model = train_arima_model(new_data)
arima_pred = forecast_arima(data_queue, arima_model)
arima_pred_queue.append(arima_pred)
decision = make_stock_decision(
current_price=data_queue[-1],
arima_prediction=arima_pred,
garch_prediction=garch_pred,
sentiment_score=sentiment,
)
decision_queue.append(decision)
@sio.on('update_ticker', namespace='/schedule')
def update_ticker(data):
global ticker
ticker = data['data']
initialize_ticker(ticker)
print('Updating tracked ticker to ', ticker)
def threaded_worker():
global data_queue, sentiment, garch_pred_queue, arima_pred_queue, total_pnl
while True:
scheduled_job(ticker)
garch_pred = garch_pred_queue[-1]
arima_pred = arima_pred_queue[-1]
decision = decision_queue[-1] if decision_queue else 'hold'
current_price = data_queue[-2]
previous_price = data_queue[-3]
if decision == 'buy':
total_pnl -= current_price - previous_price
elif decision == 'sell':
total_pnl += current_price - previous_price
print(f"Current PNL: {total_pnl}")
sio.emit(
'inference',
{
'current': current_price,
'garch': garch_pred,
'arima': arima_pred,
'decision': decision
},
namespace='/schedule'
)
time.sleep(60)
connected = False
if __name__ == "__main__":
initialize_ticker(ticker)
while not connected:
try:
sio.connect(host, namespaces=['/schedule'])
print("Socket established")
connected = True
except Exception as ex:
print("Failed to establish initial connnection to server:", type(ex).__name__)
time.sleep(2)
job_thread = threading.Thread(target=threaded_worker, daemon=True)
job_thread.start()
sio.wait()