Skip to content

Commit

Permalink
Started developing #379
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanAkkerman committed Nov 12, 2023
1 parent 8e5b2cc commit dabd823
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 30 deletions.
62 changes: 34 additions & 28 deletions src/cogs/commands/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,32 @@ def update_portfolio_db(self, new_db):
update_db(new_db, "portfolio")

@commands.dm_only()
@portfolios.command(name="add", description="Add a portfolio to the database.")
@portfolios.command(
name="add", description="Add a cryptocurrency portfolio to the database."
)
async def add(
self,
ctx: commands.Context,
input: Option(
exchange: Option(
str,
description="Provide the name of your crypto exchange.",
required=True,
),
key: Option(
str,
description="Provide your API key.",
required=True,
),
secret: Option(
str,
description="Provide the follow information: <exchange> <key> <secret> (<passphrase>).",
description="Provide your API secret.",
required=True,
),
passphrase: Option(
str,
description="Provide your API passphrase (only used for Kucoin).",
required=False,
),
) -> None:
"""
Adds your portfolio to the database.
Expand All @@ -57,33 +74,17 @@ async def add(
The information specified after `!portfolio`.
"""

# Split the input using the spaces
input = input.split(" ")

if len(input) < 3 or len(input) > 4:
raise commands.UserInputError()

exchange = input[0]
key = input[1]
secret = input[2]
passphrase = None

# Check if the exchange is supported
if exchange.lower() not in ["binance", "kucoin"]:
raise commands.BadArgument()

# Set the passphrase if necessary
if exchange.lower() == "kucoin":
if len(input) == 4:
passphrase = input[3]
else:
raise commands.BadArgument()

if exchange.lower() == "binance":
if len(input) != 3:
raise commands.BadArgument()
if not passphrase:
raise commands.UserInputError()
ccxt_exchange = ccxt.kucoin({"apiKey": key, "secret": secret})

exchange = ccxt.binance({"apiKey": key, "secret": secret})
elif exchange.lower() == "binance":
ccxt_exchange = ccxt.binance({"apiKey": key, "secret": secret})

new_data = pd.DataFrame(
{
Expand All @@ -97,10 +98,15 @@ async def add(
index=[0],
)

# Before adding the portfolio, check if it already exists
# TODO: Before adding the portfolio, check if it already exists

# Check if the API keys are valid
print(exchange.fetch_status())
status = ccxt_exchange.fetch_status()
if status["status"] != "ok":
await ctx.respond(
f"Your API keys are not valid! Please check your API keys and try again."
)
return

# Update the databse
util.vars.portfolio_db = pd.concat(
Expand All @@ -109,7 +115,7 @@ async def add(
update_db(util.vars.portfolio_db, "portfolio")

await ctx.respond(
"Succesfully added your portfolio to the database!\nPlease ensure that you set the API for read-only access."
"Succesfully added your portfolio to the database!\n⚠️ Please ensure that you set the API for read-only access ⚠️"
)

# Init Exchanges to start websockets
Expand Down Expand Up @@ -179,7 +185,7 @@ async def add_error(self, ctx: commands.Context, error: Exception) -> None:
)
elif isinstance(error, commands.UserInputError):
await ctx.respond(
f"If using `/portfolio add`, you must specify an exchange, key, secret, and optionally a passphrase!"
f"If using `/portfolio add` with Kucoin, you must specify a passphrase!"
)
elif isinstance(error, commands.PrivateMessageOnly):
await ctx.respond(
Expand Down
4 changes: 4 additions & 0 deletions src/cogs/listeners/on_raw_reaction_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ async def send_dm(self, message: discord.Message, user: discord.User) -> None:
None
"""

# Check if the message has an embed
if message.embeds == []:
return

# Get the old embed
e = message.embeds[0]

Expand Down
7 changes: 6 additions & 1 deletion src/cogs/loops/stock_halts.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ async def halt_embed(self):
if afterHours():
return

# Get the data
html = await self.get_halt_data()

if html == {}:
return

# Remove previous message first
await self.channel.purge(limit=1)

html = await self.get_halt_data()
df = self.format_df(html)

# Create embed
Expand Down
4 changes: 3 additions & 1 deletion src/util/tv_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,15 @@ async def get_tv_data(
print("TradingView websocket Error")
await session.close()
return (0, None, 0, None, website)

except aiohttp.ClientConnectionError:
print("Temporary TradingView websocket error")
return (0, None, 0, None, website)

except Exception:
print(traceback.format_exc())

return (0, None, 0, None, website)

def format_analysis(self, analysis: dict) -> str:
"""
Simple helper function to format the TA data into one string.
Expand Down

0 comments on commit dabd823

Please sign in to comment.