· 6 years ago · Nov 27, 2019, 06:28 PM
1# -*- coding: utf-8 -*-
2"""
3Spyder Editor
4
5This is a temporary script file.
6"""
7import signal
8import requests # step 1
9from time import sleep
10import sys
11class ApiException(Exception):
12 pass
13def signal_handler(signum,frame):
14 global shutdown
15 signal.signal(signal.SIGINT, signal.SIG_DFL)
16shutdown = True
17
18API_KEY= {'X-API-key': 'WHT536T'}
19shutdown = False
20SPEEDBUMP = 0.5
21MAX_VOLUME=5000
22MAX_ORDERS=5
23SPREAD = 0.01
24
25def get_tick(session):
26 resp = session.get('http://localhost:9999/v1/case')
27 if resp.ok:
28 case = resp.json()
29 return case['tick']
30 raise ApiException('Authorization error Please check API key.')
31
32def ticker_bid_ask(session, ticker):
33 payload = {'ticker': ticker}
34 resp = session.get('http://localhost:9999/v1/securities/book', params=payload)
35 if resp.ok:
36 book = resp.json()
37 return book['bids'][0]['price'], book['asks'][0]['price']
38 raise ApiException('Authorization error Please check API key.')
39
40def open_sells(session):
41 resp = session.get('http://localhost:9999/v1/orders?status=OPEN')
42 if resp.ok:
43 open_sells_volume = 0
44 ids = []
45 prices = []
46 order_volumes = []
47 volume_filled = []
48
49 open_orders=resp.json()
50 for order in open_orders:
51 if order['action'] == 'SELL':
52 volume_filled.append(order['quantity_filled'])
53 order_volumes.append(order['quantity'])
54 open_sells_volume = open_sells_volume + order['quantity']
55 prices.append(order['price'])
56 ids.append(order['order_id'])
57 return volume_filled, open_sells_volume, ids, prices, order_volumes
58
59def open_buys(session):
60 resp = session.get('http://localhost:9999/v1/orders?status=OPEN')
61 if resp.ok:
62 open_buys_volume = 0
63 ids = []
64 prices = []
65 order_volumes = []
66 volume_filled = []
67
68 open_orders=resp.json()
69 for order in open_orders:
70 if order['action'] == 'BUY':
71 open_buys_volume = open_buys_volume + order['quantity']
72 volume_filled.append(order['quantity_filled'])
73 order_volumes.append(order['quantity'])
74 prices.append(order['price'])
75 ids.append(order['order_id'])
76 return volume_filled, open_buys_volume, ids, prices, order_volumes
77
78def buy_sell(session, sell_price, buy_price):
79 session.post('http://localhost:9999/v1/orders', params = {'ticker' : 'ALGO',
80 'type': 'LIMIT', 'quantity': MAX_VOLUME, 'price': sell_price, 'action': 'SELL'})
81 session.post('http://localhost:9999/v1/orders', params = {'ticker' : 'ALGO',
82 'type': 'LIMIT', 'quantity': MAX_VOLUME, 'price': buy_price, 'action': 'BUY'})
83
84def re_order(session, number_of_orders, ids, volumes_filled, volumes, price, action):
85 for i in range(number_of_orders):
86 id = ids[i]
87 volume = volumes[i]
88 volume_filled = volumes_filled[i]
89 if(volume_filled != 0):
90 volume = MAX_VOLUME - volume_filled
91
92 deleted = session.delete('http://localhost:9999/v1/orders/{}'.format(id))
93 if(deleted.ok):
94 session.post('http://localhost:9999/v1/orders', params = {'ticker': 'ALGO',
95 'type': 'LIMIT', 'quantity':volume, 'price': price, 'action': action})
96def main():
97 buy_ids = []
98 F buy_prices = []
99 buy_volumes = []
100 volume_filled_buys = []
101 open_buys_volume = 0
102
103 sell_ids = []
104 sell_prices = []
105 sell_volumes = []
106 volume_filled_sells = []
107 open_sells_volume = 0
108
109 single_side_filled = False
110 single_side_transaction_time = 0
111
112 with requests.Session() as s:
113 s.headers.update(API_KEY)
114 tick = get_tick(s)
115
116
117 while tick > 5 and tick < 295 and not shutdown:
118 volume_filled_sells, open_sells_volume, sell_ids, sell_prices, sell_volumes = open_sells(s)
119 volume_filled_buys, open_buys_volume, buy_ids, buy_prices, buy_volumes = open_buys(s)
120 bid_price, ask_price= ticker_bid_ask(s, 'ALGO')
121 if(open_sells_volume == 0 and open_buys_volume == 0):
122 single_side_filled = False
123
124 bid_ask_spread = ask_price - bid_price*
125
126 sell_price = ask_price
127 buy_price = bid_price
128
129 if(bid_ask_spread >= SPREAD):
130 for i in range(MAX_ORDERS):
131 bid_price, ask_price= ticker_bid_ask(s, 'ALGO')
132 sell_price=ask_price
133 buy_price=bid_price
134 buy_sell(s, sell_price, buy_price)
135 sleep(SPEEDBUMP)
136 else:
137 if(not single_side_filled and (open_buys_volume == 0 or open_sells_volume == 0)):
138 single_side_filled = True
139 single_side_transaction_time = tick
140
141 if(open_sells_volume == 0):
142 if(buy_price == bid_price):
143 continue
144 elif(tick - single_side_transaction_time >= 3):
145 next_buy_price = bid_price + .01
146 potential_profit= sell_price - next_buy_price - .02
147 if(potential_profit >= .01 or tick - single_side_transaction_time >= 6):
148 action = 'BUY'
149 number_of_orders = len(buy_ids)
150 buy_price = bid_price + .01
151 price = buy_price
152 ids = buy_ids
153 volumes = buy_volumes
154 volumes_filled = volume_filled_buys
155
156 re_order(s, number_of_orders, ids, volumes_filled, volumes, price, action)
157 sleep(SPEEDBUMP)
158 elif(open_buys_volume == 0):
159 if(sell_price == ask_price):
160 continue
161 elif(tick - single_side_transaction_time >= 3):
162 next_sell_price= ask_price - .01
163 potential_profit= next_sell_price - buy_price - .02
164
165 if(potential_profit >= .01 or tick - single_side_transaction_time >= 6):
166 action = 'SELL'
167 number_of_orders = len(sell_ids)
168 sell_price = ask_price - .01
169 price = sell_price
170 ids = sell_ids
171 volumes = sell_volumes
172 volumes_filled = volume_filled_sells
173
174 re_order(s, number_of_orders, ids, volumes_filled, volumes, price, action)
175 sleep(SPEEDBUMP)
176 tick = get_tick(s)
177
178if __name__ == '__main__':
179 signal.signal(signal.SIGINT, signal_handler)
180 main()