-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbrokers.py
445 lines (363 loc) · 15.9 KB
/
brokers.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import os
import httpx
import asyncio
import pyotp
import robin_stocks.robinhood as rh
from bbae_invest_api import BBAEAPI
from dspac_invest_api import DSPACAPI
from firstrade import account as ft_account, order, symbols
from public_invest_api import Public
from fennel_invest_api import Fennel
from decimal import Decimal
from tastytrade import Session, Account
from tastytrade.instruments import Equity
from tastytrade.order import (
NewOrder,
OrderTimeInForce,
OrderType,
PriceEffect,
OrderAction,
)
from schwab import auth
from schwab.orders.equities import (
equity_buy_limit,
equity_buy_market,
equity_sell_limit,
equity_sell_market,
)
from dotenv import load_dotenv
load_dotenv("./.env")
async def robinTrade(side, qty, ticker, price):
ROBINHOOD_USER = os.getenv("ROBINHOOD_USER")
ROBINHOOD_PASS = os.getenv("ROBINHOOD_PASS")
ROBINHOOD_MFA = os.getenv("ROBINHOOD_MFA")
if not (ROBINHOOD_USER and ROBINHOOD_PASS and ROBINHOOD_MFA):
print("No Robinhood credentials supplied, skipping")
return None
mfa = pyotp.TOTP(ROBINHOOD_MFA).now()
await asyncio.to_thread(rh.login, ROBINHOOD_USER, ROBINHOOD_PASS, mfa_code=mfa)
all_accounts = await asyncio.to_thread(rh.account.load_account_profile, dataType="results")
for account in all_accounts:
account_number = account['account_number']
brokerage_account_type = account['brokerage_account_type']
if side == 'buy':
order_function = rh.order_buy_limit if price else rh.order_buy_market
elif side == 'sell':
order_function = rh.order_sell_limit if price else rh.order_sell_market
else:
print(f"Invalid side: {side}")
return None
order_args = {
"symbol": ticker,
"quantity": qty,
"account_number": account_number,
"timeInForce": "gfd",
}
if price:
order_args['limitPrice'] = price
await asyncio.to_thread(order_function, **order_args)
action_str = "Bought" if side == "buy" else "Sold"
print(f"{action_str} {ticker} on Robinhood {brokerage_account_type} account {account_number}")
async def tradierTrade(side, qty, ticker, price):
TRADIER_ACCESS_TOKEN = os.getenv("TRADIER_ACCESS_TOKEN")
if not TRADIER_ACCESS_TOKEN:
print("Missing Tradier credentials, skipping")
return None
async with httpx.AsyncClient() as client:
response = await client.get(
"https://api.tradier.com/v1/user/profile",
headers={
"Authorization": f"Bearer {TRADIER_ACCESS_TOKEN}",
"Accept": "application/json",
},
)
if response.status_code != 200:
print(f"Error: {response.status} - {await response.text()}")
return False
profile_data = response.json()
accounts = profile_data.get("profile", {}).get("account", [])
if not accounts:
print("No accounts found.")
return False
TRADIER_ACCOUNT_ID = [account["account_number"] for account in accounts]
# Order placement
order_type = "limit" if price else "market"
price_data = {"price": f"{price}"} if price else {}
for account_id in TRADIER_ACCOUNT_ID:
response = await client.post(
f"https://api.tradier.com/v1/accounts/{account_id}/orders",
data={
"class": "equity",
"symbol": ticker,
"side": side,
"quantity": qty,
"type": order_type,
"duration": "day",
**price_data,
},
headers={
"Authorization": f"Bearer {TRADIER_ACCESS_TOKEN}",
"Accept": "application/json",
},
)
if response.status_code != 200:
print(
f"Error placing order on account {account_id}: {await response.text()}"
)
else:
action_str = "Bought" if side == "buy" else "Sold"
print(f"{action_str} {ticker} on Tradier account {account_id}")
async def tastyTrade(side, qty, ticker, price):
TASTY_USER = os.getenv("TASTY_USER")
TASTY_PASS = os.getenv("TASTY_PASS")
if not (TASTY_USER or TASTY_PASS):
print("No TastyTrade credentials supplied, skipping")
return None
session = Session(TASTY_USER, TASTY_PASS)
accounts = Account.get_accounts(session)
symbol = Equity.get_equity(session, ticker)
action = OrderAction.BUY_TO_OPEN if side == "buy" else OrderAction.SELL_TO_CLOSE
# Build the order
leg = symbol.build_leg(Decimal(qty), action)
order_type = OrderType.LIMIT if price else OrderType.MARKET
price_effect = PriceEffect.DEBIT if side == "buy" else PriceEffect.CREDIT
order_args = {
"time_in_force": OrderTimeInForce.DAY,
"order_type": order_type,
"legs": [leg],
"price_effect": price_effect,
}
if price:
order_args["price"] = price
order = NewOrder(**order_args)
for acc in accounts:
placed_order = acc.place_order(session, order, dry_run = False)
order_status = placed_order.order.status.value
if order_status in ["Received", "Routed"]:
action_str = "Bought" if side == "buy" else "Sold"
print(f"{action_str} {ticker} on TastyTrade {acc.account_type_name} account {acc.account_number}")
async def publicTrade(side, qty, ticker, price):
PUBLIC_USER = os.getenv("PUBLIC_USER")
PUBLIC_PASS = os.getenv("PUBLIC_PASS")
if not (PUBLIC_USER and PUBLIC_PASS):
print("No Public credentials supplied, skipping")
return None
public = Public(path="./tokens/")
await asyncio.to_thread(public.login, username=PUBLIC_USER, password=PUBLIC_PASS, wait_for_2fa=True)
order = await asyncio.to_thread(
public.place_order,
symbol=ticker,
quantity=qty,
side=side,
order_type='MARKET' if price is None else 'LIMIT',
limit_price=None if price is None else price,
time_in_force='DAY',
tip=0,
)
if order["success"] is True:
action_str = "Bought" if side == "buy" else "Sold"
print(f"{action_str} {ticker} on Public")
async def firstradeTrade(side, qty, ticker):
FIRSTRADE_USER = os.getenv("FIRSTRADE_USER")
FIRSTRADE_PASS = os.getenv("FIRSTRADE_PASS")
FIRSTRADE_PIN = os.getenv("FIRSTRADE_PIN")
ft_ss = ft_account.FTSession(
username=FIRSTRADE_USER,
password=FIRSTRADE_PASS,
pin=FIRSTRADE_PIN,
profile_path="./tokens/"
)
need_code = ft_ss.login()
if need_code:
code = input("Please enter the pin sent to your email/phone: ")
ft_ss.login_two(code)
ft_accounts = ft_account.FTAccountData(ft_ss)
# Firstrade does not allow market orders for stocks under $1.00
symbol_data = symbols.SymbolQuote(ft_ss, ft_accounts.account_numbers[0], ticker)
if symbol_data.last < 1.00:
price_type = order.PriceType.LIMIT
if side == "buy":
price = symbol_data.last + 0.01
else:
price = symbol_data.last - 0.01
else:
price_type = order.PriceType.MARKET
price = None
ft_order = order.Order(ft_ss)
for account_number in ft_accounts.account_numbers:
try:
order_conf = await asyncio.to_thread(
ft_order.place_order,
account_number,
symbol=ticker,
price_type=price_type,
order_type=order.OrderType.BUY if side == "buy" else order.OrderType.SELL,
quantity=qty,
duration=order.Duration.DAY,
price=price,
dry_run=False,
)
if order_conf.get("message") == "Normal":
print(f"Order for {ticker} placed on Firstrade successfully.")
print(f"Order ID: {order_conf.get("result").get('order_id')}.")
else:
print(f"Failed to place order for {ticker} on Firstrade.")
print(order_conf)
except Exception as e:
print(f"An error occurred while placing order for {ticker} on Firstrade: {e}")
async def fennelTrade(side, qty, ticker, price):
FENNEL_EMAIL = os.getenv("FENNEL_EMAIL")
if not FENNEL_EMAIL:
print("No Fennel credentials supplied, skipping")
return None
fennel = Fennel(path="./tokens/")
await asyncio.to_thread(fennel.login, email=FENNEL_EMAIL, wait_for_code=True)
account_ids = await asyncio.to_thread(fennel.get_account_ids)
for account_id in account_ids:
order = await asyncio.to_thread(
fennel.place_order,
account_id=account_id,
ticker=ticker,
quantity=qty,
side=side,
price="market",
)
if order.get('data', {}).get('createOrder') == 'pending':
action_str = "Bought" if side == "buy" else "Sold"
print(f"{action_str} {ticker} on Fennel account {account_id}")
else:
print(f"Failed to place order for {ticker} on Fennel account {account_id}")
async def schwabTrade(side, qty, ticker, price):
SCHWAB_API_KEY = os.getenv("SCHWAB_API_KEY")
SCHWAB_API_SECRET = os.getenv("SCHWAB_API_SECRET")
SCHWAB_CALLBACK_URL = os.getenv("SCHWAB_CALLBACK_URL")
SCHWAB_TOKEN_PATH = os.getenv("SCHWAB_TOKEN_PATH")
c = auth.easy_client(
SCHWAB_API_KEY,
SCHWAB_API_SECRET,
SCHWAB_CALLBACK_URL,
SCHWAB_TOKEN_PATH,
interactive=False
)
accounts = c.get_account_numbers()
order_types = {
("buy", True): equity_buy_limit,
("buy", False): equity_buy_market,
("sell", True): equity_sell_limit,
("sell", False): equity_sell_market,
}
order_function = order_types.get((side.lower(), bool(price)))
if not order_function:
raise ValueError(f"Invalid combination of side: {side} and price: {price}")
for account in accounts.json():
account_hash = account["hashValue"]
order = c.place_order(
account_hash,
(
order_function(ticker, qty, price)
if price
else order_function(ticker, qty)
),
)
if order.status_code == 201:
print(f"Order placed for {qty} shares of {ticker} on Schwab account {account['accountNumber']}")
else:
print(f"Error placing order on Schwab account {account['accountNumber']}: {order.json()}")
async def bbaeTrade(side, qty, ticker, price=None):
BBAE_USER = os.getenv("BBAE_USER")
BBAE_PASS = os.getenv("BBAE_PASS")
if not (BBAE_USER and BBAE_PASS):
print("No BBAE credentials supplied, skipping")
return None
bbae = BBAEAPI(BBAE_USER, BBAE_PASS, creds_path="./tokens/")
await asyncio.to_thread(bbae.make_initial_request)
login_ticket = await asyncio.to_thread(bbae.generate_login_ticket_email)
if login_ticket.get("Data") is None:
raise Exception("Invalid response from generating login ticket")
if login_ticket.get("Data").get("needSmsVerifyCode", False):
if login_ticket.get("Data").get("needCaptchaCode", False):
captcha_image = bbae.request_captcha()
captcha_image.save("./BBAEcaptcha.png", format="PNG")
captcha_input = input(
"CAPTCHA image saved to ./BBAEcaptcha.png. Please open it and type in the code: "
)
bbae.request_email_code(captcha_input=captcha_input)
otp_code = input("Enter BBAE security code: ")
else:
bbae.request_email_code()
otp_code = input("Enter BBAE security code: ")
login_ticket = await asyncio.to_thread(bbae.generate_login_ticket_email, otp_code)
login_response = await asyncio.to_thread(bbae.login_with_ticket, login_ticket.get("Data").get("ticket"))
if login_response.get("Outcome") != "Success":
raise Exception(f"Login failed. Response: {login_response}")
account_info = await asyncio.to_thread(bbae.get_account_info)
account_number = account_info.get("Data").get('accountNumber')
if not account_number:
print("Failed to retrieve account number from BBAE.")
return None
if side == 'buy':
response = await asyncio.to_thread(bbae.execute_buy, ticker, qty, account_number, dry_run=False)
elif side == 'sell':
holdings_response = await asyncio.to_thread(bbae.check_stock_holdings, ticker, account_number)
available_qty = holdings_response.get("Data").get('enableAmount', 0)
if int(available_qty) < qty:
print(f"Not enough shares to sell. Available: {available_qty}, Requested: {qty}")
return None
response = await asyncio.to_thread(bbae.execute_sell, ticker, qty, account_number, price, dry_run=False)
else:
print(f"Invalid trade side: {side}")
return None
if response.get("Outcome") == "Success":
action_str = "Bought" if side == "buy" else "Sold"
print(f"{action_str} {qty} shares of {ticker} on BBAE.")
else:
print(f"Failed to {side} {ticker}: {response.get('Message')}")
async def dspacTrade(side, qty, ticker, price=None):
DSPAC_USER = os.getenv("DSPAC_USER")
DSPAC_PASS = os.getenv("DSPAC_PASS")
if not (DSPAC_USER and DSPAC_PASS):
print("No DSPAC credentials supplied, skipping")
return None
dspac = DSPACAPI(DSPAC_USER, DSPAC_PASS, creds_path="./tokens/")
await asyncio.to_thread(dspac.make_initial_request)
login_ticket = await asyncio.to_thread(dspac.generate_login_ticket_email)
if login_ticket.get("Data") is None:
raise Exception("Invalid response from generating login ticket")
if login_ticket.get("Data").get("needSmsVerifyCode", False):
if login_ticket.get("Data").get("needCaptchaCode", False):
captcha_image = dspac.request_captcha()
captcha_image.save("./DSPACcaptcha.png", format="PNG")
captcha_input = input(
"CAPTCHA image saved to ./DSPACcaptcha.png. Please open it and type in the code: "
)
dspac.request_email_code(captcha_input=captcha_input)
otp_code = input("Enter DSPAC security code: ")
else:
dspac.request_email_code()
otp_code = input("Enter DSPAC security code: ")
login_ticket = await asyncio.to_thread(dspac.generate_login_ticket_email, otp_code)
login_response = await asyncio.to_thread(dspac.login_with_ticket, login_ticket.get("Data").get("ticket"))
if login_response.get("Outcome") != "Success":
raise Exception(f"Login failed. Response: {login_response}")
account_info = await asyncio.to_thread(dspac.get_account_info)
account_number = account_info.get("Data").get('accountNumber')
if not account_number:
print("Failed to retrieve account number from DSPAC.")
return None
if side == 'buy':
response = await asyncio.to_thread(dspac.execute_buy, ticker, qty, account_number, dry_run=False)
elif side == 'sell':
holdings_response = await asyncio.to_thread(dspac.check_stock_holdings, ticker, account_number)
available_qty = holdings_response.get("Data").get('enableAmount', 0)
if int(available_qty) < qty:
print(f"Not enough shares to sell. Available: {available_qty}, Requested: {qty}")
return None
response = await asyncio.to_thread(dspac.execute_sell, ticker, qty, account_number, price, dry_run=False)
else:
print(f"Invalid trade side: {side}")
return None
if response.get("Outcome") == "Success":
action_str = "Bought" if side == "buy" else "Sold"
print(f"{action_str} {qty} shares of {ticker} on DSPAC.")
else:
print(f"Failed to {side} {ticker}: {response.get('Message')}")