-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
333 lines (261 loc) · 11.2 KB
/
api.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from datetime import datetime
import math
from multiprocessing import Process
from multiprocessing.managers import DictProxy
from time import sleep
from typing import Callable
from alpaca.trading.client import TradingClient
from alpaca.trading.stream import TradingStream
from alpaca.common import RawData
from alpaca.broker.client import Asset, Order
from alpaca.trading.requests import MarketOrderRequest
from alpaca.trading.enums import OrderSide, TimeInForce
import requests
import yfinance
from env import alpacaId, alpacaSecret
from sheets import getTransactionJournalRow, insertRowAtTop, log, logTransaction, read, sort, write
# Init client
print("Initializing Alpaca client...")
tradingClient = TradingClient(alpacaId, alpacaSecret, paper=True)
tradeCallbacks: list[Callable[[RawData, DictProxy], None]] = []
# Set up stream
async def updateHandler(data: RawData) -> None:
print("Stream update received!")
# Trade callbacks is sometimes a single function, so we need to convert it to a list
# Don't know how it ends up as a single function, but it does
global tradeCallbacks, sharedData
if tradeCallbacks is not list:
tradeCallbacks = [tradeCallbacks]
# Run callbacks
print("Running trade callbacks...")
try:
for callback in tradeCallbacks:
# I have no clue why this is necessary, but it is
while type(callback) is list:
if len(callback) > 0:
callback = callback[0]
callback(data, sharedData)
except Exception as e:
print("Error running trade callbacks: " + str(e))
try:
eventType = data.event
order: Order = data.order
id = order.id
symbol = order.symbol
side = order.side
shares = order.qty if order.filled_qty == 0 else order.filled_qty
logTransaction(symbol, id, str(eventType) + "-" + ("BUY" if side == OrderSide.BUY else "SELL"), \
float(shares) if shares is not None else "N/A", \
float(order.filled_avg_price) if order.filled_avg_price is not None else "N/A")
return
if eventType == "fill":
if side == OrderSide.BUY:
rowIndex = getTransactionJournalRow(symbol)
if rowIndex is not None:
log("Updating transaction journal...")
# Update journal
entry = read("Journal!A" + str(rowIndex) + ":D" + str(rowIndex))[0]
# Update buy price
avgPrice = (float(entry[1]) * float(entry[3]) + order.filled_avg_price * shares) \
/ (float(entry[3]) + shares)
entry[1] = avgPrice
entry[3] = float(entry[3]) + shares
write("Journal!A" + str(rowIndex) + ":D" + str(rowIndex), [entry])
log("Transaction journal updated!")
else:
log("Adding new entry to journal!")
# Add new entry
insertRowAtTop("743025071")
write("Journal!A2:K2", \
[[symbol, order.filled_avg_price, datetime.now(), shares, "", "", "10/14/2173 0:00:00", \
"=EQ(D2,G2)", '=IF(H2,F2-C2,"")', '=IF(H2,E2-B2,"")', '=J2>0']])
elif side == OrderSide.SELL:
rowIndex = getTransactionJournalRow(symbol)
if rowIndex is not None:
log("Updating transaction journal...")
# Update journal
entry = read("Journal!A" + str(rowIndex) + ":G" + str(rowIndex))[0]
sharesSold = entry[6]
if sharesSold == "":
sharesSold = 0
# Update sell price
if entry[4] == "":
entry[4] = order.filled_avg_price
else:
prevPrice = float(entry[4])
entry[4] = (prevPrice * sharesSold + order.filled_avg_price * shares) \
/ (sharesSold + shares)
# Update shares sold
entry[6] = float(sharesSold) + shares
# Update sell date
entry[5] = datetime.now()
write("Journal!A" + str(rowIndex) + ":H" + str(rowIndex), [entry])
log("Transaction journal updated!")
# Sort sheet so unfilled transactions stay at top
sort("743025071", 5)
except Exception as e:
print("Error handling stream update: " + str(e))
def initStream(callbacks: list[Callable[[RawData, DictProxy], None]], sharedDict: DictProxy | None) -> None:
print("Initializing Alpaca stream...")
global tradeCallbacks
tradeCallbacks = callbacks
print("Trade callbacks set!")
global sharedData
sharedData = sharedDict
tradingStream = TradingStream(alpacaId, alpacaSecret, paper=True)
tradingStream.subscribe_trade_updates(updateHandler)
print("Alpaca stream running...")
tradingStream.run()
print("Alpaca stream stopping...")
tradingStream.stop()
def startStreamProcess(sharedDict: DictProxy = None) -> None:
process = Process(target=initStream, args=(tradeCallbacks, sharedDict))
process.start()
print("Alpaca stream process started! Exit code:", process.exitcode)
if __name__ == "__main__":
process = startStreamProcess()
while True:
try:
sleep(1)
except KeyboardInterrupt:
print("Exiting...")
process.terminate()
exit()
print("Alpaca client initialized!")
def getBuyingPower() -> float:
return float(tradingClient.get_account().buying_power)
def getEquity() -> float:
return float(tradingClient.get_account().equity)
def getPosition(symbol: str) -> float:
allPositions = tradingClient.get_all_positions()
for position in allPositions:
if(position.symbol == symbol):
return float(position.qty)
return 0
def getSecurity(symbol: str) -> Asset | RawData:
return tradingClient.get_asset(symbol)
def getOpenOrders() -> list[Order] | RawData:
return tradingClient.get_orders()
def placeBuyOrder(symbol: str, shares: float) -> bool:
log("Attempting to place buy order for " + str(shares) + " shares of " + symbol + "...")
# Check if shares is valid
if(shares <= 0):
log("Invalid number of shares!")
return False
# Check if we can trade this security
security = getSecurity(symbol)
if(not security.tradable):
log("Security " + symbol + " is not tradable!")
return False
# Fetch the price from yfinance
price = float(yfinance.Ticker(symbol).info['ask'])
orderCost = price * shares
print("Order cost: $" + str(round(orderCost, 2)) + " ($" + str(round(price, 2)) + " * " + str(shares) + ")")
if orderCost < 1:
log("Order cost is less than $1! Skipping...")
return False
# Check if we have enough money
balance = getBuyingPower()
print("Balance: $" + str(balance))
if(balance < orderCost):
shares = balance / price
orderCost = price * shares
log("Insufficient funds! Buying " + str(shares) + " shares instead...")
if orderCost < 1:
log("Order cost is less than $1! Skipping...")
return False
# Place order
log("Placing order for " + str(shares) + " shares of " + symbol + " for a total cost of $" + str(orderCost) + "...")
# Generate order data
# GTC is Good Til Cancelled
# We use DAY so we can have fractional orders
orderData = MarketOrderRequest(symbol=symbol, qty=shares, side=OrderSide.BUY, time_in_force=TimeInForce.DAY)
# Submit order
order = tradingClient.submit_order(orderData)
log("Order placed!")
return True
def placeSellOrder(symbol: str, shares: float) -> bool:
log("Attempting to place sell order for " + str(shares) + " shares of " + symbol + "...")
if(shares <= 0):
log("Invalid number of shares!")
return False
security = getSecurity(symbol)
if(not security.tradable):
log("Security " + symbol + " is not tradable!")
return False
# Check if we have enough shares
position = getPosition(symbol)
log("Position: " + str(position))
if(position < shares):
log("Insufficient shares!")
return False
# Fetch price
price = int(yfinance.Ticker(symbol).info['bid'])
# Place order
log("Placing order for " + str(object=shares) + " shares of " + symbol + " for a total value of $" + str(round(price * shares, 2)) + "...")
# Generate order data
# GTC is Good Til Cancelled
# We use DAY so we can have fractional orders
orderData = MarketOrderRequest(symbol=symbol, qty=shares, side=OrderSide.SELL, time_in_force=TimeInForce.DAY)
# Submit order
order = tradingClient.submit_order(orderData)
log("Order placed!")
return True
def getCryptoPair(paySymbol: str, receiveSymbol: str, tryFlip: bool = True) -> float | None:
symbol = paySymbol + "/" + receiveSymbol
url = "https://data.alpaca.markets/v1beta3/crypto/us/latest/quotes?symbols=" + symbol
headers = {"accept": "application/json"}
try:
response = requests.get(url, headers=headers)
except requests.exceptions.RequestException as e:
print("Error fetching crypto pair", symbol, ":", e)
return None
json = response.json()
try:
return json["quotes"][symbol]["ap"]
except KeyError:
# We may want to remove this. It's unclear if we can go backwards
if tryFlip:
return 1 / getCryptoPair(receiveSymbol, paySymbol, False)
return None
def buyCrypto(buySymbol: str, paySymbol: str, payAmt: float | None = None) -> None:
if payAmt is None:
if paySymbol == "USD":
payAmt = getBuyingPower()
else:
payAmt = getPosition(paySymbol + "USD")
else:
if paySymbol == "USD":
payPosition = getBuyingPower()
else:
payPosition = getPosition(paySymbol + "USD")
print("Buying", buySymbol, "with", payAmt, paySymbol)
# Check if we have enough of the pay symbol
if 'payPosition' in locals():
payAmt = min(payAmt, payPosition)
# Find the right symbol to buy
symbol = buySymbol + "/" + paySymbol
side = OrderSide.BUY
try:
tradingClient.get_asset(symbol)
except:
symbol = paySymbol + "/" + buySymbol
side = OrderSide.SELL
# print("Using symbol", symbol)
# Convert payAmt to amt of buySymbol
pair = 1 / getCryptoPair(paySymbol, buySymbol) \
if side == OrderSide.BUY \
else getCryptoPair(buySymbol, paySymbol)
# print("Pair:", pair, buySymbol, "per", paySymbol)
# print("Pay amount:", payAmt, paySymbol)
if "USD" in symbol:
payAmt = math.floor(payAmt) * 0.95
# print("Pay amount (floored, 95%):", payAmt, paySymbol)
buyAmt = payAmt / pair
# print("Buy amount:", buyAmt, buySymbol)
# Generate order data
orderData = MarketOrderRequest(symbol=symbol, qty=buyAmt if side == OrderSide.BUY else payAmt, \
side=side, time_in_force=TimeInForce.GTC)
# print("Order data:", orderData)
tradingClient.submit_order(orderData)
print("Order placed!")