mirror of
https://gitlab.com/simple-stock-bots/simple-telegram-stock-bot.git
synced 2025-07-23 14:41:26 +00:00
so many changes
This commit is contained in:
@@ -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]
|
Reference in New Issue
Block a user