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

Merge branch 'bug-smashing-extravaganza' into 'master'

Add list of stocks to bot

Closes #105

See merge request simple-stock-bots/simple-telegram-stock-bot!55
This commit is contained in:
Anson Biggs 2023-10-12 02:42:53 +00:00
commit 10f8460cae
10 changed files with 47 additions and 23 deletions

View File

@ -1,5 +1,3 @@
FROM python:3.11 FROM python:3.11
COPY . . COPY . .
# RUN pip install -r requirements.txt

View File

@ -17,11 +17,11 @@
"vscode": { "vscode": {
"extensions": [ "extensions": [
"ms-python.python", "ms-python.python",
"ms-python.black-formatter", "ms-python.black-formatter",
"ms-python.flake8",
"ms-python.vscode-pylance", "ms-python.vscode-pylance",
"ms-python.isort", "ms-python.isort",
"charliermarsh.ruff" "charliermarsh.ruff",
"ms-toolsai.jupyter"
] ]
} }
}, },

View File

@ -4,5 +4,7 @@
"editor.formatOnSaveMode": "modificationsIfAvailable", "editor.formatOnSaveMode": "modificationsIfAvailable",
"[python]": { "[python]": {
"editor.defaultFormatter": "ms-python.black-formatter", "editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true,
"editor.formatOnSaveMode": "file"
} }
} }

View File

@ -20,6 +20,7 @@ class MarketData:
SYMBOL_REGEX = "[$]([a-zA-Z]{1,4})" SYMBOL_REGEX = "[$]([a-zA-Z]{1,4})"
symbol_list: Dict[str, Dict] = {}
charts: Dict[Stock, pd.DataFrame] = {} charts: Dict[Stock, pd.DataFrame] = {}
openTime = dt.time(hour=9, minute=30, second=0) openTime = dt.time(hour=9, minute=30, second=0)
@ -48,6 +49,9 @@ class MarketData:
if self.MARKETDATA_TOKEN != "": if self.MARKETDATA_TOKEN != "":
schedule.every().day.do(self.clear_charts) 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: def get(self, endpoint, params: dict = {}, timeout=10) -> dict:
url = "https://api.marketdata.app/v1/" + endpoint url = "https://api.marketdata.app/v1/" + endpoint
@ -85,6 +89,21 @@ class MarketData:
return {} 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: def clear_charts(self) -> None:
""" """
Clears cache of chart data. Clears cache of chart data.
@ -129,13 +148,13 @@ class MarketData:
Formatted markdown 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) price = round(quoteResp["last"][0], 2)
try: try:
changePercent = round(quoteResp["changepct"][0], 2) changePercent = round(quoteResp["changepct"][0], 2)
except TypeError: 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 " message = f"The current price of {symbol.name} is ${price} and "

View File

@ -29,11 +29,12 @@ class Symbol:
class Stock(Symbol): class Stock(Symbol):
"""Stock Market Object. Gets data from MarketData""" """Stock Market Object. Gets data from MarketData"""
def __init__(self, symbol: str) -> None: def __init__(self, symbol_info: dict) -> None:
self.symbol = symbol self.symbol = symbol_info["ticker"]
self.id = symbol self.id = symbol_info["ticker"]
self.name = "$" + symbol.upper() self.name = symbol_info["title"]
self.tag = "$" + symbol.lower() self.tag = "$" + symbol_info["ticker"]
self.market_cap_rank = symbol_info["mkt_cap_rank"]
class Coin(Symbol): class Coin(Symbol):

View File

@ -65,10 +65,13 @@ class Router:
schedule.run_pending() schedule.run_pending()
symbols: list[Symbol] = [] symbols: list[Symbol] = []
stocks = set(re.findall(self.STOCK_REGEX, text)) stock_matches = set(re.findall(self.STOCK_REGEX, text))
for stock in stocks: for stock_match in stock_matches:
# Market data lacks tools to check if a symbol is valid. # 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)) coins = set(re.findall(self.CRYPTO_REGEX, text))
for coin in coins: for coin in coins:

View File

@ -1,10 +1,11 @@
-r common/requirements.txt -r common/requirements.txt
-r docs/requirements.txt -r site/requirements.txt
black==23.7.0 black==23.9.1
flake8==6.1.0 ipython==8.16.1
Flake8-pyproject==1.2.3 jupyter_client==8.4.0
jupyter_core==5.4.0
pylama==8.4.1 pylama==8.4.1
mypy==1.5.1 mypy==1.5.1
types-cachetools==5.3.0.6 types-cachetools==5.3.0.6
types-pytz==2023.3.0.1 types-pytz==2023.3.1.1
ruff==0.0.292 ruff==0.0.292

View File

@ -1,2 +1,2 @@
nextcord==2.4.2 nextcord==2.6.0
-r requirements.txt -r requirements.txt

View File

@ -15,7 +15,7 @@ nav:
- Self-Host: host.md - Self-Host: host.md
- Donate: donate.md - Donate: donate.md
- Contact: contact.md - Contact: contact.md
- Blog: /blog - Blog: blog/index.md
theme: theme:
name: material name: material

View File

@ -1,2 +1,2 @@
python-telegram-bot==20.5 python-telegram-bot==20.6
-r requirements.txt -r requirements.txt