forked from NelsonDane/auto-rsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firstradeAPI.py
183 lines (173 loc) · 7.17 KB
/
firstradeAPI.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
# Donald Ryan Gullett(MaxxRK)
# Firstrade API
import asyncio
import os
import pprint
import traceback
from time import sleep
from dotenv import load_dotenv
from firstrade import account as ft_account
from firstrade import order, symbols
from helperAPI import (
Brokerage,
getOTPCodeDiscord,
maskString,
printAndDiscord,
printHoldings,
stockOrder,
)
def firstrade_init(botObj=None, loop=None):
# Initialize .env file
load_dotenv()
if not os.getenv("FIRSTRADE"):
print("Firstrade not found, skipping...")
return None
accounts = os.environ["FIRSTRADE"].strip().split(",")
# Log in to Firstrade account
print("Logging in to Firstrade...")
firstrade_obj = Brokerage("Firstrade")
for account in accounts:
index = accounts.index(account) + 1
name = f"Firstrade {index}"
try:
account = account.split(":")
firstrade = ft_account.FTSession(
username=account[0],
password=account[1],
pin=(
account[2]
if len(account[2]) == 4 and account[2].isdigit()
else None
),
phone=(
account[2][-4:]
if len(account[2]) == 10 and account[2].isdigit()
else None
),
email=account[2] if "@" in account[2] else None,
mfa_secret=(
account[2]
if len(account[2]) > 14 and "@" not in account[2]
else None
),
profile_path="./creds/",
)
need_code = firstrade.login()
if need_code:
if botObj is None and loop is None:
firstrade.login_two(input("Enter code: "))
else:
sms_code = asyncio.run_coroutine_threadsafe(
getOTPCodeDiscord(botObj, name, timeout=300, loop=loop), loop
).result()
if sms_code is None:
raise Exception(
f"Firstrade {index} code not received in time...", loop
)
firstrade.login_two(sms_code)
print("Logged in to Firstrade!")
account_info = ft_account.FTAccountData(firstrade)
firstrade_obj.set_logged_in_object(name, firstrade)
for account in account_info.account_numbers:
firstrade_obj.set_account_number(name, account)
firstrade_obj.set_account_totals(
name, account, account_info.account_balances[account]
)
print_accounts = [maskString(a) for a in account_info.account_numbers]
print(f"The following Firstrade accounts were found: {print_accounts}")
except Exception as e:
print(f"Error logging in to Firstrade: {e}")
print(traceback.format_exc())
return None
return firstrade_obj
def firstrade_holdings(firstrade_o: Brokerage, loop=None):
# Get holdings on each account
for key in firstrade_o.get_account_numbers():
for account in firstrade_o.get_account_numbers(key):
obj: ft_account.FTSession = firstrade_o.get_logged_in_objects(key)
try:
data = ft_account.FTAccountData(obj).get_positions(account=account)
for item in data["items"]:
firstrade_o.set_holdings(
key,
account,
item.get("symbol") or "Unknown",
item["quantity"],
item["market_value"],
)
except Exception as e:
printAndDiscord(f"{key} {account}: Error getting holdings: {e}", loop)
print(traceback.format_exc())
continue
printHoldings(firstrade_o, loop)
def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=None):
print()
print("==============================")
print("Firstrade")
print("==============================")
print()
# Buy on each account
for s in orderObj.get_stocks():
for key in firstrade_o.get_account_numbers():
printAndDiscord(
f"{key} {orderObj.get_action()}ing {orderObj.get_amount()} {s} @ {orderObj.get_price()}",
loop,
)
for account in firstrade_o.get_account_numbers(key):
obj: ft_account.FTSession = firstrade_o.get_logged_in_objects(key)
print_account = maskString(account)
# If DRY is True, don't actually make the transaction
if orderObj.get_dry():
printAndDiscord(
"Running in DRY mode. No transactions will be made.", loop
)
try:
symbol_data = symbols.SymbolQuote(obj, account, s)
if symbol_data.last < 1.00:
price_type = order.PriceType.LIMIT
if orderObj.get_action().capitalize() == "Buy":
price = symbol_data.last + 0.01
else:
price = symbol_data.last - 0.01
else:
price_type = order.PriceType.MARKET
price = 0.00
if orderObj.get_action().capitalize() == "Buy":
order_type = order.OrderType.BUY
else:
order_type = order.OrderType.SELL
ft_order = order.Order(obj)
order_conf = ft_order.place_order(
account=account,
symbol=s,
price_type=price_type,
order_type=order_type,
quantity=int(orderObj.get_amount()),
duration=order.Duration.DAY,
price=price,
dry_run=orderObj.get_dry(),
)
print("The order verification produced the following messages: ")
pprint.pprint(order_conf)
printAndDiscord(
(
f"{key} account {print_account}: The order verification was "
+ "successful"
if order_conf["error"] == ""
else "unsuccessful"
),
loop,
)
if not order_conf["error"] == "":
printAndDiscord(
f"{key} account {print_account}: The order verification produced the following messages: {order_conf}",
loop,
)
except Exception as e:
printAndDiscord(
f"{key} {print_account}: Error submitting order: {e}", loop
)
print(traceback.format_exc())
continue
sleep(1)
print()