forked from NelsonDane/auto-rsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoRSA.py
433 lines (407 loc) · 14.9 KB
/
autoRSA.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
# Nelson Dane
# Script to automate RSA stock purchases
# Import libraries
import asyncio
import os
import sys
import traceback
# Check Python version (minimum 3.10)
print("Python version:", sys.version)
if sys.version_info < (3, 10):
print("ERROR: Python 3.10 or newer is required")
sys.exit(1)
print()
try:
import discord
from discord.ext import commands
from dotenv import load_dotenv
# Custom API libraries
from bbaeAPI import *
from chaseAPI import *
from dspacAPI import *
from fennelAPI import *
from fidelityAPI import *
from firstradeAPI import *
from helperAPI import (
ThreadHandler,
check_package_versions,
printAndDiscord,
stockOrder,
updater,
)
from publicAPI import *
from robinhoodAPI import *
from schwabAPI import *
from tastyAPI import *
from tornadoAPI import *
from tradierAPI import *
from vanguardAPI import *
from webullAPI import *
from wellsfargoAPI import *
except Exception as e:
print(f"Error importing libraries: {e}")
print(traceback.format_exc())
print("Please run 'pip install -r requirements.txt'")
sys.exit(1)
# Initialize .env file
load_dotenv()
# Global variables
SUPPORTED_BROKERS = [
"bbae",
"chase",
"dspac",
"fennel",
"fidelity",
"firstrade",
"public",
"robinhood",
"schwab",
"tastytrade",
"tornado",
"tradier",
"vanguard",
"webull",
"wellsfargo",
]
DAY1_BROKERS = [
"bbae",
"chase",
"dspac",
"fennel",
"firstrade",
"public",
"schwab",
"tastytrade",
"tradier",
"webull",
]
DISCORD_BOT = False
DOCKER_MODE = False
DANGER_MODE = False
# Account nicknames
def nicknames(broker):
if broker == "bb":
return "bbae"
if broker == "ds":
return "dspac"
if broker in ["fid", "fido"]:
return "fidelity"
if broker == "ft":
return "firstrade"
if broker == "rh":
return "robinhood"
if broker == "tasty":
return "tastytrade"
if broker == "vg":
return "vanguard"
if broker == "wb":
return "webull"
if broker == "wf":
return "wellsfargo"
return broker
# Runs the specified function for each broker in the list
# broker name + type of function
def fun_run(orderObj: stockOrder, command, botObj=None, loop=None):
if command in [("_init", "_holdings"), ("_init", "_transaction")]:
for broker in orderObj.get_brokers():
if broker in orderObj.get_notbrokers():
continue
broker = nicknames(broker)
first_command, second_command = command
try:
# Initialize broker
fun_name = broker + first_command
if broker.lower() == "wellsfargo":
# Fidelity requires docker mode argument
orderObj.set_logged_in(
globals()[fun_name](
DOCKER=DOCKER_MODE, botObj=botObj, loop=loop
),
broker,
)
elif broker.lower() == "tornado":
# Requires docker mode argument and loop
orderObj.set_logged_in(
globals()[fun_name](DOCKER=DOCKER_MODE, loop=loop),
broker,
)
elif broker.lower() in [
"bbae",
"dspac",
"fennel",
"firstrade",
"public",
]:
# Requires bot object and loop
orderObj.set_logged_in(
globals()[fun_name](botObj=botObj, loop=loop), broker
)
elif broker.lower() in ["chase", "fidelity", "vanguard"]:
fun_name = broker + "_run"
# PLAYWRIGHT_BROKERS have to run all transactions with one function
th = ThreadHandler(
globals()[fun_name],
orderObj=orderObj,
command=command,
botObj=botObj,
loop=loop,
)
th.start()
th.join()
_, err = th.get_result()
if err is not None:
raise Exception(
"Error in "
+ fun_name
+ ": Function did not complete successfully."
)
else:
orderObj.set_logged_in(globals()[fun_name](), broker)
print()
if broker.lower() not in ["chase", "fidelity", "vanguard"]:
# Verify broker is logged in
orderObj.order_validate(preLogin=False)
logged_in_broker = orderObj.get_logged_in(broker)
if logged_in_broker is None:
print(f"Error: {broker} not logged in, skipping...")
continue
# Get holdings or complete transaction
if second_command == "_holdings":
fun_name = broker + second_command
globals()[fun_name](logged_in_broker, loop)
elif second_command == "_transaction":
fun_name = broker + second_command
globals()[fun_name](
logged_in_broker,
orderObj,
loop,
)
printAndDiscord(
f"All {broker.capitalize()} transactions complete",
loop,
)
except Exception as ex:
print(traceback.format_exc())
print(f"Error in {fun_name} with {broker}: {ex}")
print(orderObj)
print()
printAndDiscord("All commands complete in all brokers", loop)
else:
print(f"Error: {command} is not a valid command")
# Parse input arguments and update the order object
def argParser(args: list) -> stockOrder:
args = [x.lower() for x in args]
# Initialize order object
orderObj = stockOrder()
# If first argument is holdings, set holdings to true
if args[0] == "holdings":
orderObj.set_holdings(True)
# Next argument is brokers
if args[1] == "all":
orderObj.set_brokers(SUPPORTED_BROKERS)
elif args[1] == "day1":
orderObj.set_brokers(DAY1_BROKERS)
elif args[1] == "most":
orderObj.set_brokers(
list(filter(lambda x: x != "vanguard", SUPPORTED_BROKERS))
)
elif args[1] == "fast":
orderObj.set_brokers(DAY1_BROKERS + ["robinhood"])
else:
for broker in args[1].split(","):
orderObj.set_brokers(nicknames(broker))
# If next argument is not, set not broker
if len(args) > 3 and args[2] == "not":
for broker in args[3].split(","):
if nicknames(broker) in SUPPORTED_BROKERS:
orderObj.set_notbrokers(nicknames(broker))
return orderObj
# Otherwise: action, amount, stock, broker, (optional) not broker, (optional) dry
orderObj.set_action(args[0])
orderObj.set_amount(args[1])
for stock in args[2].split(","):
orderObj.set_stock(stock)
# Next argument is a broker, set broker
if args[3] == "all":
orderObj.set_brokers(SUPPORTED_BROKERS)
elif args[3] == "day1":
orderObj.set_brokers(DAY1_BROKERS)
elif args[3] == "most":
orderObj.set_brokers(list(filter(lambda x: x != "vanguard", SUPPORTED_BROKERS)))
elif args[3] == "fast":
orderObj.set_brokers(DAY1_BROKERS + ["robinhood"])
else:
for broker in args[3].split(","):
if nicknames(broker) in SUPPORTED_BROKERS:
orderObj.set_brokers(nicknames(broker))
# If next argument is not, set not broker
if len(args) > 4 and args[4] == "not":
for broker in args[5].split(","):
if nicknames(broker) in SUPPORTED_BROKERS:
orderObj.set_notbrokers(nicknames(broker))
# If next argument is false, set dry to false
if args[-1] == "false":
orderObj.set_dry(False)
# Validate order object
orderObj.order_validate(preLogin=True)
return orderObj
if __name__ == "__main__":
# Determine if ran from command line
if len(sys.argv) == 1: # If no arguments, do nothing
print("No arguments given, see README for usage")
sys.exit(1)
# Check if danger mode is enabled
if os.getenv("DANGER_MODE", "").lower() == "true":
DANGER_MODE = True
print("DANGER MODE ENABLED")
print()
# If docker argument, run docker bot
if sys.argv[1].lower() == "docker":
print("Running bot from docker")
DOCKER_MODE = DISCORD_BOT = True
# If discord argument, run discord bot, no docker, no prompt
elif sys.argv[1].lower() == "discord":
updater()
check_package_versions()
print("Running Discord bot from command line")
DISCORD_BOT = True
else: # If any other argument, run bot, no docker or discord bot
updater()
check_package_versions()
print("Running bot from command line")
print()
cliOrderObj = argParser(sys.argv[1:])
if not cliOrderObj.get_holdings():
print(f"Action: {cliOrderObj.get_action()}")
print(f"Amount: {cliOrderObj.get_amount()}")
print(f"Stock: {cliOrderObj.get_stocks()}")
print(f"Time: {cliOrderObj.get_time()}")
print(f"Price: {cliOrderObj.get_price()}")
print(f"Broker: {cliOrderObj.get_brokers()}")
print(f"Not Broker: {cliOrderObj.get_notbrokers()}")
print(f"DRY: {cliOrderObj.get_dry()}")
print()
print("If correct, press enter to continue...")
try:
if not DANGER_MODE:
input("Otherwise, press ctrl+c to exit")
print()
except KeyboardInterrupt:
print()
print("Exiting, no orders placed")
sys.exit(0)
# Validate order object
cliOrderObj.order_validate(preLogin=True)
# Get holdings or complete transaction
if cliOrderObj.get_holdings():
fun_run(cliOrderObj, ("_init", "_holdings"))
else:
fun_run(cliOrderObj, ("_init", "_transaction"))
sys.exit(0)
# If discord bot, run discord bot
if DISCORD_BOT:
# Get discord token and channel from .env file
if not os.environ["DISCORD_TOKEN"]:
raise Exception("DISCORD_TOKEN not found in .env file, please add it")
if not os.environ["DISCORD_CHANNEL"]:
raise Exception("DISCORD_CHANNEL not found in .env file, please add it")
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
DISCORD_CHANNEL = int(os.getenv("DISCORD_CHANNEL"))
# Initialize discord bot
intents = discord.Intents.default()
intents.message_content = True
# Discord bot command prefix
bot = commands.Bot(command_prefix="!", intents=intents)
bot.remove_command("help")
print()
print("Discord bot is started...")
print()
# Bot event when bot is ready
@bot.event
async def on_ready():
channel = bot.get_channel(DISCORD_CHANNEL)
if channel is None:
print(
"ERROR: Invalid channel ID, please check your DISCORD_CHANNEL in your .env file and try again"
)
os._exit(1) # Special exit code to restart docker container
await channel.send("Discord bot is started...")
# Process the message only if it's from the specified channel
@bot.event
async def on_message(message):
if message.channel.id == DISCORD_CHANNEL:
await bot.process_commands(message)
# Bot ping-pong
@bot.command(name="ping")
async def ping(ctx):
print("ponged")
await ctx.send("pong")
# Help command
@bot.command()
async def help(ctx):
# String of available commands
await ctx.send(
"Available RSA commands:\n"
"!ping\n"
"!help\n"
"!rsa holdings [all|<broker1>,<broker2>,...] [not broker1,broker2,...]\n"
"!rsa [buy|sell] [amount] [stock1|stock1,stock2] [all|<broker1>,<broker2>,...] [not broker1,broker2,...] [DRY: true|false]\n"
"!restart"
)
# Main RSA command
@bot.command(name="rsa")
async def rsa(ctx, *args):
discOrdObj = await bot.loop.run_in_executor(None, argParser, args)
event_loop = asyncio.get_event_loop()
try:
# Validate order object
discOrdObj.order_validate(preLogin=True)
# Get holdings or complete transaction
if discOrdObj.get_holdings():
# Run Holdings
await bot.loop.run_in_executor(
None,
fun_run,
discOrdObj,
("_init", "_holdings"),
bot,
event_loop,
)
else:
# Run Transaction
await bot.loop.run_in_executor(
None,
fun_run,
discOrdObj,
("_init", "_transaction"),
bot,
event_loop,
)
except Exception as err:
print(traceback.format_exc())
print(f"Error placing order: {err}")
if ctx:
await ctx.send(f"Error placing order: {err}")
# Restart command
@bot.command(name="restart")
async def restart(ctx):
print("Restarting...")
print()
await ctx.send("Restarting...")
await bot.close()
if DOCKER_MODE:
os._exit(0) # Special exit code to restart docker container
else:
os.execv(sys.executable, [sys.executable] + sys.argv)
# Catch bad commands
@bot.event
async def on_command_error(ctx, error):
print(f"Command Error: {error}")
await ctx.send(f"Command Error: {error}")
# Print help command
print("Type '!help' for a list of commands")
await ctx.send("Type '!help' for a list of commands")
# Run Discord bot
bot.run(DISCORD_TOKEN)
print("Discord bot is running...")
print()