mirror of
https://gitlab.com/simple-stock-bots/simple-telegram-stock-bot.git
synced 2025-06-15 14:36:48 +00:00
Add list of stocks to bot
This commit is contained in:
parent
4a556597df
commit
66f233918e
@ -1,5 +1,3 @@
|
||||
FROM python:3.11
|
||||
|
||||
COPY . .
|
||||
|
||||
# RUN pip install -r requirements.txt
|
@ -17,11 +17,11 @@
|
||||
"vscode": {
|
||||
"extensions": [
|
||||
"ms-python.python",
|
||||
"ms-python.black-formatter",
|
||||
"ms-python.flake8",
|
||||
"ms-python.black-formatter",
|
||||
"ms-python.vscode-pylance",
|
||||
"ms-python.isort",
|
||||
"charliermarsh.ruff"
|
||||
"charliermarsh.ruff",
|
||||
"ms-toolsai.jupyter"
|
||||
]
|
||||
}
|
||||
},
|
||||
|
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -4,5 +4,7 @@
|
||||
"editor.formatOnSaveMode": "modificationsIfAvailable",
|
||||
"[python]": {
|
||||
"editor.defaultFormatter": "ms-python.black-formatter",
|
||||
"editor.formatOnSave": true,
|
||||
"editor.formatOnSaveMode": "file"
|
||||
}
|
||||
}
|
@ -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:
|
||||
|
11
dev-reqs.txt
11
dev-reqs.txt
@ -1,10 +1,11 @@
|
||||
-r common/requirements.txt
|
||||
-r docs/requirements.txt
|
||||
black==23.7.0
|
||||
flake8==6.1.0
|
||||
Flake8-pyproject==1.2.3
|
||||
-r site/requirements.txt
|
||||
black==23.9.1
|
||||
ipython==8.16.1
|
||||
jupyter_client==8.4.0
|
||||
jupyter_core==5.4.0
|
||||
pylama==8.4.1
|
||||
mypy==1.5.1
|
||||
types-cachetools==5.3.0.6
|
||||
types-pytz==2023.3.0.1
|
||||
types-pytz==2023.3.1.1
|
||||
ruff==0.0.292
|
@ -1,2 +1,2 @@
|
||||
nextcord==2.4.2
|
||||
nextcord==2.6.0
|
||||
-r requirements.txt
|
@ -15,7 +15,7 @@ nav:
|
||||
- Self-Host: host.md
|
||||
- Donate: donate.md
|
||||
- Contact: contact.md
|
||||
- Blog: /blog
|
||||
- Blog: blog/index.md
|
||||
|
||||
theme:
|
||||
name: material
|
||||
|
@ -1,2 +1,2 @@
|
||||
python-telegram-bot==20.5
|
||||
python-telegram-bot==20.6
|
||||
-r requirements.txt
|
Loading…
x
Reference in New Issue
Block a user