forked from DerekNPelkey/auto-rsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoRSA.py
310 lines (285 loc) · 11.3 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
# Nelson Dane
# Script to automate RSA stock purchases
# Import libraries
import os
import re
import sys
import traceback
try:
import discord
from discord.ext import commands
from dotenv import load_dotenv
# Custom API libraries
from allyAPI import *
from fidelityAPI import *
from helperAPI import killDriver, stockOrder, updater
from robinhoodAPI import *
from schwabAPI import *
from tastyAPI import *
from tradierAPI import *
except Exception as e:
print(f"Error importing libraries: {e}")
print("Please run 'pip install -r requirements.txt'")
sys.exit(1)
# Initialize .env file
load_dotenv()
# Global variables
SUPPORTED_BROKERS = ["ally", "fidelity", "robinhood", "schwab", "tastytrade", "tradier"]
DISCORD_BOT = False
DOCKER_MODE = False
SUPRESS_OLD_WARN = False
# Account nicknames
def nicknames(broker):
if broker == "rh":
return "robinhood"
if broker == "tasty":
return "tastytrade"
return broker
# Runs the specified function for each broker in the list
# broker name + type of function
def fun_run(orderObj: stockOrder, command, loop=None):
if command in ["_init", "_holdings", "_transaction"]:
for broker in orderObj.get_brokers():
if broker in orderObj.get_notbrokers():
continue
fun_name = broker + command
try:
orderObj.order_validate(preLogin=True)
if command == "_init":
if nicknames(broker) == "fidelity":
# Fidelity requires docker mode argument
orderObj.set_logged_in(
globals()[fun_name](DOCKER=DOCKER_MODE), nicknames(broker)
)
else:
orderObj.set_logged_in(globals()[fun_name](), nicknames(broker))
# Holdings and transaction
elif orderObj.get_logged_in(nicknames(broker)) is None:
print(f"Error: {broker} not logged in, skipping...")
elif command == "_holdings":
orderObj.order_validate(preLogin=False)
globals()[fun_name](orderObj.get_logged_in(nicknames(broker)), loop)
elif command == "_transaction":
orderObj.order_validate(preLogin=False)
globals()[fun_name](
orderObj.get_logged_in(nicknames(broker)),
orderObj,
loop,
)
except Exception as ex:
print(traceback.format_exc())
print(f"Error in {fun_name} with {broker}: {ex}")
print(orderObj)
print()
else:
print(f"Error: {command} is not a valid command")
# Regex function to check if stock ticker is valid
def isStockTicker(symbol):
pattern = r"^[A-Z]{1,5}$" # Regex pattern for stock tickers
return re.match(pattern, symbol)
# Parse input arguments and update the order object
def argParser(args: str):
orderObj = stockOrder()
for arg in args:
arg = arg.lower()
# Exclusions
if arg == "not":
next_arg = nicknames(args[args.index(arg) + 1]).split(",")
for broker in next_arg:
if nicknames(broker) in SUPPORTED_BROKERS:
orderObj.set_notbrokers(nicknames(broker))
elif arg in ["buy", "sell"]:
orderObj.set_action(arg)
elif arg.isnumeric():
orderObj.set_amount(arg)
elif arg == "false":
orderObj.set_dry(False)
# If first item of list is a broker, it must be a list of brokers
elif nicknames(arg.split(",")[0]) in SUPPORTED_BROKERS:
for broker in arg.split(","):
# Add broker if it is valid and not in notbrokers
if (
nicknames(broker) in SUPPORTED_BROKERS
and nicknames(broker) not in orderObj.get_notbrokers()
):
orderObj.set_brokers(nicknames(broker))
elif arg == "all":
if "all" not in orderObj.get_brokers() and orderObj.get_brokers() == []:
orderObj.set_brokers(SUPPORTED_BROKERS)
elif arg == "holdings":
orderObj.set_holdings(True)
# If first item of list is a stock, it must be a list of stocks
elif (
isStockTicker(arg.split(",")[0].upper())
and arg.lower() != "dry"
and orderObj.get_stocks() == []
):
for stock in arg.split(","):
orderObj.set_stock(stock.upper())
# Validate order object
orderObj.order_validate(preLogin=True)
return orderObj
if __name__ == "__main__":
# Check for legacy .env file format
# This should be removed in a future release
if os.getenv("SUPRESS_OLD_WARN", "").lower() == "true":
SUPRESS_OLD_WARN = True
if re.search(r"(_USERNAME|_PASSWORD)", str(os.environ)) and not SUPRESS_OLD_WARN:
print("Legacy .env file found. Please update to new format.")
print("See .env.example for details.")
print("To supress this warning, set SUPRESS_OLD_WARN=True in .env")
# Print troublesome variables
print("Please update/remove the following variables:")
for key in os.environ:
if re.search(r"(_USERNAME|_PASSWORD)", key):
print(f"{key}={os.environ[key]}")
sys.exit(1)
# 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)
elif (
len(sys.argv) == 2 and sys.argv[1].lower() == "docker"
): # If docker argument, run docker bot
print("Running bot from docker")
DOCKER_MODE = DISCORD_BOT = True
elif (
len(sys.argv) == 2 and sys.argv[1].lower() == "discord"
): # If discord argument, run discord bot, no docker, no prompt
updater()
print("Running Discord bot from command line")
DISCORD_BOT = True
else: # If any other argument, run bot, no docker or discord bot
updater()
print("Running bot from command line")
cliOrderObj: stockOrder = 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:
input("Otherwise, press ctrl+c to exit")
print()
except KeyboardInterrupt:
print()
print("Exiting, no orders placed")
sys.exit(0)
# Login to brokers
fun_run(cliOrderObj, "_init")
# Validate order object
cliOrderObj.order_validate()
# Get holdings or complete transaction
if cliOrderObj.get_holdings():
fun_run(cliOrderObj, "_holdings")
else:
fun_run(cliOrderObj, "_transaction")
# Kill Selenium drivers
for b in cliOrderObj.get_logged_in():
if b.lower() == "fidelity":
killDriver(cliOrderObj.get_logged_in(b))
sys.exit(0)
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()
# String of available commands
help_string = (
"Available commands:\n"
"!ping\n"
"!help\n"
"!rsa holdings [all|<broker1>,<broker2>,...]\n"
"!rsa [buy|sell] [amount] [stock] [all|<broker1>,<broker2>,...] [not <broker1>,<broker2>,...] [DRY: true|false]\n"
"!restart"
)
# 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)
await channel.send("Discord bot is started...")
# Old .env file format warning
if not SUPRESS_OLD_WARN:
await channel.send(
"Heads up! .env file format has changed, see .env.example for new format"
)
await channel.send(
"To supress this message, set SUPRESS_OLD_WARN to True in your .env file"
)
# 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):
await ctx.send(help_string)
# Main RSA command
@bot.command(name="rsa")
async def rsa(ctx, *args):
discOrdObj: stockOrder = await bot.loop.run_in_executor(
None, argParser, args
)
loop = asyncio.get_event_loop()
try:
# Login to brokers
await bot.loop.run_in_executor(None, fun_run, discOrdObj, "_init")
# Validate order object
discOrdObj.order_validate()
# Get holdings or complete transaction
if discOrdObj.get_holdings():
await bot.loop.run_in_executor(
None, fun_run, discOrdObj, "_holdings", loop
)
else:
await bot.loop.run_in_executor(
None, fun_run, discOrdObj, "_transaction", 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()
os._exit(0)
# Catch bad commands
@bot.event
async def on_command_error(ctx, error):
print(f"Error: {error}")
await ctx.send(f"Error: {error}")
# Print help command
await ctx.send(help_string)
# Run Discord bot
bot.run(DISCORD_TOKEN)
print("Discord bot is running...")
print()