-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrading.py
245 lines (218 loc) · 9.1 KB
/
trading.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
import alpaca_trade_api as tradeapi
import threading
import time
class PortfolioManager():
def __init__(self):
self.api = tradeapi.REST()
self.r_positions = {}
def format_percent(self, num):
if(str(num)[-1] == "%"):
return float(num[:-1]) / 100
else:
return float(num)
def clear_orders(self):
try:
self.api.cancel_all_orders()
print("All open orders cancelled.")
except Exception as e:
print(f"Error: {str(e)}")
def add_items(self, data):
''' Expects a list of lists containing two items: symbol and position qty/pct
'''
for row in data:
self.r_positions[row[0]] = [row[1], 0]
def percent_rebalance(self, order_style, timeout=60):
print(f"Desired positions: ")
for sym in self.r_positions:
print(f"{sym} - {self.r_positions.get(sym)[0]} of portfolio.")
print()
positions = self.api.list_positions()
account = self.api.get_account()
portfolio_val = float(account.portfolio_value)
for sym in self.r_positions:
price = self.api.get_barset(sym, "minute", 1)[sym][0].c
self.r_positions[sym][0] = int(
self.format_percent(
self.r_positions.get(sym)[0]) * portfolio_val / price)
print(f"Current positions: ")
for position in positions:
print(
f"{position.symbol} - {round(float(position.market_value) / portfolio_val * 100, 2)}% of portfolio.")
print()
self.clear_orders()
print("Clearing extraneous positions.")
for position in positions:
if(self.r_positions.get(position.symbol)):
self.r_positions.get(position.symbol)[1] = int(position.qty)
else:
self.send_basic_order(
position.symbol, position.qty, ("buy", "sell")[
position.side == "long"])
print()
if(order_style == "send"):
for sym in self.r_positions:
qty = self.r_positions.get(
sym)[0] - self.r_positions.get(sym)[1]
self.send_basic_order(sym, qty, ("buy", "sell")[qty < 0])
elif(order_style == "timeout"):
threads = []
for i, sym in enumerate(self.r_positions):
qty = self.r_positions.get(
sym)[0] - self.r_positions.get(sym)[1]
threads.append(
threading.Thread(
target=self.timeout_execution, args=(
sym, qty, ("buy", "sell")[
qty < 0], self.r_positions.get(sym)[0], timeout)))
threads[i].start()
for i in range(len(threads)):
threads[i].join()
elif(order_style == "block"):
threads = []
for i, sym in enumerate(self.r_positions):
qty = self.r_positions.get(
sym)[0] - self.r_positions.get(sym)[1]
threads.append(
threading.Thread(
target=self.confirm_full_execution, args=(
sym, qty, ("buy", "sell")[
qty < 0], self.r_positions.get(sym)[0])))
threads[i].start()
for i in range(len(threads)):
threads[i].join()
def rebalance(self, order_style, timeout=60):
print(f"Desired positions: ")
for sym in self.r_positions:
print(f"{sym} - {self.r_positions.get(sym)[0]} shares.")
print("\n")
positions = self.api.list_positions()
print(f"Current positions: ")
for position in positions:
print(f"{position.symbol} - {position.qty} shares owned.")
print()
self.clear_orders()
print("Clearing extraneous positions.")
for position in positions:
if(self.r_positions.get(position.symbol)):
self.r_positions[position.symbol][1] = int(position.qty)
else:
self.send_basic_order(
position.symbol, position.qty, ("buy", "sell")[
position.side == "long"])
print()
if(order_style == "send"):
for sym in self.r_positions:
qty = int(self.r_positions.get(sym)[
0]) - self.r_positions.get(sym)[1]
self.send_basic_order(sym, qty, ("buy", "sell")[qty < 0])
elif(order_style == "timeout"):
threads = []
for i, sym in enumerate(self.r_positions):
qty = int(self.r_positions.get(sym)[
0]) - self.r_positions.get(sym)[1]
threads.append(
threading.Thread(
target=self.timeout_execution, args=(
sym, qty, ("buy", "sell")[
qty < 0], self.r_positions.get(sym)[0], timeout)))
threads[i].start()
for i in range(len(threads)):
threads[i].join()
elif(order_style == "block"):
threads = []
for i, sym in enumerate(self.r_positions):
qty = int(self.r_positions.get(sym)[
0]) - self.r_positions.get(sym)[1]
threads.append(
threading.Thread(
target=self.confirm_full_execution, args=(
sym, qty, ("buy", "sell")[
qty < 0], self.r_positions.get(sym)[0])))
threads[i].start()
for i in range(len(threads)):
threads[i].join()
def send_basic_order(self, sym, qty, side):
qty = int(qty)
if(qty == 0):
return
q2 = 0
try:
position = self.api.get_position(sym)
curr_pos = int(position.qty)
if((curr_pos + qty > 0) != (curr_pos > 0)):
q2 = curr_pos
qty = curr_pos + qty
except BaseException:
pass
try:
if q2 != 0:
self.api.submit_order(sym, abs(q2), side, "market", "gtc")
try:
self.api.submit_order(sym, abs(qty), side, "market", "gtc")
except Exception as e:
print(
f"Error: {str(e)}. Order of | {abs(qty) + abs(q2)} {sym} {side} | partially sent ({abs(q2)} shares sent).")
return False
else:
self.api.submit_order(sym, abs(qty), side, "market", "gtc")
print(f"Order of | {abs(qty) + abs(q2)} {sym} {side} | submitted.")
return True
except Exception as e:
print(
f"Error: {str(e)}. Order of | {abs(qty) + abs(q2)} {sym} {side} | not sent.")
return False
def confirm_full_execution(self, sym, qty, side, expected_qty):
sent = self.send_basic_order(sym, qty, side)
if(not sent):
return
executed = False
while(not executed):
try:
position = self.api.get_position(sym)
if int(position.qty) == int(expected_qty):
executed = True
else:
print(f"Waiting on execution for {sym}...")
time.sleep(20)
except BaseException:
print(f"Waiting on execution for {sym}...")
time.sleep(20)
print(
f"Order of | {abs(qty)} {sym} {side} | completed. Position is now {expected_qty} {sym}.")
def timeout_execution(self, sym, qty, side, expected_qty, timeout):
sent = self.send_basic_order(sym, qty, side)
if(not sent):
return
output = []
executed = False
timer = threading.Thread(
target=self.set_timeout, args=(
timeout, output))
timer.start()
while(not executed):
if(len(output) == 0):
try:
position = self.api.get_position(sym)
if int(position.qty) == int(expected_qty):
executed = True
else:
print(f"Waiting on execution for {sym}...")
time.sleep(20)
except BaseException:
print(f"Waiting on execution for {sym}...")
time.sleep(20)
else:
timer.join()
try:
position = self.api.get_position(sym)
curr_qty = position.qty
except BaseException:
curr_qty = 0
print(
f"Process timeout at {timeout} seconds: order of | {abs(qty)} {sym} {side} | not completed. Position is currently {curr_qty} {sym}.")
return
print(
f"Order of | {abs(qty)} {sym} {side} | completed. Position is now {expected_qty} {sym}.")
def set_timeout(self, timeout, output):
time.sleep(timeout)
output.append(True)