· 6 years ago · Mar 06, 2020, 11:14 PM
1import time
2import signal
3import requests
4from time import sleep
5import sys
6
7
8
9class ApiException(Exception):
10 pass
11
12
13def signal_handler(signum, frame):
14 global shutdown
15 signal.signal(signal.SIGINT, signal.STG_DFL)
16 shutdown = True
17
18
19API_KEY = {'X-API-Key': 'JEQTMLCS'}
20shutdown = False
21
22
23MAX_VOLUME = 1000
24
25MAX_ORDERS = 5
26
27SPREAD = .01
28
29
30def get_tick(session):
31 resp = session.get('http://localhost:9999/v1/case')
32 if resp.ok:
33 case = resp.json()
34 return case ['tick']
35 raise ApiException('Authorization error Please check API key.')
36
37
38def ticker_bid_ask(session, ticker):
39 payload = {'ticker': ticker}
40 resp = session.get('http://localhost:9999/v1/securities/book', params=payload)
41 if resp.ok:
42 book = resp.json()
43 return book['bids'][0]['price'], book['asks'][0]['price']
44 raise ApiException('Authorization error Please check API key')
45
46
47number_of_orders = 0
48total_speedbumps = 0
49def speedbump(transaction_time):
50
51 global total_speedbumps
52 global number_of_orders
53
54 order_speedbump = -transaction_time + 1/MAX_ORDERS
55
56 total_speedbumps = total_speedbumps + order_speedbump
57
58 number_of_orders = number_of_orders + 1
59
60 sleep(abs(total_speedbumps/number_of_orders))
61
62
63def open_sells(session):
64 resp = session.get('http://localhost:9999/v1/orders?status=OPEN')
65 if resp.ok:
66 open_sells_volume = 0
67 ids = []
68 prices = []
69 order_volumes = []
70 volume_filled = []
71
72 open_orders = resp.json()
73 for order in open_orders:
74 if order['action'] == 'SELL':
75 volume_filled.append(order['quantity_filled'])
76 order_volumes.append(order['quantity'])
77 open_sells_volume = open_sells_volume + order['quantity']
78 prices.append(order['price'])
79 ids.append(order['order_id'])
80 return volume_filled, open_sells_volume , ids, prices, order_volumes
81
82
83def open_buys(session):
84 resp = session.get('http://localhost:9999/v1/orders?status=OPEN')
85 if resp.ok:
86 open_buys_volume = 0
87 ids = []
88 prices = []
89 order_volumes = []
90 volume_filled = []
91
92 open_orders = resp.json()
93 for order in open_orders:
94 if order['action'] == 'BUY':
95 open_buys_volume = open_buys_volume + order['quantity']
96 volume_filled.append(order['quantity_filled'])
97 order_volumes.append(order['quantity'])
98 prices.append(order['price'])
99 ids.append(order['order_id'])
100
101 return volume_filled, open_buys_volume, ids, prices, order_volumes
102
103
104def buy_sell(session, sell_price, buy_price):
105 for i in range(MAX_ORDERS):
106 session.post('http://localhost:9999/v1/orders', params = {'ticker': 'ALG',
107 'type': 'LIMIT', 'quantity': MAX_VOLUME,'price': sell_price, 'action': 'SELL'})
108 session.post('http://localhost:9999/v1/orders', params = {'ticker' : 'ALG',
109 'type': 'LIMIT', 'quantity': MAX_VOLUME, 'price': buy_price, 'action': 'BUY'})
110
111
112def re_order(session, number_of_orders, ids, volumes_filled, volumes, price, action):
113 for i in range(number_of_orders):
114 id = ids[i]
115 volume = volumes[i]
116 volume_filled = volumes_filled[i]
117
118 if(volume_filled != 0):
119 volume = MAX_VOLUME - volume_filled
120
121
122 deleted = session.delete('http://localhost:9999/v1/orders/{}'.format(id))
123 if(deleted.ok):
124 session.post('http://localhost:9999/v1/orders', params = {'ticker': 'ALG',
125 'type': 'LIMIT' , 'quantity': volume, 'price': price, 'action': action})
126
127def main():
128
129 buy_ids = []
130 buy_prices = []
131 buy_volumes = []
132 volume_filled_buys = []
133 open_buys_volume = 0
134
135
136 sell_ids = []
137 sell_prices = []
138 sell_volumes = []
139 volume_filled_sells = []
140 open_sells_volume = 0
141
142
143 single_side_filled = False
144 single_side_transaction_time = 0
145
146
147 with requests.Session() as s:
148 s.headers.update(API_KEY)
149 tick = get_tick(s)
150
151
152 while tick > 5 and tick < 295 and not shutdown:
153
154 volume_filled_sells, open_sells_volume, sell_ids, sell_prices, sell_volumes = open_sells(s)
155 volume_filled_buys, open_buys_volume, buy_ids, buy_prices, buy_volumes = open_buys(s)
156 bid_price, ask_price = ticker_bid_ask(s, 'ALG')
157
158
159 if(open_sells_volume == 0 and open_buys_volume == 0):
160
161 single_side_filled = False
162
163
164 bid_ask_spread = ask_price - bid_price
165
166
167 sell_price = ask_price
168 buy_price = bid_price
169
170 start = time.time()
171
172 if(bid_ask_spread >= SPREAD):
173
174 buy_sell(s, sell_price, buy_price)
175 transaction_time = time.time() - start
176 speedbump(transaction_time)
177
178
179 else:
180
181 if(not single_side_filled and (open_buys_volume == 0 or open_sells_volume == 0)):
182 single_side_filled = True
183 single_side_transaction_time = tick
184
185
186 if(open_sells_volume == 0):
187
188 if(buy_price == bid_price):
189 continue
190
191
192 elif(tick - single_side_transaction_time >= 3):
193
194 next_buy_price = bid_price + .01
195 potential_profit = sell_price - next_buy_price - .02
196
197 start = time.time()
198
199 if(potential_profit >= .01 or tick - single_side_transaction_time >= 6):
200 action = 'BUY'
201 number_of_orders = len(buy_ids)
202 buy_price = bid_price +.01
203 price = buy_price
204 ids = buy_ids
205 volumes = buy_volumes
206 volumes_filled = volume_filled_buys
207
208
209 re_order(s, number_of_orders, ids, volumes_filled, volumes, price, action)
210 transaction_time = time.time() - start
211 speedbump(transaction_time)
212
213
214 elif(open_buys_volume == 0):
215
216 if(sell_price == ask_price):
217 continue
218
219
220 elif(tick - single_side_transaction_time >= 3):
221
222 next_sell_price = ask_price - .01
223 potential_profit = next_sell_price - buy_price - .02
224
225 start = time.time()
226
227 if(potential_profit >= .01 or tick - single_side_transaction_time >= 6):
228 action = 'SELL'
229 number_of_orders = len(sell_ids)
230 sell_price = ask_price - .01
231 price = sell_price
232 ids = sell_ids
233 volumes = sell_volumes
234 volumes_filled = volume_filled_sells
235
236
237 re_order(s, number_of_orders, ids, volumes_filled, volumes, price, action)
238 transaction_time = time.time() - start
239 speedbump(transaction_time)
240
241
242 tick = get_tick(s)
243
244if __name__ == '__main__':
245 signal.signal(signal.SIGINT, signal_handler)
246 main()