mirror of
https://gitlab.com/simple-stock-bots/simple-stock-bot.git
synced 2025-06-16 07:16:40 +00:00
so many changes
This commit is contained in:
parent
6e37e541d6
commit
e08f3c4275
@ -8,6 +8,7 @@ import pandas as pd
|
||||
import requests as r
|
||||
import schedule
|
||||
from fuzzywuzzy import fuzz
|
||||
import os
|
||||
|
||||
|
||||
class IEX_Symbol:
|
||||
@ -20,7 +21,7 @@ class IEX_Symbol:
|
||||
searched_symbols = {}
|
||||
charts = {}
|
||||
|
||||
def __init__(self, IEX_TOKEN: str) -> None:
|
||||
def __init__(self) -> None:
|
||||
"""Creates a Symbol Object
|
||||
|
||||
Parameters
|
||||
@ -28,8 +29,15 @@ class IEX_Symbol:
|
||||
IEX_TOKEN : str
|
||||
IEX Token
|
||||
"""
|
||||
self.IEX_TOKEN = IEX_TOKEN
|
||||
if IEX_TOKEN != "":
|
||||
try:
|
||||
self.IEX_TOKEN = os.environ["IEX"]
|
||||
except KeyError:
|
||||
self.IEX_TOKEN = ""
|
||||
print(
|
||||
"Starting without an IEX Token will not allow you to get market data!"
|
||||
)
|
||||
|
||||
if self.IEX_TOKEN != "":
|
||||
self.get_symbol_list()
|
||||
|
||||
schedule.every().day.do(self.get_symbol_list)
|
||||
@ -148,7 +156,7 @@ class IEX_Symbol:
|
||||
markdown formatted string of the symbols price and movement.
|
||||
"""
|
||||
|
||||
IEXurl = f"https://cloud.iexapis.com/stable/stock/{symbol}/quote?token={self.IEX_TOKEN}"
|
||||
IEXurl = f"https://cloud.iexapis.com/stable/stock/{symbol.id}/quote?token={self.IEX_TOKEN}"
|
||||
|
||||
response = r.get(IEXurl)
|
||||
if response.status_code == 200:
|
||||
|
27
Symbol.py
Normal file
27
Symbol.py
Normal file
@ -0,0 +1,27 @@
|
||||
import requests as r
|
||||
|
||||
|
||||
class Symbol:
|
||||
currency = "usd"
|
||||
pass
|
||||
|
||||
|
||||
class Stock(Symbol):
|
||||
def __init__(self, symbol) -> None:
|
||||
self.symbol = symbol
|
||||
|
||||
|
||||
class Coin(Symbol):
|
||||
def __init__(self, symbol) -> None:
|
||||
self.symbol = symbol
|
||||
self.get_data()
|
||||
|
||||
def get_data(self) -> None:
|
||||
data = r.get("https://api.coingecko.com/api/v3/coins/" + self.symbol).json()
|
||||
|
||||
self.id = data["id"]
|
||||
self.name = data["name"]
|
||||
self.description = data["description"]
|
||||
self.price = data["market_data"]["current_price"][self.currency]
|
||||
|
||||
self.data = data
|
34
bot.py
34
bot.py
@ -31,18 +31,13 @@ from T_info import T_info
|
||||
|
||||
TELEGRAM_TOKEN = os.environ["TELEGRAM"]
|
||||
|
||||
try:
|
||||
IEX_TOKEN = os.environ["IEX"]
|
||||
except KeyError:
|
||||
IEX_TOKEN = ""
|
||||
print("Starting without an IEX Token will not allow you to get market data!")
|
||||
try:
|
||||
STRIPE_TOKEN = os.environ["STRIPE"]
|
||||
except KeyError:
|
||||
STRIPE_TOKEN = ""
|
||||
print("Starting without a STRIPE Token will not allow you to accept Donations!")
|
||||
|
||||
s = Router(IEX=IEX_TOKEN)
|
||||
s = Router()
|
||||
t = T_info()
|
||||
|
||||
# Enable logging
|
||||
@ -160,11 +155,11 @@ def symbol_detect(update: Update, context: CallbackContext):
|
||||
if symbols:
|
||||
# Let user know bot is working
|
||||
context.bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
|
||||
|
||||
for reply in s.price_reply(symbols).items():
|
||||
|
||||
print(symbols)
|
||||
for reply in s.price_reply(symbols):
|
||||
print(reply)
|
||||
update.message.reply_text(
|
||||
text=reply[1], parse_mode=telegram.ParseMode.MARKDOWN
|
||||
text=reply, parse_mode=telegram.ParseMode.MARKDOWN
|
||||
)
|
||||
|
||||
|
||||
@ -270,7 +265,7 @@ def intra(update: Update, context: CallbackContext):
|
||||
)
|
||||
return
|
||||
|
||||
symbol = s.find_symbols(message)[0]
|
||||
symbol = s.find_symbols(message)
|
||||
|
||||
df = s.intra_reply(symbol)
|
||||
if df.empty:
|
||||
@ -289,7 +284,7 @@ def intra(update: Update, context: CallbackContext):
|
||||
df,
|
||||
type="renko",
|
||||
title=f"\n${symbol.upper()}",
|
||||
volume=True,
|
||||
volume="volume" in df.keys(),
|
||||
style="yahoo",
|
||||
mav=20,
|
||||
savefig=dict(fname=buf, dpi=400, bbox_inches="tight"),
|
||||
@ -317,9 +312,9 @@ def chart(update: Update, context: CallbackContext):
|
||||
)
|
||||
return
|
||||
|
||||
symbol = s.find_symbols(message)[0]
|
||||
symbols = s.find_symbols(message)
|
||||
|
||||
df = s.chart_reply(symbol)
|
||||
df, symbol = s.chart_reply(symbols)
|
||||
if df.empty:
|
||||
update.message.reply_text(
|
||||
text="Invalid symbol please see `/help` for usage details.",
|
||||
@ -330,13 +325,13 @@ def chart(update: Update, context: CallbackContext):
|
||||
context.bot.send_chat_action(
|
||||
chat_id=chat_id, action=telegram.ChatAction.UPLOAD_PHOTO
|
||||
)
|
||||
|
||||
print(symbol)
|
||||
buf = io.BytesIO()
|
||||
mpf.plot(
|
||||
df,
|
||||
type="candle",
|
||||
title=f"\n${symbol.upper()}",
|
||||
volume=True,
|
||||
volume="volume" in df.keys(),
|
||||
style="yahoo",
|
||||
savefig=dict(fname=buf, dpi=400, bbox_inches="tight"),
|
||||
)
|
||||
@ -345,7 +340,7 @@ def chart(update: Update, context: CallbackContext):
|
||||
update.message.reply_photo(
|
||||
photo=buf,
|
||||
caption=f"\n1 Month chart for ${symbol.upper()} from {df.first_valid_index().strftime('%d, %b %Y')}"
|
||||
+ f" to {df.last_valid_index().strftime('%d, %b %Y')}\n\n{s.price_reply([symbol])[symbol]}",
|
||||
+ f" to {df.last_valid_index().strftime('%d, %b %Y')}\n\n{s.price_reply(symbols)[0]}",
|
||||
parse_mode=telegram.ParseMode.MARKDOWN,
|
||||
)
|
||||
|
||||
@ -451,7 +446,8 @@ def error(update: Update, context: CallbackContext):
|
||||
None, context.error, context.error.__traceback__
|
||||
)
|
||||
tb_string = "".join(tb_list)
|
||||
|
||||
print(tb_string)
|
||||
if update:
|
||||
message = (
|
||||
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))}"
|
||||
@ -464,6 +460,8 @@ def error(update: Update, context: CallbackContext):
|
||||
# Finally, send the message
|
||||
update.message.reply_text(text=message, parse_mode=telegram.ParseMode.HTML)
|
||||
update.message.reply_text(text="Please inform the bot admin of this issue.")
|
||||
print("-" * 50)
|
||||
print(tb_string)
|
||||
|
||||
|
||||
def main():
|
||||
|
@ -119,7 +119,7 @@ class cg_Crypto:
|
||||
"""
|
||||
|
||||
response = r.get(
|
||||
f"https://api.coingecko.com/api/v3/coins/{symbol}?localization=false"
|
||||
f"https://api.coingecko.com/api/v3/coins/{symbol.id}?localization=false"
|
||||
)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
@ -189,6 +189,7 @@ class cg_Crypto:
|
||||
response = r.get(
|
||||
"https://api.coingecko.com/api/v3/coins/{symbol}/ohlc?vs_currency=usd&days=30"
|
||||
)
|
||||
print(response.status_code)
|
||||
if response.status_code == 200:
|
||||
df = pd.DataFrame(
|
||||
response.json(), columns=["Date", "Open", "High", "Low", "Close"]
|
||||
|
@ -2,6 +2,7 @@
|
||||
"""
|
||||
|
||||
import re
|
||||
import requests as r
|
||||
import pandas as pd
|
||||
|
||||
from typing import List, Dict
|
||||
@ -14,8 +15,8 @@ class Router:
|
||||
STOCK_REGEX = "(?:^|[^\\$])\\$([a-zA-Z]{1,4})"
|
||||
CRYPTO_REGEX = "[$]{2}([a-zA-Z]{1,9})"
|
||||
|
||||
def __init__(self, IEX_TOKEN):
|
||||
self.symbol = IEX_Symbol(IEX_TOKEN)
|
||||
def __init__(self):
|
||||
self.stock = IEX_Symbol()
|
||||
self.crypto = cg_Crypto()
|
||||
|
||||
def find_symbols(self, text: str) -> Dict[str, str]:
|
||||
@ -33,11 +34,20 @@ class Router:
|
||||
List[str]
|
||||
List of stock symbols as strings without dollar sign.
|
||||
"""
|
||||
symbols = {}
|
||||
symbols["stocks"] = list(set(re.findall(self.STOCK_REGEX, text)))
|
||||
symbols["crypto"] = [
|
||||
self.crypto.symbol_id(c) for c in set(re.findall(self.CRYPTO_REGEX, text))
|
||||
]
|
||||
symbols = []
|
||||
stocks = set(re.findall(self.STOCK_REGEX, text))
|
||||
for stock in stocks:
|
||||
if stock.upper() in self.stock.symbol_list["symbol"].values:
|
||||
symbols.append(Stock(stock))
|
||||
else:
|
||||
print(f"{stock} is not in list of stocks")
|
||||
|
||||
coins = set(re.findall(self.CRYPTO_REGEX, text))
|
||||
for coin in coins:
|
||||
if coin.lower() in self.crypto.symbol_list["symbol"].values:
|
||||
symbols.append(Coin(coin.lower()))
|
||||
else:
|
||||
print(f"{coin} is not in list of coins")
|
||||
|
||||
return symbols
|
||||
|
||||
@ -64,7 +74,7 @@ class Router:
|
||||
A list tuples of every stock sorted in order of how well they match. Each tuple contains: (Symbol, Issue Name).
|
||||
"""
|
||||
# TODO add support for crypto
|
||||
return self.symbol.find_symbols(str)
|
||||
return self.stock.find_symbols(search)
|
||||
|
||||
def price_reply(self, symbols: dict) -> List[str]:
|
||||
"""Returns current market price or after hours if its available for a given stock symbol.
|
||||
@ -82,14 +92,14 @@ class Router:
|
||||
"""
|
||||
replies = []
|
||||
|
||||
if symbols["stocks"]:
|
||||
for s in symbols["stocks"]:
|
||||
replies.append(self.symbol.price_reply(s))
|
||||
|
||||
if symbols["crypto"]:
|
||||
for s in symbols["crypto"]:
|
||||
replies.append(self.crypto.price_reply(s))
|
||||
|
||||
for symbol in symbols:
|
||||
if isinstance(symbol, Stock):
|
||||
replies.append(self.stock.price_reply(symbol))
|
||||
elif isinstance(symbol, Coin):
|
||||
replies.append(self.crypto.price_reply(symbol))
|
||||
else:
|
||||
print(f"{symbol} is not a Stock or Coin")
|
||||
print(replies)
|
||||
return replies
|
||||
|
||||
def dividend_reply(self, symbols: dict) -> Dict[str, str]:
|
||||
@ -109,7 +119,7 @@ class Router:
|
||||
|
||||
if symbols["stocks"]:
|
||||
for s in symbols["stocks"]:
|
||||
replies.append(self.symbol.price_reply(s))
|
||||
replies.append(self.stock.price_reply(s))
|
||||
|
||||
if symbols["crypto"]:
|
||||
replies.append("Cryptocurrencies do no have Dividends.")
|
||||
@ -131,7 +141,7 @@ class Router:
|
||||
|
||||
if symbols["stocks"]:
|
||||
for s in symbols["stocks"]:
|
||||
replies.append(self.symbol.price_reply(s))
|
||||
replies.append(self.stock.price_reply(s))
|
||||
|
||||
if symbols["crypto"]:
|
||||
for s in symbols["crypto"]:
|
||||
@ -156,7 +166,7 @@ class Router:
|
||||
|
||||
if symbols["stocks"]:
|
||||
for s in symbols["stocks"]:
|
||||
replies.append(self.symbol.price_reply(s))
|
||||
replies.append(self.stock.price_reply(s))
|
||||
|
||||
if symbols["crypto"]:
|
||||
for s in symbols["crypto"]:
|
||||
@ -178,7 +188,7 @@ class Router:
|
||||
Returns a timeseries dataframe with high, low, and volume data if its available. Otherwise returns empty pd.DataFrame.
|
||||
"""
|
||||
if type == "stocks":
|
||||
return self.symbol.intra_reply(symbol)
|
||||
return self.stock.intra_reply(symbol)
|
||||
elif type == "crypto":
|
||||
return self.crypto.intra_reply(symbol)
|
||||
else:
|
||||
@ -199,9 +209,9 @@ class Router:
|
||||
Returns a timeseries dataframe with high, low, and volume data if its available. Otherwise returns empty pd.DataFrame.
|
||||
"""
|
||||
if symbols["stocks"]:
|
||||
return self.symbol.intra_reply(symbol := symbols["stocks"][0]), symbol
|
||||
return self.stock.intra_reply(symbol := symbols["stocks"][0]), symbol
|
||||
if symbols["crypto"]:
|
||||
return self.symbol.intra_reply(symbol := symbols["crypto"][0]), symbol
|
||||
return self.stock.intra_reply(symbol := symbols["crypto"][0]), symbol
|
||||
|
||||
def stat_reply(self, symbols: List[str]) -> Dict[str, str]:
|
||||
"""Gets key statistics for each symbol in the list
|
||||
@ -220,8 +230,37 @@ class Router:
|
||||
|
||||
if symbols["stocks"]:
|
||||
for s in symbols["stocks"]:
|
||||
replies.append(self.symbol.price_reply(s))
|
||||
replies.append(self.stock.price_reply(s))
|
||||
|
||||
if symbols["crypto"]:
|
||||
for s in symbols["crypto"]:
|
||||
replies.append(self.crypto.price_reply(s))
|
||||
|
||||
|
||||
class Symbol:
|
||||
currency = "usd"
|
||||
pass
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"{self.__class__.__name__} instance of {self.id} at {id(self)}"
|
||||
|
||||
|
||||
class Stock(Symbol):
|
||||
def __init__(self, symbol) -> None:
|
||||
self.symbol = symbol
|
||||
self.id = symbol
|
||||
|
||||
|
||||
class Coin(Symbol):
|
||||
def __init__(self, symbol) -> None:
|
||||
self.symbol = symbol
|
||||
self.get_data()
|
||||
|
||||
def get_data(self) -> None:
|
||||
self.id = cg_Crypto().symbol_id(self.symbol)
|
||||
data = r.get("https://api.coingecko.com/api/v3/coins/" + self.id).json()
|
||||
self.data = data
|
||||
|
||||
self.name = data["name"]
|
||||
self.description = data["description"]
|
||||
self.price = data["market_data"]["current_price"][self.currency]
|
Loading…
x
Reference in New Issue
Block a user