1
0
mirror of https://gitlab.com/simple-stock-bots/simple-stock-bot.git synced 2025-06-15 23:06:40 +00:00
This commit is contained in:
MisterBiggs 2021-06-23 14:17:50 -07:00
parent ae0075b986
commit baf45e09c8
4 changed files with 111 additions and 0 deletions

View File

@ -400,6 +400,28 @@ class IEX_Symbol:
else:
return f"No information found for: {symbol}\nEither today is boring or the symbol does not exist."
def cap_reply(self, stock: Stock) -> str:
"""Get the Market Cap of a stock"""
response = r.get(
f"https://cloud.iexapis.com/stable/stock/{stock.id}/stats?token={self.IEX_TOKEN}",
timeout=5,
)
if response.status_code == 200:
try:
data = response.json()
cap = data["marketcap"]
except KeyError:
return f"{stock.id} returned an error."
message = f"The current market cap of {stock.name} is $**{cap:,.2f}**"
else:
message = f"The Coin: {stock.name} was not found or returned and error."
return message
def intra_reply(self, symbol: Stock) -> pd.DataFrame:
"""Returns price data for a symbol since the last market open.

27
bot.py
View File

@ -397,6 +397,32 @@ def stat(update: Update, context: CallbackContext):
)
def cap(update: Update, context: CallbackContext):
"""
Market Cap Information
"""
message = update.message.text
chat_id = update.message.chat_id
if message.strip().split("@")[0] == "/cap":
update.message.reply_text(
"This command returns the market cap for a symbol.\nExample: /cap $tsla"
)
return
symbols = s.find_symbols(message)
if symbols:
context.bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
for reply in s.cap_reply(symbols):
update.message.reply_text(
text=reply,
parse_mode=telegram.ParseMode.MARKDOWN,
disable_notification=True,
)
def trending(update: Update, context: CallbackContext):
"""
Trending Symbols
@ -501,6 +527,7 @@ def main():
dp.add_handler(CommandHandler("info", info))
dp.add_handler(CommandHandler("stat", stat))
dp.add_handler(CommandHandler("stats", stat))
dp.add_handler(CommandHandler("cap", cap))
dp.add_handler(CommandHandler("trending", trending))
dp.add_handler(CommandHandler("search", search))
dp.add_handler(CommandHandler("intraday", intra))

View File

@ -252,6 +252,43 @@ class cg_Crypto:
else:
return f"{symbol.symbol} returned an error."
def cap_reply(self, coin: Coin) -> str:
"""Gets market Cap for each symbol in the list
Parameters
----------
symbols : List[str]
List of coin symbols
Returns
-------
Dict[str, str]
Each symbol passed in is a key with its value being a human readable formatted string of the symbols markey cap.
"""
response = r.get(
f"https://api.coingecko.com/api/v3/simple/price?ids={coin.id}&vs_currencies={self.vs_currency}&include_market_cap=true",
timeout=5,
)
if response.status_code == 200:
try:
data = response.json()[coin.id]
price = data[self.vs_currency]
cap = data[self.vs_currency + "_market_cap"]
except KeyError:
return f"{coin.id} returned an error."
if cap == 0:
return f"The market cap for {coin.name} is not available for unknown reasons."
message = f"The current price of {coin.name} is $**{price:,}** and its market cap is $**{cap:,.2f}** {self.vs_currency.upper()}"
else:
message = f"The Coin: {coin.name} was not found or returned and error."
return message
def info_reply(self, symbol: Coin) -> str:
"""Gets information on stock symbols.

View File

@ -314,6 +314,31 @@ class Router:
return replies
def cap_reply(self, symbols: List[Symbol]) -> List[str]:
"""Gets market cap for each symbol in the list
Parameters
----------
symbols : List[str]
List of stock symbols
Returns
-------
Dict[str, str]
Each symbol passed in is a key with its value being a human readable formatted string of the symbols market cap.
"""
replies = []
for symbol in symbols:
if isinstance(symbol, Stock):
replies.append(self.stock.cap_reply(symbol))
elif isinstance(symbol, Coin):
replies.append(self.crypto.cap_reply(symbol))
else:
print(f"{symbol} is not a Stock or Coin")
return replies
def trending(self) -> str:
"""Checks APIs for trending symbols.