TelegramAktienBot/telegram_bot/shares/share_fetcher.py

137 lines
4.7 KiB
Python
Raw Normal View History

2022-03-15 07:55:12 +00:00
"""
script for share fetching (by symbols (e.g. AAPL, TSLA etc.))
"""
__author__ = "Florian Kellermann, Linus Eickhoff"
2022-05-10 15:37:20 +00:00
__date__ = "10.05.2022"
2022-05-11 21:33:48 +00:00
__version__ = "1.0.1"
__license__ = "None"
2022-05-11 21:33:48 +00:00
import helper_functions as hf
import investpy
import pandas
from currency_converter import CurrencyConverter
2022-05-11 21:33:48 +00:00
def get_share_price(str_search_for):
2022-05-07 17:34:54 +00:00
"""get stock price per share for company name or isin or symbol
Args:
2022-05-07 17:38:17 +00:00
str_search_for (string): search for this string/isin
2022-05-07 17:38:17 +00:00
Returns: none
"""
try:
search_result = investpy.search_quotes(text=str_search_for, products=['stocks'], n_results=1, countries=['germany'])
2022-05-11 07:40:08 +00:00
currency = str(search_result.retrieve_currency()) # retrieve currency from data
# should always be Euro because of countries=['germany']
2022-05-11 07:40:08 +00:00
recent_data = pandas.DataFrame(search_result.retrieve_recent_data()) # stock prices of last few days
2022-05-11 07:40:08 +00:00
stock_price = recent_data.iloc[-1]["Close"] # retrieve latest stock price
stock_price = round(float(stock_price), 2)
2022-05-11 07:40:08 +00:00
str_return =str(stock_price) + " " + str(currency) # return + currency
return str_return
2022-05-11 07:40:08 +00:00
except RuntimeError: # if no shares are found for germany (e.g. isin: US.....)
try:
2022-05-11 07:40:08 +00:00
my_Converter = CurrencyConverter() # need a currency converter
search_result = investpy.search_quotes(text=str_search_for, products=['stocks'], n_results=1)
2022-05-11 21:33:48 +00:00
currency = str(search_result.retrieve_currency())
2022-05-11 21:33:48 +00:00
recent_data = pandas.DataFrame(search_result.retrieve_recent_data())
2022-05-11 21:33:48 +00:00
stock_price = recent_data.iloc[-1]["Close"]
2022-05-11 07:40:08 +00:00
#convert stock price from currency to EUR
stock_price = my_Converter.convert(float(stock_price), str(currency), 'EUR')
stock_price = round(float(stock_price), 2)
2022-05-11 21:33:48 +00:00
str_return = str(stock_price) + " EUR"
return str_return
except RuntimeError:
return "None"
2022-05-11 21:33:48 +00:00
2022-05-07 17:33:02 +00:00
def get_share_price_no_currency(str_search_for):
2022-05-07 17:38:17 +00:00
"""get stock price per share for company name or isin or symbol no currency
2022-05-07 17:33:02 +00:00
Args:
2022-05-07 17:38:17 +00:00
str_search_for (string): search for this string/isin
Returns: none
2022-05-07 17:33:02 +00:00
"""
try:
search_result = investpy.search_quotes(text=str_search_for, products=['stocks'],
2022-05-11 21:33:48 +00:00
countries=['germany'], n_results=1)
recent_data = pandas.DataFrame(search_result.retrieve_recent_data())
2022-05-11 21:33:48 +00:00
stock_price = recent_data.iloc[-1]["Close"]
2022-05-11 21:33:48 +00:00
stock_price = round(float(stock_price), 2)
2022-05-11 21:33:48 +00:00
return stock_price
2022-05-11 21:33:48 +00:00
except RuntimeError:
my_Converter = CurrencyConverter()
2022-05-11 21:33:48 +00:00
search_result = investpy.search_quotes(text=str_search_for, products=['stocks'], n_results=1)
2022-05-11 21:33:48 +00:00
currency = str(search_result.retrieve_currency())
2022-05-11 21:33:48 +00:00
recent_data = pandas.DataFrame(search_result.retrieve_recent_data())
2022-05-11 21:33:48 +00:00
stock_price = recent_data.iloc[-1]["Close"]
2022-05-11 21:33:48 +00:00
stock_price = my_Converter.convert(float(stock_price), str(currency), 'EUR')
2022-05-11 21:33:48 +00:00
stock_price = round(float(stock_price), 2)
2022-05-11 21:33:48 +00:00
str_return = str(stock_price)
return str_return
2022-05-07 17:33:02 +00:00
2022-05-11 21:33:48 +00:00
def get_share_information(str_search_for):
search_result = investpy.search_quotes(text=str_search_for, products=['stocks'],
2022-05-11 21:33:48 +00:00
countries=['germany'], n_results=1)
str_return = "Company: " + search_result.name + "\nSymbol: " + search_result.symbol + "\nCurrent Price/Share: " + get_share_price(str_search_for)
2022-05-11 21:33:48 +00:00
return str_return
2022-05-11 21:33:48 +00:00
def get_share_information_markdown(str_search_for):
2022-05-11 15:01:46 +00:00
try:
search_result = investpy.search_quotes(text=str_search_for, products=['stocks'],
countries=['germany'], n_results=1)
except RuntimeError as e:
return hf.make_markdown_proof(f"no shares found for \"{str_search_for}\"") # if no shares are found, make error message markdown proof and return
except ConnectionError as e:
return hf.make_markdown_proof(f"connection not possible. Try again later.") # if no connection, make error message markdown proof and return
2022-05-10 19:51:27 +00:00
str_return = f'*{hf.make_markdown_proof(search_result.name)}*\n_{hf.make_markdown_proof(search_result.symbol)}_\nworth: {hf.make_markdown_proof(get_share_price(str_search_for))}'
return str_return
2022-05-11 21:33:48 +00:00
def get_share_information_simple(str_search_for):
search_result = investpy.search_quotes(text=str_search_for, products=['stocks'],
2022-05-11 21:33:48 +00:00
countries=['germany'], n_results=1)
str_return = search_result.name + "\n" + search_result.symbol + "\nworth: " + get_share_price(str_search_for)
return str_return
2022-05-11 21:33:48 +00:00
if __name__ == "__main__":
2022-05-11 21:33:48 +00:00
print("None")