mirror of
https://gitlab.com/simple-stock-bots/simple-stock-bot.git
synced 2025-07-22 06:01:40 +00:00
Add list of stocks to bot
This commit is contained in:
@@ -20,6 +20,7 @@ class MarketData:
|
||||
|
||||
SYMBOL_REGEX = "[$]([a-zA-Z]{1,4})"
|
||||
|
||||
symbol_list: Dict[str, Dict] = {}
|
||||
charts: Dict[Stock, pd.DataFrame] = {}
|
||||
|
||||
openTime = dt.time(hour=9, minute=30, second=0)
|
||||
@@ -48,6 +49,9 @@ class MarketData:
|
||||
if self.MARKETDATA_TOKEN != "":
|
||||
schedule.every().day.do(self.clear_charts)
|
||||
|
||||
self.get_symbol_list()
|
||||
schedule.every().day.do(self.get_symbol_list)
|
||||
|
||||
def get(self, endpoint, params: dict = {}, timeout=10) -> dict:
|
||||
url = "https://api.marketdata.app/v1/" + endpoint
|
||||
|
||||
@@ -85,6 +89,21 @@ class MarketData:
|
||||
|
||||
return {}
|
||||
|
||||
def symbol_id(self, symbol: str) -> Dict[str, Dict]:
|
||||
return self.symbol_list.get(symbol.upper(), None)
|
||||
|
||||
def get_symbol_list(self):
|
||||
sec_resp = r.get("https://www.sec.gov/files/company_tickers.json")
|
||||
sec_resp.raise_for_status()
|
||||
sec_data = sec_resp.json()
|
||||
|
||||
for rank, ticker_info in sec_data.items():
|
||||
self.symbol_list[ticker_info["ticker"]] = {
|
||||
"ticker": ticker_info["ticker"],
|
||||
"title": ticker_info["title"],
|
||||
"mkt_cap_rank": rank,
|
||||
}
|
||||
|
||||
def clear_charts(self) -> None:
|
||||
"""
|
||||
Clears cache of chart data.
|
||||
@@ -129,13 +148,13 @@ class MarketData:
|
||||
Formatted markdown
|
||||
"""
|
||||
|
||||
if quoteResp := self.get(f"stocks/quotes/{symbol}/"):
|
||||
if quoteResp := self.get(f"stocks/quotes/{symbol.symbol}/"):
|
||||
price = round(quoteResp["last"][0], 2)
|
||||
|
||||
try:
|
||||
changePercent = round(quoteResp["changepct"][0], 2)
|
||||
except TypeError:
|
||||
return f"The price of {symbol} is {price}"
|
||||
return f"The price of {symbol.name} is ${price}"
|
||||
|
||||
message = f"The current price of {symbol.name} is ${price} and "
|
||||
|
||||
|
@@ -29,11 +29,12 @@ class Symbol:
|
||||
class Stock(Symbol):
|
||||
"""Stock Market Object. Gets data from MarketData"""
|
||||
|
||||
def __init__(self, symbol: str) -> None:
|
||||
self.symbol = symbol
|
||||
self.id = symbol
|
||||
self.name = "$" + symbol.upper()
|
||||
self.tag = "$" + symbol.lower()
|
||||
def __init__(self, symbol_info: dict) -> None:
|
||||
self.symbol = symbol_info["ticker"]
|
||||
self.id = symbol_info["ticker"]
|
||||
self.name = symbol_info["title"]
|
||||
self.tag = "$" + symbol_info["ticker"]
|
||||
self.market_cap_rank = symbol_info["mkt_cap_rank"]
|
||||
|
||||
|
||||
class Coin(Symbol):
|
||||
|
@@ -65,10 +65,13 @@ class Router:
|
||||
schedule.run_pending()
|
||||
|
||||
symbols: list[Symbol] = []
|
||||
stocks = set(re.findall(self.STOCK_REGEX, text))
|
||||
for stock in stocks:
|
||||
stock_matches = set(re.findall(self.STOCK_REGEX, text))
|
||||
for stock_match in stock_matches:
|
||||
# Market data lacks tools to check if a symbol is valid.
|
||||
symbols.append(Stock(stock))
|
||||
if stock_info := self.stock.symbol_id(stock_match):
|
||||
symbols.append(Stock(stock_info))
|
||||
else:
|
||||
log.info(f"{stock_match} is not in list of stocks")
|
||||
|
||||
coins = set(re.findall(self.CRYPTO_REGEX, text))
|
||||
for coin in coins:
|
||||
|
Reference in New Issue
Block a user