Skip to content

Commit

Permalink
concat holdings to save on discord api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonDane committed Jul 8, 2023
1 parent b151215 commit 44b22a9
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 44 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
__pycache__/
did.bin
test.py
*.png
*.png
.vscode/
17 changes: 0 additions & 17 deletions .vscode/settings.json

This file was deleted.

9 changes: 3 additions & 6 deletions allyAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,12 @@ def ally_holdings(ao, ctx=None, loop=None):
qty = (ah["qty"].values).tolist()
current_price = (ah["marketvalue"].values).tolist()
printAndDiscord(f"Ally {index} account symbols:", ctx, loop)
print_string = ""
for symbol in account_symbols:
# Set index for easy use
i = account_symbols.index(symbol)
printAndDiscord(
f"{symbol}: {float(qty[i])} @ ${round(float(current_price[i]), 2)} "
+ f"= ${round(float(qty[i]) * float(current_price[i]), 2)}",
ctx,
loop,
)
print_string += f"{symbol}: {float(qty[i])} @ ${round(float(current_price[i]), 2)} \n"
printAndDiscord(print_string, ctx, loop)
except Exception as e:
printAndDiscord(f"Ally {index}: Error getting account holdings: {e}", ctx, loop)

Expand Down
29 changes: 21 additions & 8 deletions autoRSA.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,25 @@ def argParser(args):
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
if DISCORD_CHANNEL:

@bot.event
async def on_ready():
channel = bot.get_channel(DISCORD_CHANNEL)
await channel.send("Discord bot is started...")
# Temp warning message
await channel.send("Heads up! .env file format has changed, see .env.example for new format")

# Bot ping-pong
@bot.command(name="ping")
Expand All @@ -266,14 +278,7 @@ async def ping(ctx):
# Help command
@bot.command()
async def help(ctx):
await ctx.send("Available commands:")
await ctx.send("!ping")
await ctx.send("!help")
await ctx.send("!holdings [all|ally|robinhood/rh|schwab|tradier]")
await ctx.send(
"!rsa [buy|sell] [amount] [stock] [all|ally|robinhood/rh|schwab|tradier] [DRY/true/false]"
)
await ctx.send("!restart")
await ctx.send(help_string)

# Main RSA command
@bot.command(name="rsa")
Expand Down Expand Up @@ -304,6 +309,14 @@ async def restart(ctx):
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...")
Expand Down
4 changes: 3 additions & 1 deletion robinhoodAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def robinhood_holdings(rho, ctx=None, loop=None):
printAndDiscord(f"No holdings in Robinhood {index}", ctx, loop)
else:
printAndDiscord(f"Holdings in Robinhood {index}:", ctx, loop)
print_string = ""
for item in positions:
# Get symbol, quantity, price, and total value
sym = item["symbol"] = obj.get_symbol_by_url(item["instrument"])
Expand All @@ -67,7 +68,8 @@ def robinhood_holdings(rho, ctx=None, loop=None):
if "NoneType" in str(e):
current_price = "N/A"
total_value = "N/A"
printAndDiscord(f"{sym}: {qty} @ ${(current_price)} = ${total_value}", ctx, loop)
print_string += f"{sym}: {qty} @ ${(current_price)} = ${total_value}\n"
printAndDiscord(print_string, ctx, loop)
except Exception as e:
printAndDiscord(f"Robinhood {index}: Error getting account holdings: {e}", ctx, loop)
print(traceback.format_exc())
Expand Down
5 changes: 4 additions & 1 deletion schwabAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def schwab_holdings(schwab_o, ctx=None, loop=None):
for account in list(obj.get_account_info().keys()):
printAndDiscord(f"Holdings in Schwab {index} Account: {account}", ctx, loop)
holdings = obj.get_account_info()[account]["positions"]
print_string = ""
for item in holdings:
# Get symbol, market value, quantity, current price, and total holdings
sym = item["symbol"]
Expand All @@ -70,7 +71,9 @@ def schwab_holdings(schwab_o, ctx=None, loop=None):
current_price = 0
else:
current_price = round(mv / qty, 2)
printAndDiscord(f"{sym}: {qty} @ ${current_price} = ${mv}", ctx, loop)
# Add to print string
print_string += f"{sym}: {qty} @ ${current_price} = ${mv}\n"
printAndDiscord(print_string, ctx, loop)
except Exception as e:
printAndDiscord(f"Schwab {index} {account}: Error getting holdings: {e}", ctx, loop)

Expand Down
8 changes: 3 additions & 5 deletions tastyAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,11 @@ def tastytrade_holdings(tt_o, ctx, loop=None):
temp_value = round((float(amounts[i]) * float(current_price[i])), 2)
current_value.append(temp_value)
printAndDiscord(f"Holdings on Tastytrade {index} {acct.account_number}")
print_string = ""
for position in stocks:
i = stocks.index(position)
printAndDiscord(
f"{position}: {amounts[i]} @ ${current_price[i]} = ${current_value[i]}",
ctx,
loop,
)
print_string += f"{position}: {amounts[i]} @ ${current_price[i]} = ${current_value[i]}\n"
printAndDiscord(print_string, ctx, loop)
printAndDiscord(f"Account cash balance is ${round(float(cash_balance), 2)}.", ctx, loop)
printAndDiscord(f"All accounts cash balance is ${round(float(all_account_balance), 2)}.", ctx, loop)
print()
Expand Down
8 changes: 3 additions & 5 deletions tradierAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,12 @@ def tradier_holdings(tradier_o, ctx=None, loop=None):
current_price[i] = round(current_price[i], 2)
# Print and send them
printAndDiscord(f"Holdings on Tradier {account_number}", ctx=ctx, loop=loop)
print_string = ""
for position in stocks:
# Set index for easy use
i = stocks.index(position)
printAndDiscord(
f"{position}: {amounts[i]} @ ${current_price[i]} = ${current_value[i]}",
ctx=ctx,
loop=loop,
)
print_string += f"{position}: {amounts[i]} @ ${current_price[i]} = ${current_value[i]}\n"
printAndDiscord(print_string, ctx=ctx, loop=loop)
except Exception as e:
printAndDiscord(f"Tradier {account_number}: Error getting holdings: {e}", ctx=ctx, loop=loop)
print(traceback.format_exc())
Expand Down

0 comments on commit 44b22a9

Please sign in to comment.