1
0
mirror of https://gitlab.com/simple-stock-bots/simple-stock-bot.git synced 2025-06-16 15:17:28 +00:00

renamed ticker to symbol everywhere

This commit is contained in:
Anson 2019-07-10 12:03:59 -07:00
parent 38fcf6d054
commit 33c80bd75b
2 changed files with 69 additions and 56 deletions

38
bot.py
View File

@ -7,7 +7,7 @@ from functions import *
from telegram.ext import CommandHandler, Filters, MessageHandler, Updater
TELEGRAM_TOKEN = os.environ["TELEGRAM"]
TICKER_REGEX = "[$]([a-zA-Z]{1,4})"
symbol_REGEX = "[$]([a-zA-Z]{1,4})"
# Enable logging
logging.basicConfig(
@ -31,19 +31,19 @@ def help(bot, update):
update.message.reply_text(text=message, parse_mode=telegram.ParseMode.MARKDOWN)
def tickerDetect(bot, update):
def symbolDetect(bot, update):
"""
Runs on any message that doesn't have a command and searches for tickers, then returns the prices of any tickers found.
Runs on any message that doesn't have a command and searches for symbols, then returns the prices of any symbols found.
"""
message = update.message.text
chat_id = update.message.chat_id
tickers = getTickers(message)
symbols = getSymbols(message)
if tickers:
if symbols:
# Let user know bot is working
bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
for symbol, reply in tickerDataReply(tickers).items():
for symbol, reply in symbolDataReply(symbols).items():
update.message.reply_text(
text=reply, parse_mode=telegram.ParseMode.MARKDOWN
@ -52,16 +52,16 @@ def tickerDetect(bot, update):
def dividend(bot, update):
"""
waits for /dividend or /div command and then finds dividend info on that ticker.
waits for /dividend or /div command and then finds dividend info on that symbol.
"""
message = update.message.text
chat_id = update.message.chat_id
tickers = getTickers(message)
symbols = getSymbols(message)
if tickers:
if symbols:
bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
for symbol, reply in tickerDividend(tickers).items():
for symbol, reply in symbolDividend(symbols).items():
update.message.reply_text(
text=reply, parse_mode=telegram.ParseMode.MARKDOWN
@ -70,16 +70,16 @@ def dividend(bot, update):
def news(bot, update):
"""
waits for /news command and then finds news info on that ticker.
waits for /news command and then finds news info on that symbol.
"""
message = update.message.text
chat_id = update.message.chat_id
tickers = getTickers(message)
symbols = getSymbols(message)
if tickers:
if symbols:
bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
for symbol, reply in tickerNews(tickers).items():
for symbol, reply in symbolNews(symbols).items():
update.message.reply_text(
text=reply, parse_mode=telegram.ParseMode.MARKDOWN
@ -88,16 +88,16 @@ def news(bot, update):
def info(bot, update):
"""
waits for /info command and then finds info on that ticker.
waits for /info command and then finds info on that symbol.
"""
message = update.message.text
chat_id = update.message.chat_id
tickers = getTickers(message)
symbols = getSymbols(message)
if tickers:
if symbols:
bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
for symbol, reply in tickerInfo(tickers).items():
for symbol, reply in symbolInfo(symbols).items():
update.message.reply_text(
text=reply, parse_mode=telegram.ParseMode.MARKDOWN
@ -126,7 +126,7 @@ def main():
dp.add_handler(CommandHandler("info", info))
# on noncommand i.e message - echo the message on Telegram
dp.add_handler(MessageHandler(Filters.text, tickerDetect))
dp.add_handler(MessageHandler(Filters.text, symbolDetect))
# log all errors
dp.add_error_handler(error)

View File

@ -5,27 +5,27 @@ import urllib.request
import requests
from datetime import datetime
IEX_TOKEN = os.environ["IEX"]
IEX_TOKEN = "sk_130b8e8f75ba4e14a5683ff37a655584" # os.environ["IEX"]
def getTickers(text: str):
def getSymbols(text: str):
"""
Takes a blob of text and returns any stock tickers found.
Takes a blob of text and returns any stock symbols found.
"""
TICKER_REGEX = "[$]([a-zA-Z]{1,4})"
SYMBOL_REGEX = "[$]([a-zA-Z]{1,4})"
return list(set(re.findall(TICKER_REGEX, text)))
return list(set(re.findall(SYMBOL_REGEX, text)))
def tickerDataReply(tickers: list):
def symbolDataReply(symbols: list):
"""
Takes a list of tickers and returns a list of strings with information about the ticker.
Takes a list of symbols and returns a list of strings with information about the symbol.
"""
tickerReplies = {}
for ticker in tickers:
symbolReplies = {}
for symbol in symbols:
IEXURL = (
f"https://cloud.iexapis.com/stable/stock/{ticker}/quote?token={IEX_TOKEN}"
f"https://cloud.iexapis.com/stable/stock/{symbol}/quote?token={IEX_TOKEN}"
)
try:
with urllib.request.urlopen(IEXURL) as url:
@ -42,74 +42,87 @@ def tickerDataReply(tickers: list):
else:
reply += ", the stock hasn't shown any movement today."
except:
reply = f"The ticker: {ticker} was not found."
reply = f"The symbol: {symbol} was not found."
tickerReplies[ticker] = reply
symbolReplies[symbol] = reply
return tickerReplies
return symbolReplies
def tickerDividend(tickers: list):
def symbolDividend(symbols: list):
messages = {}
for ticker in tickers:
IEXurl = f"https://cloud.iexapis.com/stable/data-points/{ticker}/NEXTDIVIDENDDATE?token={IEX_TOKEN}"
date = requests.get(IEXurl).json()
if date:
for symbol in symbols:
IEXurl = f"https://cloud.iexapis.com/stable/data-points/{symbol}/NEXTDIVIDENDDATE?token={IEX_TOKEN}"
response = requests.get(IEXurl)
if response.status_code is 200:
date = response.json()
# Pattern IEX uses for dividend date.
pattern = "%Y-%m-%d"
divDate = datetime.strptime(date, pattern)
#
daysDelta = divDate - datetime.now()
daysDelta = (divDate - datetime.now()).days
datePretty = divDate.strftime("%A, %B %w")
if daysDelta < 0:
messages[
symbol
] = f"{symbol.upper()} dividend was on {datePretty} and a new date hasn't been announced yet."
elif daysDelta > 0:
messages[
symbol
] = f"{symbol.upper()} dividend is on {datePretty} which is in {daysDelta} Days."
else:
messages[symbol] = f"{symbol.upper()} is today."
messages[
ticker
] = f"{ticker.upper()} dividend is on {datePretty} which is in {daysDelta} Days."
else:
messages[ticker] = f"{ticker} either doesn't exist or pays no dividend."
messages[symbol] = f"{symbol} either doesn't exist or pays no dividend."
return messages
def tickerNews(tickers: list):
def symbolNews(symbols: list):
messages = {}
for ticker in tickers:
IEXurl = f"https://cloud.iexapis.com/stable/stock/{ticker}/news/last/3?token={IEX_TOKEN}"
for symbol in symbols:
IEXurl = f"https://cloud.iexapis.com/stable/stock/{symbol}/news/last/3?token={IEX_TOKEN}"
with urllib.request.urlopen(IEXurl) as url:
data = json.loads(url.read().decode())
if data:
messages[ticker] = f"News for **{ticker.upper()}**:\n"
messages[symbol] = f"News for **{symbol.upper()}**:\n"
for news in data:
message = f"\t[{news['headline']}]({news['url']})\n\n"
messages[ticker] = messages[ticker] + message
messages[symbol] = messages[symbol] + message
else:
messages[
ticker
] = f"No news found for: {ticker}\nEither today is boring or the ticker does not exist."
symbol
] = f"No news found for: {symbol}\nEither today is boring or the symbol does not exist."
return messages
def tickerInfo(tickers: list):
def symbolInfo(symbols: list):
messages = {}
for ticker in tickers:
for symbol in symbols:
IEXurl = (
f"https://cloud.iexapis.com/stable/stock/{ticker}/company?token={IEX_TOKEN}"
f"https://cloud.iexapis.com/stable/stock/{symbol}/company?token={IEX_TOKEN}"
)
with urllib.request.urlopen(IEXurl) as url:
data = json.loads(url.read().decode())
if data:
messages[
ticker
symbol
] = f"Company Name: [{data['companyName']}]({data['website']})\nIndustry: {data['industry']}\nSector: {data['sector']}\nCEO: {data['CEO']}\nDescription: {data['description']}\n"
else:
messages[
ticker
] = f"No information found for: {ticker}\nEither today is boring or the ticker does not exist."
symbol
] = f"No information found for: {symbol}\nEither today is boring or the symbol does not exist."
return messages
# symbolDividend(["psec", "f", "tsla"])
print(symbolDividend(["f"]))