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

added documentation for more confusing functions

This commit is contained in:
Anson 2020-04-10 03:13:30 -07:00
parent f213f1e7e7
commit 3c22fe4d5c
2 changed files with 38 additions and 7 deletions

5
bot.py
View File

@ -68,7 +68,7 @@ def dividend(update, context):
if symbols:
context.bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
for reply in s.symbol_name(symbols).items():
for reply in s.dividend_reply(symbols).items():
update.message.reply_text(
text=reply[1], parse_mode=telegram.ParseMode.MARKDOWN
@ -87,7 +87,6 @@ def news(update, context):
context.bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
for reply in s.news_reply(symbols).items():
update.message.reply_text(
text=reply[1], parse_mode=telegram.ParseMode.MARKDOWN
)
@ -105,7 +104,6 @@ def info(update, context):
context.bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
for reply in s.info_reply(symbols).items():
update.message.reply_text(
text=reply[1], parse_mode=telegram.ParseMode.MARKDOWN
)
@ -132,6 +130,7 @@ def inline_query(update, context):
)
)
except TypeError:
logging.warning(str(match))
pass
if len(results) == 5:

View File

@ -9,6 +9,10 @@ from fuzzywuzzy import fuzz
class Symbol:
"""
Functions for finding stock market information about symbols.
"""
SYMBOL_REGEX = "[$]([a-zA-Z]{1,4})"
LIST_URL = "http://oatsreportable.finra.org/OATSReportableSecurities-SOD.txt"
@ -17,6 +21,13 @@ class Symbol:
self.symbol_list, self.symbol_ts = self.get_symbol_list()
def get_symbol_list(self):
"""
Fetches a list of stock market symbols from FINRA
Returns:
pd.DataFrame -- [DataFrame with columns: Symbol | Issue_Name | Primary_Listing_Mkt
datetime -- The time when the list of symbols was fetched. The Symbol list is updated every open and close of every trading day.
"""
raw_symbols = r.get(self.LIST_URL).text
symbols = pd.DataFrame(
[line.split("|") for line in raw_symbols.split("\n")][:-1]
@ -28,6 +39,15 @@ class Symbol:
return symbols, datetime.now()
def search_symbols(self, search: str):
"""
Performs a fuzzy search to find stock symbols closest to a search term.
Arguments:
search {str} -- String used to search, could be a company name or something close to the companies stock ticker.
Returns:
List of Tuples -- A list tuples of every stock sorted in order of how well they match. Each tuple contains: (Symbol, Issue Name).
"""
if self.symbol_ts - datetime.now() > timedelta(hours=12):
self.symbol_list, self.symbol_ts = self.get_symbol_list()
@ -48,14 +68,26 @@ class Symbol:
def find_symbols(self, text: str):
"""
Takes a blob of text and returns a list of symbols without any repeats.
Finds stock tickers starting with a dollar sign in a blob of text and returns them in a list. Only returns each match once. Example: Whats the price of $tsla? -> ['tsla']
Arguments:
text {str} -- Blob of text that might contain tickers with the format: $TICKER
Returns:
list -- List of every found match without the dollar sign.
"""
return list(set(re.findall(self.SYMBOL_REGEX, text)))
def price_reply(self, symbols: list):
"""
Takes a list of symbols and returns a dictionary of strings with information about the symbol.
Takes a list of symbols and replies with Markdown formatted text about the symbols price change for the day.
Arguments:
symbols {list} -- List of stock market symbols.
Returns:
dict -- Dictionary with keys of symbols and values of markdown formatted text example: {'tsla': 'The current stock price of Tesla Motors is $**420$$, the stock price is currently **up 42%**}
"""
dataMessages = {}
for symbol in symbols:
@ -80,7 +112,7 @@ class Symbol:
return dataMessages
def symbol_name(self, symbols: list):
def dividend_reply(self, symbols: list):
divMessages = {}
for symbol in symbols: