1
0
mirror of https://gitlab.com/simple-stock-bots/simple-telegram-stock-bot.git synced 2025-06-16 15:06:53 +00:00
This commit is contained in:
Anson 2021-05-26 22:16:41 -07:00
parent 1d884110e6
commit aba7120889
4 changed files with 47 additions and 21 deletions

View File

@ -69,8 +69,9 @@ class IEX_Symbol:
symbols["description"] = "$" + symbols["symbol"] + ": " + symbols["name"]
symbols["id"] = symbols["symbol"]
symbols["type_id"] = "$" + symbols["symbol"].str.lower()
symbols = symbols[["id", "symbol", "name", "description"]]
symbols = symbols[["id", "symbol", "name", "description", "type_id"]]
self.symbol_list = symbols
if return_df:
return symbols, datetime.now()

5
bot.py
View File

@ -422,12 +422,11 @@ def inline_query(update: Update, context: CallbackContext):
Does a fuzzy search on input and returns stocks that are close.
"""
matches = s.search_symbols(update.inline_query.query)[:]
matches = s.inline_search(update.inline_query.query)[:5]
symbols = " ".join([match[1].split(":")[0] for match in matches])
prices = s.batch_price_reply(s.find_symbols(symbols))
# print(len(matches), len(prices))
# print(prices)
results = []
print(update.inline_query.query)
for match, price in zip(matches, prices):

View File

@ -47,8 +47,11 @@ class cg_Crypto:
raw_symbols = r.get("https://api.coingecko.com/api/v3/coins/list").json()
symbols = pd.DataFrame(data=raw_symbols)
symbols["description"] = "$$" + symbols["symbol"] + ": " + symbols["name"]
symbols["description"] = (
"$$" + symbols["symbol"].str.upper() + ": " + symbols["name"]
)
symbols = symbols[["id", "symbol", "name", "description"]]
symbols["type_id"] = "$$" + symbols["id"]
self.symbol_list = symbols
if return_df:
@ -287,19 +290,15 @@ class cg_Crypto:
).json()
replies = []
for name, val in prices.items():
if price := val.get("usd"):
price = val.get("usd")
else:
replies.append(f"{name} price data unavailable.")
break
for coin in coins:
if coin.id in prices:
p = prices[coin.id]
change = 0
if val.get("usd_24h_change") is not None:
change = val.get("usd_24h_change")
if p.get("usd_24h_change") is None:
p["usd_24h_change"] = 0
replies.append(
f"{name}: ${price:,} and has moved {change:.2f}% in the past 24 hours."
)
replies.append(
f"{coin.name}: ${p.get('usd',0):,} and has moved {p.get('usd_24h_change',0.0):.2f}% in the past 24 hours."
)
return replies

View File

@ -113,6 +113,34 @@ class Router:
self.searched_symbols[search] = symbol_list
return symbol_list
def inline_search(self, search: str) -> List[Tuple[str, str]]:
"""Searches based on the shortest symbol that contains the same string as the search.
Should be very fast compared to a fuzzy search.
Parameters
----------
search : str
String used to match against symbols.
Returns
-------
List[tuple[str, str]]
Each tuple contains: (Symbol, Issue Name).
"""
df = pd.concat([self.stock.symbol_list, self.crypto.symbol_list])
search = search.lower()
df = df[df["type_id"].str.contains(search, regex=False)].sort_values(
by="type_id", key=lambda x: x.str.len()
)
symbols = df.head(20)
symbol_list = list(zip(list(symbols["symbol"]), list(symbols["description"])))
self.searched_symbols[search] = symbol_list
return symbol_list
def price_reply(self, symbols: list[Symbol]) -> List[str]:
"""Returns current market price or after hours if its available for a given stock symbol.
@ -347,11 +375,10 @@ class Router:
print(f"{symbol} is not a Stock or Coin")
if stocks:
for (
stock
) in stocks: # IEX batch endpoint doesnt seem to be working right now
# IEX batch endpoint doesnt seem to be working right now
for stock in stocks:
replies.append(self.stock.price_reply(stock))
if coins:
replies.append(self.crypto.batch_price(coins))
replies = replies + self.crypto.batch_price(coins)
return replies