-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcase_2v1.py
290 lines (249 loc) · 7.92 KB
/
case_2v1.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import requests as r
import time
import re
from dotenv import load_dotenv
import os
load_dotenv()
ritKey = os.getenv('RITKEY')
API_KEY = {'X-API-key': ritKey}
case = 'http://localhost:9999/v1/case'
order = "http://localhost:9999/v1/orders"
news = "http://localhost:9999/v1/news"
orderLimt = 5
##########String interpelation version#############
class ApiException(Exception):
pass
# Get case status
def caseStatus(session):
caseInfo = session.get(case)
rDic = caseInfo.json()
status = rDic['status']
tick = rDic['tick']
if status == 'ACTIVE' and tick > 1:
return True
else:
time.sleep(2)
print('Waiting')
global totalSpeedBumps
global numberOfOrders
totalSpeedBumps = 0
numberOfOrders = 0
return caseStatus(session)
# Get security order book
def getOrderBook(session, ticker):
book = 'http://localhost:9999/v1/securities/book'
payload = {'ticker': ticker}
resp = session.get(book, params=payload)
try:
if resp.ok:
book = resp.json()
return book['bids'][0], book['asks'][0]
raise ApiException('Auth error, please check API key')
except:
caseStatus(session)
def getNews(session, since):
payload = {'since': since}
request = session.get(news, params=payload)
if request.ok:
return request.json()
def drawSame(actual, expected):
length = len(actual)
if length == 32:
actualQuant = int(actual[20:22])*-1
else:
actualQuant = int(actual[22:24])*-1
# Expected is draw
length = len(expected)
if length == 25:
expectedQuant = int(expected[15:17])*-1
else:
expectedQuant = int(expected[15:17])*-1
if abs(actualQuant-expectedQuant) < 5:
print('too low')
return False, False
action = 'Buy' if actualQuant < expectedQuant else 'Sell'
eiaDic = [actualQuant, expectedQuant]
return action, eiaDic
def buildSame(actual, expected):
# Actual is build
length = len(actual)
if length == 33:
actualQuant = int(actual[21:23])
else:
actualQuant = int(actual[23:25])
length = len(expected)
# Expected is build
if length == 25:
expectedQuant = int(expected[15:17])
else:
expectedQuant = int(expected[15:18])
if abs(actualQuant-expectedQuant) < 5:
print('too low')
time.sleep(1)
return False, False
action = 'Buy' if actualQuant < expectedQuant else 'Sell'
eiaDic = [actualQuant, expectedQuant]
return action, eiaDic
def eiaNews(news):
# Extract information from EIA reports
[actual, expected] = news.split('VS')
dPattern = re.compile(r'DRAW')
bPattern = re.compile(r'BUILD')
if dPattern.search(actual) and dPattern.search(expected):
return drawSame(actual, expected)
if bPattern.search(actual) and bPattern.search(expected):
return buildSame(actual, expected)
# Handle expected EIA
if dPattern.search(actual):
# Actual is draw
length = len(actual)
if length == 33:
actualQuant = int(actual[21:23])*-1
else:
actualQuant = int(actual[21:24])*-1
else:
# Actual is build
length = len(actual)
if length == 35:
actualQuant = int(actual[22:25])
else:
actualQuant = int(actual[22:24])
# Handle expected EIA
if dPattern.search(expected):
# Excpected is draw
length = len(expected)
if length == 25:
expectedQuant = int(expected[15:17])*-1
else:
expectedQuant = int(expected[15:18])*-1
else:
# Expected is build
length = len(expected)
if length == 26:
expectedQuant = int(expected[16:18])
else:
expectedQuant = int(expected[16:19])
eiaDic = [actualQuant, expectedQuant]
# print(eiaDic)
if abs(actualQuant-expectedQuant) < 5:
print('too low')
time.sleep(1)
return False, False
action = 'Buy' if eiaDic[0] < 0 and eiaDic[1] > 0 else 'Sell'
return action, eiaDic
def newsFilter(news, dic):
# Filter news reports to EIA
wPattern = re.compile(r'WEEK')
if wPattern.search(news['headline']):
if dic.get(news['news_id']) is None:
return eiaNews(news['body'])
else:
print('Key already exists')
time.sleep(1)
return False, False
else:
print('----')
print('non-eia')
print('----')
time.sleep(1)
return False, False
# return globalNews(s, news['body'], news['news_id'])
def getTime(s, info):
caseInfo = s.get(case)
rDic = caseInfo.json()
return rDic[info]
def calculateDelta(eiaDic, s):
# Calculate the delta of the futures
[actual, expected] = eiaDic
# currentPrice =
spotDelta = (expected-actual)/10
period = getTime(s, 'period')
ticker = getTime(s, 'tick')
if period == 1:
exp = 2/12
else:
exp = 1/12
futureDelta = spotDelta*((1+0.015)**exp)
currentPrice = getOrderBook(s, 'CL-2F')[1]['price']
return currentPrice+futureDelta
# Main function
def main():
with r.Session() as s:
dic = {}
flag = True
s.headers.update(API_KEY)
while flag:
flag = caseStatus(s)
news = getNews(s, 0)
action, eiaDic = newsFilter(news[0], dic)
if action:
print(action)
exitPrice = calculateDelta(eiaDic, s)
dic[news[0]['news_id']] = [action, f"Exit at {exitPrice}"]
print(dic[news[0]['news_id']])
time.sleep(10)
num_processes = 8
if __name__ == '__main__':
main()
# testArr = ["WEEK 6 CL ACTUAL DRAW 99 MLN BBLS VS FORECAST DRAW 99 MLN BBLS",
# "WEEK 6 CL ACTUAL DRAW 99 MLN BBLS VS FORECAST DRAW 9 MLN BBLS",
# "WEEK 6 CL ACTUAL DRAW 99 MLN BBLS VS FORECAST BUILD 99 MLN BBLS",
# "WEEK 6 CL ACTUAL DRAW 99 MLN BBLS VS FORECAST BUILD 9 MLN BBLS",
# "WEEK 6 CL ACTUAL DRAW 9 MLN BBLS VS FORECAST DRAW 99 MLN BBLS",
# "WEEK 6 CL ACTUAL DRAW 9 MLN BBLS VS FORECAST DRAW 9 MLN BBLS",
# "WEEK 6 CL ACTUAL DRAW 9 MLN BBLS VS FORECAST BUILD 99 MLN BBLS",
# "WEEK 6 CL ACTUAL DRAW 9 MLN BBLS VS FORECAST BUILD 9 MLN BBLS",
# "WEEK 6 CL ACTUAL BUILD 99 MLN BBLS VS FORECAST DRAW 99 MLN BBLS",
# "WEEK 6 CL ACTUAL BUILD 99 MLN BBLS VS FORECAST DRAW 9 MLN BBLS",
# "WEEK 6 CL ACTUAL BUILD 99 MLN BBLS VS FORECAST BUILD 99 MLN BBLS",
# "WEEK 6 CL ACTUAL BUILD 99 MLN BBLS VS FORECAST BUILD 9 MLN BBLS",
# "WEEK 6 CL ACTUAL BUILD 9 MLN BBLS VS FORECAST DRAW 99 MLN BBLS",
# "WEEK 6 CL ACTUAL BUILD 9 MLN BBLS VS FORECAST DRAW 9 MLN BBLS",
# "WEEK 6 CL ACTUAL BUILD 9 MLN BBLS VS FORECAST BUILD 99 MLN BBLS",
# "WEEK 6 CL ACTUAL BUILD 9 MLN BBLS VS FORECAST BUILD 9 MLN BBLS"]
# temp = []
# for i in range(len(testArr)):
# news = [
# {
# "news_id": i,
# "period": 0,
# "tick": 0,
# "ticker": "string",
# "headline": "WEEK",
# "body": testArr[i]
# }
# ]
# temp.append(newsFilter(news[0]))
# print(temp)
# [(False, False),
# ('Buy', [99, 9], 'G'),
# ('Buy', [-99, 99], 'G'),
# ('Buy', [-99, 9], 'G'),
# ('Sell', [9, 99], 'G'),
# (False, False),
# ('Buy', [-9, 99]),
# ('Buy', [-9, 9]),
# ('Sell', [99, -99]),
# ('Sell', [99, -9]),
# (False, False),
# ('Sell', [-99, -9]),
# ('Sell', [9, -99]),
# ('Sell', [9, -9]),
# ('Buy', [-9, -99]),
# (False, False)]
# [(False, False),
# ('Sell', [-99, -9]),
# ('Buy', [-99, 99]),
# ('Buy', [-99, 9]),
# ('Buy', [-9, -99]),
# (False, False),
# ('Buy', [-9, 99]),
# ('Buy', [-9, 9]),
# ('Sell', [99, -99]),
# ('Sell', [99, -9]),
# (False, False),
# ('Buy', [99, 9]),
# ('Sell', [9, -99]),
# ('Sell', [9, -9]),
# ('Sell', [9, 99]),
# (False, False)]