· 6 years ago · Nov 27, 2019, 07:18 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': '6QL1Q5PO'}
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 for i in range(MAX_ORDERS):
80 bid_price, ask_price= ticker_bid_ask(session, 'ALGO')
81 sell_price = int(ask_price)
82 buy_price = int(bid_price)
83 session.post('http://localhost:9999/v1/orders', params = {'ticker' : 'ALGO',
84 'type': 'LIMIT', 'quantity': MAX_VOLUME, 'price': sell_price, 'action': 'SELL'})
85 session.post('http://localhost:9999/v1/orders', params = {'ticker' : 'ALGO',
86 'type': 'LIMIT', 'quantity': MAX_VOLUME, 'price': buy_price, 'action': 'BUY'})
87
88def re_order(session, number_of_orders, ids, volumes_filled, volumes, price, action):
89 for i in range(number_of_orders):
90 id = ids[i]
91 volume = volumes[i]
92 volume_filled = volumes_filled[i]
93 if(volume_filled != 0):
94 volume = MAX_VOLUME - volume_filled
95
96 deleted = session.delete('http://localhost:9999/v1/orders/{}'.format(id))
97 if(deleted.ok):
98 session.post('http://localhost:9999/v1/orders', params = {'ticker': 'ALGO',
99 'type': 'LIMIT', 'quantity':volume, 'price': price, 'action': action})
100def main():
101 buy_price=0
102 buy_ids = []
103 buy_prices = []
104 buy_volumes = []
105 volume_filled_buys = []
106 open_buys_volume = 0
107
108 sell_ids = []
109 sell_prices = []
110 sell_volumes = []
111 volume_filled_sells = []
112 open_sells_volume = 0
113
114 single_side_filled = False
115 single_side_transaction_time = 0
116
117 with requests.Session() as s:
118 s.headers.update(API_KEY)
119 tick = get_tick(s)
120
121
122 while tick > 5 and tick < 295 and not shutdown:
123 volume_filled_sells, open_sells_volume, sell_ids, sell_prices, sell_volumes = open_sells(s)
124 volume_filled_buys, open_buys_volume, buy_ids, buy_prices, buy_volumes = open_buys(s)
125 bid_price, ask_price= ticker_bid_ask(s, 'ALGO')
126 if(open_sells_volume == 0 and open_buys_volume == 0 or 1==1):
127 single_side_filled = False
128
129 bid_ask_spread = ask_price - bid_price
130
131 sell_price = int(ask_price)
132 buy_price = int(bid_price)
133 if(bid_ask_spread >= SPREAD):
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()