mirror of
https://gitlab.com/simple-stock-bots/simple-stock-bot.git
synced 2026-06-03 21:00:26 +00:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fa81669825 | |||
| 845c929311 | |||
| ed7c9f423a | |||
| bf37d4f0ea | |||
| a2fe7cdffc | |||
| 3ba785824c | |||
| f4ef76592d | |||
| 8fd3e4a6cd | |||
| b3af8b3270 | |||
| 86349deba4 | |||
| 22ed74d194 |
@@ -13,7 +13,7 @@ COPY --from=builder /root/.local /root/.local
|
|||||||
|
|
||||||
RUN pip install --no-cache-dir black
|
RUN pip install --no-cache-dir black
|
||||||
ENV TELEGRAM=TOKEN
|
ENV TELEGRAM=TOKEN
|
||||||
ENV IEX=TOKEN
|
ENV MARKETDATA=TOKEN
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
|
|||||||
+54
-14
@@ -1,18 +1,17 @@
|
|||||||
"""Class with functions for running the bot with IEX Cloud.
|
import datetime as dt
|
||||||
"""
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import datetime as dt
|
|
||||||
from logging import warning
|
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
import pytz
|
||||||
import requests as r
|
import requests as r
|
||||||
import schedule
|
import schedule
|
||||||
|
|
||||||
from Symbol import Stock
|
from Symbol import Stock
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class MarketData:
|
class MarketData:
|
||||||
"""
|
"""
|
||||||
@@ -23,6 +22,9 @@ class MarketData:
|
|||||||
|
|
||||||
charts: Dict[Stock, pd.DataFrame] = {}
|
charts: Dict[Stock, pd.DataFrame] = {}
|
||||||
|
|
||||||
|
openTime = dt.time(hour=9, minute=30, second=0)
|
||||||
|
marketTimeZone = pytz.timezone("US/Eastern")
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
"""Creates a Symbol Object
|
"""Creates a Symbol Object
|
||||||
|
|
||||||
@@ -31,6 +33,7 @@ class MarketData:
|
|||||||
MARKETDATA_TOKEN : str
|
MARKETDATA_TOKEN : str
|
||||||
MarketData.app API Token
|
MarketData.app API Token
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.MARKETDATA_TOKEN = os.environ["MARKETDATA"]
|
self.MARKETDATA_TOKEN = os.environ["MARKETDATA"]
|
||||||
|
|
||||||
@@ -38,7 +41,9 @@ class MarketData:
|
|||||||
self.MARKETDATA_TOKEN = ""
|
self.MARKETDATA_TOKEN = ""
|
||||||
except KeyError:
|
except KeyError:
|
||||||
self.MARKETDATA_TOKEN = ""
|
self.MARKETDATA_TOKEN = ""
|
||||||
warning("Starting without an MarketData.app Token will not allow you to get market data!")
|
log.warning("Starting without an MarketData.app Token will not allow you to get market data!")
|
||||||
|
log.warning("Use this affiliate link so that the bot can stay free:")
|
||||||
|
log.warning("https://dashboard.marketdata.app/marketdata/aff/go/misterbiggs?keyword=repo")
|
||||||
|
|
||||||
if self.MARKETDATA_TOKEN != "":
|
if self.MARKETDATA_TOKEN != "":
|
||||||
schedule.every().day.do(self.clear_charts)
|
schedule.every().day.do(self.clear_charts)
|
||||||
@@ -47,7 +52,11 @@ class MarketData:
|
|||||||
url = "https://api.marketdata.app/v1/" + endpoint
|
url = "https://api.marketdata.app/v1/" + endpoint
|
||||||
|
|
||||||
# set token param if it wasn't passed.
|
# set token param if it wasn't passed.
|
||||||
params["token"] = params.get("token", self.MARKETDATA_TOKEN)
|
params["token"] = self.MARKETDATA_TOKEN
|
||||||
|
|
||||||
|
# Undocumented query variable that ensures bot usage can be
|
||||||
|
# monitored even if someone doesn't make it through an affiliate link.
|
||||||
|
params["application"] = "simplestockbot"
|
||||||
|
|
||||||
resp = r.get(url, params=params, timeout=timeout)
|
resp = r.get(url, params=params, timeout=timeout)
|
||||||
|
|
||||||
@@ -85,7 +94,27 @@ class MarketData:
|
|||||||
self.charts = {}
|
self.charts = {}
|
||||||
|
|
||||||
def status(self) -> str:
|
def status(self) -> str:
|
||||||
return "status isnt implemented by marketdata.app"
|
# TODO: At the moment this API is poorly documented, this function likely needs to be revisited later.
|
||||||
|
|
||||||
|
try:
|
||||||
|
status = r.get(
|
||||||
|
"https://stats.uptimerobot.com/api/getMonitorList/6Kv3zIow0A",
|
||||||
|
timeout=5,
|
||||||
|
)
|
||||||
|
status.raise_for_status()
|
||||||
|
except r.HTTPError:
|
||||||
|
return f"API returned an HTTP error code {status.status_code} in {status.elapsed.total_seconds()} Seconds."
|
||||||
|
except r.Timeout:
|
||||||
|
return "API timed out before it was able to give status. This is likely due to a surge in usage or a complete outage."
|
||||||
|
|
||||||
|
statusJSON = status.json()
|
||||||
|
|
||||||
|
if statusJSON["status"] == "ok":
|
||||||
|
return (
|
||||||
|
f"CoinGecko API responded that it was OK with a {status.status_code} in {status.elapsed.total_seconds()} Seconds."
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return f"MarketData.app is currently reporting the following status: {statusJSON['status']}"
|
||||||
|
|
||||||
def price_reply(self, symbol: Stock) -> str:
|
def price_reply(self, symbol: Stock) -> str:
|
||||||
"""Returns price movement of Stock for the last market day, or after hours.
|
"""Returns price movement of Stock for the last market day, or after hours.
|
||||||
@@ -117,6 +146,14 @@ class MarketData:
|
|||||||
else:
|
else:
|
||||||
return f"Getting a quote for {symbol} encountered an error."
|
return f"Getting a quote for {symbol} encountered an error."
|
||||||
|
|
||||||
|
def spark_reply(self, symbol: Stock) -> str:
|
||||||
|
if quoteResp := self.get(f"stocks/quotes/{symbol}/"):
|
||||||
|
changePercent = round(quoteResp["changepct"][0], 2)
|
||||||
|
return f"`{symbol.tag}`: {changePercent}%"
|
||||||
|
else:
|
||||||
|
logging.warning(f"{symbol} did not have 'changepct' field.")
|
||||||
|
return f"`{symbol.tag}`"
|
||||||
|
|
||||||
def intra_reply(self, symbol: Stock) -> pd.DataFrame:
|
def intra_reply(self, symbol: Stock) -> pd.DataFrame:
|
||||||
"""Returns price data for a symbol of the past month up until the previous trading days close.
|
"""Returns price data for a symbol of the past month up until the previous trading days close.
|
||||||
Also caches multiple requests made in the same day.
|
Also caches multiple requests made in the same day.
|
||||||
@@ -138,18 +175,21 @@ class MarketData:
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
resolution = "5" # minutes
|
resolution = "15" # minutes
|
||||||
|
now = dt.datetime.now(self.marketTimeZone)
|
||||||
|
|
||||||
|
if self.openTime < now.time():
|
||||||
|
startTime = now.replace(hour=9, minute=30)
|
||||||
|
else:
|
||||||
|
startTime = now - dt.timedelta(days=1)
|
||||||
|
|
||||||
if data := self.get(
|
if data := self.get(
|
||||||
f"stocks/candles/{resolution}/{symbol}",
|
f"stocks/candles/{resolution}/{symbol}",
|
||||||
params={
|
params={"from": startTime.timestamp(), "to": now.timestamp(), "extended": True},
|
||||||
"from": dt.datetime.now().strftime("%Y-%m-%d"),
|
|
||||||
"to": dt.datetime.now().isoformat(),
|
|
||||||
},
|
|
||||||
):
|
):
|
||||||
data.pop("s")
|
data.pop("s")
|
||||||
df = pd.DataFrame(data)
|
df = pd.DataFrame(data)
|
||||||
df["t"] = pd.to_datetime(df["t"], unit="s")
|
df["t"] = pd.to_datetime(df["t"], unit="s", utc=True)
|
||||||
df.set_index("t", inplace=True)
|
df.set_index("t", inplace=True)
|
||||||
|
|
||||||
df.rename(
|
df.rename(
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ https://docs.simplestockbot.com/commands/
|
|||||||
|
|
||||||
## Donate
|
## Donate
|
||||||
|
|
||||||
Simple Stock Bot is run entirely on donations, and costs about $420 a year to run. All donations go directly towards paying for servers, and premium market data provided by [IEX Cloud](https://iexcloud.io/).
|
Simple Stock Bot is run entirely on donations, and costs about $420 a year to run. All donations go directly towards paying for servers, and premium market data provided by [marketdata.app](https://dashboard.marketdata.app/marketdata/aff/go/misterbiggs?keyword=repo).
|
||||||
|
|
||||||
The best way to donate is through [Buy Me A Coffee](https://www.buymeacoffee.com/Anson) which accepts Paypal or Credit card.
|
The best way to donate is through [Buy Me A Coffee](https://www.buymeacoffee.com/Anson) which accepts Paypal or Credit card.
|
||||||
|
|
||||||
@@ -29,4 +29,4 @@ If you have any questions get in [touch.](contact.md)
|
|||||||
- Contribute to the project on [GitLab](https://gitlab.com/simple-stock-bots) or just leave a star
|
- Contribute to the project on [GitLab](https://gitlab.com/simple-stock-bots) or just leave a star
|
||||||
- Using my referral links to host your own Bot
|
- Using my referral links to host your own Bot
|
||||||
- [DigitalOcean](https://m.do.co/c/6b5df7ef55b6)
|
- [DigitalOcean](https://m.do.co/c/6b5df7ef55b6)
|
||||||
- [IEX Cloud](https://iexcloud.io/s/62c8503e)
|
- [marketdata.app](https://dashboard.marketdata.app/marketdata/aff/go/misterbiggs?keyword=repo)
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ class T_info:
|
|||||||
help_text = """
|
help_text = """
|
||||||
Thanks for using this bot, consider supporting it by [buying me a beer.](https://www.buymeacoffee.com/Anson)
|
Thanks for using this bot, consider supporting it by [buying me a beer.](https://www.buymeacoffee.com/Anson)
|
||||||
|
|
||||||
|
If you are interested in stock market data, or want to host your own bot, be sure to use my affiliate link so that the bot can stay free: [marketdata.app](https://dashboard.marketdata.app/marketdata/aff/go/misterbiggs?keyword=telegram)
|
||||||
|
|
||||||
Keep up with the latest news for the bot in its Telegram Channel: https://t.me/simplestockbotnews
|
Keep up with the latest news for the bot in its Telegram Channel: https://t.me/simplestockbotnews
|
||||||
|
|
||||||
Full documentation on using and running your own stock bot can be found on the bots [docs.](https://docs.simplestockbot.com)
|
Full documentation on using and running your own stock bot can be found on the bots [docs.](https://docs.simplestockbot.com)
|
||||||
@@ -26,20 +28,15 @@ Simply calling a symbol in any message that the bot can see will also return the
|
|||||||
|
|
||||||
**Commands**
|
**Commands**
|
||||||
- `/donate [amount in USD]` to donate. 🎗️
|
- `/donate [amount in USD]` to donate. 🎗️
|
||||||
- `/dividend $[symbol]` Dividend information for the symbol. 📅
|
|
||||||
- `/intra $[symbol]` Plot of the stocks movement since the last market open. 📈
|
- `/intra $[symbol]` Plot of the stocks movement since the last market open. 📈
|
||||||
- `/chart $[symbol]` Plot of the stocks movement for the past 1 month. 📊
|
- `/chart $[symbol]` Plot of the stocks movement for the past 1 month. 📊
|
||||||
- `/news $[symbol]` News about the symbol. 📰
|
|
||||||
- `/info $[symbol]` General information about the symbol. ℹ️
|
|
||||||
- `/stat $[symbol]` Key statistics about the symbol. 🔢
|
|
||||||
- `/cap $[symbol]` Market Capitalization of symbol. 💰
|
|
||||||
- `/trending` Trending Stocks and Cryptos. 💬
|
- `/trending` Trending Stocks and Cryptos. 💬
|
||||||
- `/help` Get some help using the bot. 🆘
|
- `/help` Get some help using the bot. 🆘
|
||||||
|
|
||||||
**Inline Features**
|
**Inline Features**
|
||||||
You can type @SimpleStockBot `[search]` in any chat or direct message to search for the stock bots full list of stock and crypto symbols and return the price. Then once you select the ticker want the bot will send a message as you in that chat with the latest stock price. Prices may be delayed by up to an hour.
|
You can type @SimpleStockBot `[search]` in any chat or direct message to search for the stock bots full list of stock and crypto symbols and return the price. Then once you select the ticker want the bot will send a message as you in that chat with the latest stock price. Prices may be delayed by up to an hour.
|
||||||
|
|
||||||
Market data is provided by [IEX Cloud](https://iexcloud.io)
|
Market data is provided by [marketdata.app](https://dashboard.marketdata.app/marketdata/aff/go/misterbiggs?keyword=telegram)
|
||||||
|
|
||||||
If you believe the bot is not behaving properly run `/status` or [get in touch](https://docs.simplestockbot.com/contact).
|
If you believe the bot is not behaving properly run `/status` or [get in touch](https://docs.simplestockbot.com/contact).
|
||||||
"""
|
"""
|
||||||
@@ -47,27 +44,22 @@ Simply calling a symbol in any message that the bot can see will also return the
|
|||||||
donate_text = """
|
donate_text = """
|
||||||
Simple Stock Bot is run entirely on donations[.](https://www.buymeacoffee.com/Anson)
|
Simple Stock Bot is run entirely on donations[.](https://www.buymeacoffee.com/Anson)
|
||||||
All donations go directly towards paying for servers, and market data is provided by
|
All donations go directly towards paying for servers, and market data is provided by
|
||||||
[IEX Cloud](https://iexcloud.io/).
|
[marketdata.app](https://dashboard.marketdata.app/marketdata/aff/go/misterbiggs?keyword=telegram).
|
||||||
|
|
||||||
The easiest way to donate is to run the `/donate [amount in USD]` command with US dollars you would like to donate.
|
The easiest way to donate is to run the `/donate [amount in USD]` command with US dollars you would like to donate.
|
||||||
|
|
||||||
Example: `/donate 2` would donate 2 USD.
|
Example: `/donate 2` would donate 2 USD.
|
||||||
|
|
||||||
An alternative way to donate is through https://www.buymeacoffee.com/Anson which requires no account and accepts Paypal or Credit card.
|
An alternative way to donate is through https://www.buymeacoffee.com/Anson which requires no account and accepts Paypal or Credit card.
|
||||||
If you have any questions see the [website](https:docs.simplestockbot.com)
|
If you have any questions see the [website](https://docs.simplestockbot.com)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
commands = """
|
commands = """ # Not used by the bot but for updating commands with BotFather
|
||||||
donate - Donate to the bot 🎗️
|
donate - Donate to the bot 🎗️
|
||||||
help - Get some help using the bot. 🆘
|
help - Get some help using the bot. 🆘
|
||||||
info - $[symbol] General information about the symbol. ℹ️
|
|
||||||
news - $[symbol] News about the symbol. 📰
|
|
||||||
stat - $[symbol] Key statistics about the symbol. 🔢
|
|
||||||
cap - $[symbol] Market Capitalization of symbol. 💰
|
|
||||||
dividend - $[symbol] Dividend info 📅
|
|
||||||
trending - Trending Stocks and Cryptos. 💬
|
trending - Trending Stocks and Cryptos. 💬
|
||||||
intra - $[symbol] Plot since the last market open. 📈
|
intra - $[symbol] Plot since the last market open. 📈
|
||||||
chart - $[chart] Plot of the past month. 📊
|
chart - $[chart] Plot of the past month. 📊
|
||||||
""" # Not used by the bot but for updaing commands with BotFather
|
"""
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import os
|
|||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
import traceback
|
import traceback
|
||||||
import logging as log
|
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
import mplfinance as mpf
|
import mplfinance as mpf
|
||||||
@@ -32,6 +31,10 @@ from telegram.ext import (
|
|||||||
from symbol_router import Router
|
from symbol_router import Router
|
||||||
from T_info import T_info
|
from T_info import T_info
|
||||||
|
|
||||||
|
# Enable logging
|
||||||
|
logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO)
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
TELEGRAM_TOKEN = os.environ["TELEGRAM"]
|
TELEGRAM_TOKEN = os.environ["TELEGRAM"]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -43,10 +46,7 @@ except KeyError:
|
|||||||
s = Router()
|
s = Router()
|
||||||
t = T_info()
|
t = T_info()
|
||||||
|
|
||||||
# Enable logging
|
|
||||||
logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO)
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
log.info("Bot script started.")
|
log.info("Bot script started.")
|
||||||
|
|
||||||
|
|
||||||
@@ -237,7 +237,7 @@ def intra(update: Update, context: CallbackContext):
|
|||||||
update.message.reply_photo(
|
update.message.reply_photo(
|
||||||
photo=buf,
|
photo=buf,
|
||||||
caption=f"\nIntraday chart for {symbol.name} from {df.first_valid_index().strftime('%d %b at %H:%M')} to"
|
caption=f"\nIntraday chart for {symbol.name} from {df.first_valid_index().strftime('%d %b at %H:%M')} to"
|
||||||
+ f" {df.last_valid_index().strftime('%d %b at %H:%M')}"
|
+ f" {df.last_valid_index().strftime('%d %b at %H:%M %Z')}"
|
||||||
+ f"\n\n{s.price_reply([symbol])[0]}",
|
+ f"\n\n{s.price_reply([symbol])[0]}",
|
||||||
parse_mode=telegram.ParseMode.MARKDOWN,
|
parse_mode=telegram.ParseMode.MARKDOWN,
|
||||||
disable_notification=True,
|
disable_notification=True,
|
||||||
@@ -304,7 +304,6 @@ def trending(update: Update, context: CallbackContext):
|
|||||||
context.bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
|
context.bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
|
||||||
|
|
||||||
trending_list = s.trending()
|
trending_list = s.trending()
|
||||||
log.info(trending_list)
|
|
||||||
|
|
||||||
update.message.reply_text(
|
update.message.reply_text(
|
||||||
text=trending_list,
|
text=trending_list,
|
||||||
@@ -380,22 +379,21 @@ def error(update: Update, context: CallbackContext):
|
|||||||
log.warning(f"Logging error: {err_code}")
|
log.warning(f"Logging error: {err_code}")
|
||||||
|
|
||||||
if update:
|
if update:
|
||||||
message = (
|
log.warning(
|
||||||
f"An exception was raised while handling an update\n"
|
f"An exception was raised while handling an update\n"
|
||||||
f"<pre>update = {html.escape(json.dumps(update.to_dict(), indent=2, ensure_ascii=False))}"
|
f"\tupdate = {html.escape(json.dumps(update.to_dict(), indent=2, ensure_ascii=False))}\n"
|
||||||
"</pre>\n\n"
|
f"\tcontext.chat_data = {str(context.chat_data)}\n"
|
||||||
f"<pre>context.chat_data = {html.escape(str(context.chat_data))}</pre>\n\n"
|
f"\tcontext.user_data = {str(context.user_data)}\n"
|
||||||
f"<pre>context.user_data = {html.escape(str(context.user_data))}</pre>\n\n"
|
f"\t{html.escape(tb_string)}"
|
||||||
f"<pre>{html.escape(tb_string)}</pre>"
|
|
||||||
)
|
)
|
||||||
log.warning(message)
|
|
||||||
else:
|
|
||||||
log.warning(tb_string)
|
|
||||||
|
|
||||||
update.message.reply_text(
|
update.message.reply_text(
|
||||||
text=f"An error has occured. Please inform @MisterBiggs if the error persists. Error Code: `{err_code}`",
|
text=f"An error has occured. Please inform @MisterBiggs if the error persists. Error Code: `{err_code}`",
|
||||||
parse_mode=telegram.ParseMode.MARKDOWN,
|
parse_mode=telegram.ParseMode.MARKDOWN,
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
log.warning("No message to send to user.")
|
||||||
|
log.warning(tb_string)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
+3
-11
@@ -1,7 +1,4 @@
|
|||||||
"""Class with functions for running the bot with IEX Cloud.
|
import logging
|
||||||
"""
|
|
||||||
|
|
||||||
import logging as log
|
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
@@ -11,6 +8,8 @@ from markdownify import markdownify
|
|||||||
|
|
||||||
from Symbol import Coin
|
from Symbol import Coin
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class cg_Crypto:
|
class cg_Crypto:
|
||||||
"""
|
"""
|
||||||
@@ -22,13 +21,6 @@ class cg_Crypto:
|
|||||||
trending_cache: List[str] = []
|
trending_cache: List[str] = []
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
"""Creates a Symbol Object
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
IEX_TOKEN : str
|
|
||||||
IEX Token
|
|
||||||
"""
|
|
||||||
self.get_symbol_list()
|
self.get_symbol_list()
|
||||||
schedule.every().day.do(self.get_symbol_list)
|
schedule.every().day.do(self.get_symbol_list)
|
||||||
|
|
||||||
|
|||||||
+3
-1
@@ -3,4 +3,6 @@ black==23.3.0
|
|||||||
flake8==5.0.4
|
flake8==5.0.4
|
||||||
Flake8-pyproject==1.2.3
|
Flake8-pyproject==1.2.3
|
||||||
pylama==8.4.1
|
pylama==8.4.1
|
||||||
mypy==1.2.0
|
mypy==1.2.0
|
||||||
|
types-cachetools==5.3.0.5
|
||||||
|
types-pytz==2023.3.0.0
|
||||||
+28
-32
@@ -5,7 +5,6 @@ import datetime
|
|||||||
import logging
|
import logging
|
||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
from logging import critical, debug, error, info, warning
|
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import schedule
|
import schedule
|
||||||
@@ -15,11 +14,15 @@ from cg_Crypto import cg_Crypto
|
|||||||
from MarketData import MarketData
|
from MarketData import MarketData
|
||||||
from Symbol import Coin, Stock, Symbol
|
from Symbol import Coin, Stock, Symbol
|
||||||
|
|
||||||
|
from typing import Dict
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Router:
|
class Router:
|
||||||
STOCK_REGEX = "(?:^|[^\\$])\\$([a-zA-Z.]{1,6})"
|
STOCK_REGEX = "(?:^|[^\\$])\\$([a-zA-Z.]{1,6})"
|
||||||
CRYPTO_REGEX = "[$]{2}([a-zA-Z]{1,20})"
|
CRYPTO_REGEX = "[$]{2}([a-zA-Z]{1,20})"
|
||||||
trending_count = {}
|
trending_count: Dict[str, float] = {}
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.stock = MarketData()
|
self.stock = MarketData()
|
||||||
@@ -43,9 +46,9 @@ class Router:
|
|||||||
t_copy.pop(dead)
|
t_copy.pop(dead)
|
||||||
|
|
||||||
self.trending_count = t_copy.copy()
|
self.trending_count = t_copy.copy()
|
||||||
info("Decayed trending symbols.")
|
log.info("Decayed trending symbols.")
|
||||||
|
|
||||||
def find_symbols(self, text: str, *, trending_weight: int = 1) -> list[Symbol]:
|
def find_symbols(self, text: str, *, trending_weight: int = 1) -> list[Stock | Symbol]:
|
||||||
"""Finds stock tickers starting with a dollar sign, and cryptocurrencies with two dollar signs
|
"""Finds stock tickers starting with a dollar sign, and cryptocurrencies with two dollar signs
|
||||||
in a blob of text and returns them in a list.
|
in a blob of text and returns them in a list.
|
||||||
|
|
||||||
@@ -61,7 +64,7 @@ class Router:
|
|||||||
"""
|
"""
|
||||||
schedule.run_pending()
|
schedule.run_pending()
|
||||||
|
|
||||||
symbols = []
|
symbols: list[Symbol] = []
|
||||||
stocks = set(re.findall(self.STOCK_REGEX, text))
|
stocks = set(re.findall(self.STOCK_REGEX, text))
|
||||||
for stock in stocks:
|
for stock in stocks:
|
||||||
# Market data lacks tools to check if a symbol is valid.
|
# Market data lacks tools to check if a symbol is valid.
|
||||||
@@ -70,16 +73,16 @@ class Router:
|
|||||||
coins = set(re.findall(self.CRYPTO_REGEX, text))
|
coins = set(re.findall(self.CRYPTO_REGEX, text))
|
||||||
for coin in coins:
|
for coin in coins:
|
||||||
sym = self.crypto.symbol_list[self.crypto.symbol_list["symbol"].str.fullmatch(coin.lower(), case=False)]
|
sym = self.crypto.symbol_list[self.crypto.symbol_list["symbol"].str.fullmatch(coin.lower(), case=False)]
|
||||||
if ~sym.empty:
|
if sym.empty:
|
||||||
symbols.append(Coin(sym))
|
log.info(f"{coin} is not in list of coins")
|
||||||
else:
|
else:
|
||||||
info(f"{coin} is not in list of coins")
|
symbols.append(Coin(sym))
|
||||||
if symbols:
|
if symbols:
|
||||||
info(symbols)
|
|
||||||
for symbol in symbols:
|
for symbol in symbols:
|
||||||
self.trending_count[symbol.tag] = self.trending_count.get(symbol.tag, 0) + trending_weight
|
self.trending_count[symbol.tag] = self.trending_count.get(symbol.tag, 0) + trending_weight
|
||||||
|
log.debug(self.trending_count)
|
||||||
|
|
||||||
return symbols
|
return symbols
|
||||||
|
|
||||||
def status(self, bot_resp) -> str:
|
def status(self, bot_resp) -> str:
|
||||||
"""Checks for any issues with APIs.
|
"""Checks for any issues with APIs.
|
||||||
@@ -101,7 +104,7 @@ class Router:
|
|||||||
{self.crypto.status()}
|
{self.crypto.status()}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
warning(stats)
|
log.warning(stats)
|
||||||
|
|
||||||
return stats
|
return stats
|
||||||
|
|
||||||
@@ -150,13 +153,13 @@ class Router:
|
|||||||
replies = []
|
replies = []
|
||||||
|
|
||||||
for symbol in symbols:
|
for symbol in symbols:
|
||||||
info(symbol)
|
log.info(symbol)
|
||||||
if isinstance(symbol, Stock):
|
if isinstance(symbol, Stock):
|
||||||
replies.append(self.stock.price_reply(symbol))
|
replies.append(self.stock.price_reply(symbol))
|
||||||
elif isinstance(symbol, Coin):
|
elif isinstance(symbol, Coin):
|
||||||
replies.append(self.crypto.price_reply(symbol))
|
replies.append(self.crypto.price_reply(symbol))
|
||||||
else:
|
else:
|
||||||
info(f"{symbol} is not a Stock or Coin")
|
log.info(f"{symbol} is not a Stock or Coin")
|
||||||
|
|
||||||
return replies
|
return replies
|
||||||
|
|
||||||
@@ -182,7 +185,7 @@ class Router:
|
|||||||
elif isinstance(symbol, Coin):
|
elif isinstance(symbol, Coin):
|
||||||
replies.append(self.crypto.info_reply(symbol))
|
replies.append(self.crypto.info_reply(symbol))
|
||||||
else:
|
else:
|
||||||
debug(f"{symbol} is not a Stock or Coin")
|
log.debug(f"{symbol} is not a Stock or Coin")
|
||||||
|
|
||||||
return replies
|
return replies
|
||||||
|
|
||||||
@@ -206,7 +209,7 @@ class Router:
|
|||||||
elif isinstance(symbol, Coin):
|
elif isinstance(symbol, Coin):
|
||||||
return self.crypto.intra_reply(symbol)
|
return self.crypto.intra_reply(symbol)
|
||||||
else:
|
else:
|
||||||
debug(f"{symbol} is not a Stock or Coin")
|
log.debug(f"{symbol} is not a Stock or Coin")
|
||||||
return pd.DataFrame()
|
return pd.DataFrame()
|
||||||
|
|
||||||
def chart_reply(self, symbol: Symbol) -> pd.DataFrame:
|
def chart_reply(self, symbol: Symbol) -> pd.DataFrame:
|
||||||
@@ -229,7 +232,7 @@ class Router:
|
|||||||
elif isinstance(symbol, Coin):
|
elif isinstance(symbol, Coin):
|
||||||
return self.crypto.chart_reply(symbol)
|
return self.crypto.chart_reply(symbol)
|
||||||
else:
|
else:
|
||||||
debug(f"{symbol} is not a Stock or Coin")
|
log.debug(f"{symbol} is not a Stock or Coin")
|
||||||
return pd.DataFrame()
|
return pd.DataFrame()
|
||||||
|
|
||||||
def stat_reply(self, symbols: list[Symbol]) -> list[str]:
|
def stat_reply(self, symbols: list[Symbol]) -> list[str]:
|
||||||
@@ -254,7 +257,7 @@ class Router:
|
|||||||
elif isinstance(symbol, Coin):
|
elif isinstance(symbol, Coin):
|
||||||
replies.append(self.crypto.stat_reply(symbol))
|
replies.append(self.crypto.stat_reply(symbol))
|
||||||
else:
|
else:
|
||||||
debug(f"{symbol} is not a Stock or Coin")
|
log.debug(f"{symbol} is not a Stock or Coin")
|
||||||
|
|
||||||
return replies
|
return replies
|
||||||
|
|
||||||
@@ -280,7 +283,7 @@ class Router:
|
|||||||
elif isinstance(symbol, Coin):
|
elif isinstance(symbol, Coin):
|
||||||
replies.append(self.crypto.cap_reply(symbol))
|
replies.append(self.crypto.cap_reply(symbol))
|
||||||
else:
|
else:
|
||||||
debug(f"{symbol} is not a Stock or Coin")
|
log.debug(f"{symbol} is not a Stock or Coin")
|
||||||
|
|
||||||
return replies
|
return replies
|
||||||
|
|
||||||
@@ -301,12 +304,11 @@ class Router:
|
|||||||
|
|
||||||
for symbol in symbols:
|
for symbol in symbols:
|
||||||
if isinstance(symbol, Stock):
|
if isinstance(symbol, Stock):
|
||||||
replies.append("Command not supported for stocks.")
|
replies.append(self.stock.spark_reply(symbol))
|
||||||
# replies.append(self.stock.spark_reply(symbol))
|
|
||||||
elif isinstance(symbol, Coin):
|
elif isinstance(symbol, Coin):
|
||||||
replies.append(self.crypto.spark_reply(symbol))
|
replies.append(self.crypto.spark_reply(symbol))
|
||||||
else:
|
else:
|
||||||
debug(f"{symbol} is not a Stock or Coin")
|
log.debug(f"{symbol} is not a Stock or Coin")
|
||||||
|
|
||||||
return replies
|
return replies
|
||||||
|
|
||||||
@@ -320,26 +322,21 @@ class Router:
|
|||||||
List of preformatted strings to be sent to user.
|
List of preformatted strings to be sent to user.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
stocks = self.stock.trending()
|
# stocks = self.stock.trending()
|
||||||
coins = self.crypto.trending()
|
coins = self.crypto.trending()
|
||||||
|
|
||||||
reply = ""
|
reply = ""
|
||||||
|
|
||||||
|
log.warning(self.trending_count)
|
||||||
if self.trending_count:
|
if self.trending_count:
|
||||||
reply += "🔥Trending on the Stock Bot:\n`"
|
reply += "🔥Trending on the Stock Bot:\n`"
|
||||||
reply += "━" * len("Trending on the Stock Bot:") + "`\n"
|
reply += "━" * len("Trending on the Stock Bot:") + "`\n"
|
||||||
|
|
||||||
sorted_trending = [s[0] for s in sorted(self.trending_count.items(), key=lambda item: item[1])][::-1][0:5]
|
sorted_trending = [s[0] for s in sorted(self.trending_count.items(), key=lambda item: item[1])][::-1][0:5]
|
||||||
|
log.warning(sorted_trending)
|
||||||
for t in sorted_trending:
|
for t in sorted_trending:
|
||||||
reply += self.spark_reply(self.find_symbols(t))[0] + "\n"
|
reply += self.spark_reply(self.find_symbols(t))[0] + "\n"
|
||||||
|
|
||||||
if stocks:
|
|
||||||
reply += "\n\n💵Trending Stocks:\n`"
|
|
||||||
reply += "━" * len("Trending Stocks:") + "`\n"
|
|
||||||
for stock in stocks:
|
|
||||||
reply += stock + "\n"
|
|
||||||
|
|
||||||
if coins:
|
if coins:
|
||||||
reply += "\n\n🦎Trending Crypto:\n`"
|
reply += "\n\n🦎Trending Crypto:\n`"
|
||||||
reply += "━" * len("Trending Crypto:") + "`\n"
|
reply += "━" * len("Trending Crypto:") + "`\n"
|
||||||
@@ -352,7 +349,7 @@ class Router:
|
|||||||
if reply:
|
if reply:
|
||||||
return reply
|
return reply
|
||||||
else:
|
else:
|
||||||
warning("Failed to collect trending data.")
|
log.warning("Failed to collect trending data.")
|
||||||
return "Trending data is not currently available."
|
return "Trending data is not currently available."
|
||||||
|
|
||||||
def random_pick(self) -> str:
|
def random_pick(self) -> str:
|
||||||
@@ -385,10 +382,9 @@ class Router:
|
|||||||
elif isinstance(symbol, Coin):
|
elif isinstance(symbol, Coin):
|
||||||
coins.append(symbol)
|
coins.append(symbol)
|
||||||
else:
|
else:
|
||||||
debug(f"{symbol} is not a Stock or Coin")
|
log.debug(f"{symbol} is not a Stock or Coin")
|
||||||
|
|
||||||
if stocks:
|
if stocks:
|
||||||
# IEX batch endpoint doesnt seem to be working right now
|
|
||||||
for stock in stocks:
|
for stock in stocks:
|
||||||
replies.append(self.stock.price_reply(stock))
|
replies.append(self.stock.price_reply(stock))
|
||||||
if coins:
|
if coins:
|
||||||
|
|||||||
Reference in New Issue
Block a user