def do_echo(bot: Bot, update: Update): chat_id = update.message.chat_id text = update.message.text if text == BUTTON2_APTEKA36_6: df = investpy.get_stock_recent_data(stock='aptk', country='russia') bot.send_message( chat_id=chat_id, text="На открытии Курс акций Аптека36.6 : {} {}, на закрытии : {} {}" " Изменение составило {} {}".format( df['Open'][-1], df['Currency'][-1], df['Close'][-1], df['Currency'][-1], "%2f" % (df['Close'][-1] - df['Open'][-1]), df['Currency'][-1]), ) elif text == BUTTON1_YANDEX: df = investpy.get_stock_recent_data(stock='yndx', country='russia') bot.send_message( chat_id=chat_id, text="На открытии Курс Яндекса : {} {}, на закрытии : {} {}" " Изменение составило {} {}".format( df['Open'][-1], df['Currency'][-1], df['Close'][-1], df['Currency'][-1], "%2f" % (df['Close'][-1] - df['Open'][-1]), df['Currency'][-1]), ) elif text == BUTTON3_SBERBANK: df = investpy.get_stock_recent_data(stock='SBER', country='russia') bot.send_message( chat_id=chat_id, text="На открытии курс Сбербанка : {} {}, на закрытии : {} {}" " Изменение составило {} {}".format( df['Open'][-1], df['Currency'][-1], df['Close'][-1], df['Currency'][-1], "%2f" % (df['Close'][-1] - df['Open'][-1]), df['Currency'][-1]), ) elif text == BUTTON4_MCDONALDS: df = investpy.get_stock_recent_data(stock='MCD', country='United States') bot.send_message( chat_id=chat_id, text="На открытии курс McDonalds : {} {}, на закрытии : {} {}" " Изменение составило {} {}".format( df['Open'][-1], df['Currency'][-1], df['Close'][-1], df['Currency'][-1], "%2f" % (df['Close'][-1] - df['Open'][-1]), df['Currency'][-1]), )
def getstockValue(stock, country): df = investpy.get_stock_recent_data(stock=stock, country=country, as_json=False, order='ascending') a = df.last('1D')['Close'].item() b = df.last('1D')['Currency'].item() c = CurrencyRates() try: rate = c.get_rates(b)['EUR'] return a*rate except: return a
def get_current_data(symbol): """ Function returns stock data of latest trading day for given stock symbol """ try: day_data = investpy.get_stock_recent_data(symbol, "india") current_day_data = day_data.iloc[-1] return current_day_data except IndexError: return False
def get_price_investpy(sname, sticker, stype, scountry): if stype == "Stock": try: df = investpy.get_stock_recent_data(sname, scountry) except RuntimeError: try: df = investpy.get_stock_recent_data(sticker, scountry) except RuntimeError: raise MethodNotWorkingError elif stype == "Fund": try: df = investpy.get_fund_recent_data(sname, scountry) except RuntimeError: try: df = investpy.get_fund_recent_data(sticker, scountry) except RuntimeError: raise MethodNotWorkingError elif stype == "ETF": try: df = investpy.get_etf_recent_data(sname, scountry) except RuntimeError: try: df = investpy.get_etf_recent_data(sticker, scountry) except RuntimeError: raise MethodNotWorkingError elif stype == "Crypto": try: df = investpy.get_crypto_recent_data(sname) except RuntimeError: try: df = investpy.get_crypto_recent_data(sticker) except RuntimeError: raise MethodNotWorkingError else: raise MethodNotWorkingError return df.iloc[-1]['Close']
def extraer(cond, stock_valor, pais, pi="", pf=""): """ Se extraen los datos del precio final diaro del valor """ if cond == 1: # Se extraen los datos del precio final diario del valor en el rango de días dado df = investpy.get_stock_historical_data(stock_valor, pais, pi, pf) else: # Se extraen los datos del precio final diario del valor en el último mes del valor df = investpy.get_stock_recent_data(stock_valor, pais, as_json=False, order='ascending') return df
def test_stocks_errors(): """ This function raises errors on stock retrieval functions """ try: retrieve_stocks(test_mode=None) except: pass try: retrieve_stock_countries(test_mode=None) except: pass params = [ { 'country': ['error'] }, { 'country': 'error' }, ] for param in params: try: investpy.get_stocks(country=param['country']) except: pass try: investpy.get_stocks_list(country=param['country']) except: pass params = [ { 'country': ['error'], 'columns': None, 'as_json': False }, { 'country': 'spain', 'columns': None, 'as_json': 'error' }, { 'country': 'spain', 'columns': 0, 'as_json': True }, { 'country': 'spain', 'columns': ['error'], 'as_json': False }, ] for param in params: try: investpy.get_stocks_dict(country=param['country'], columns=param['columns'], as_json=param['as_json']) except: pass params = [ { 'stock': 'FERR', 'country': 'spain', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'stock': None, 'country': 'spain', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'stock': 'BBVA', 'country': None, 'as_json': False, 'order': 'ascending', 'debug': True }, { 'stock': 'BBVA', 'country': ['error'], 'as_json': False, 'order': 'ascending', 'debug': True }, { 'stock': 'BBVA', 'country': 'greece', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'stock': 'BBVA', 'country': 'spain', 'as_json': 'error', 'order': 'ascending', 'debug': True }, { 'stock': 'BBVA', 'country': 'spain', 'as_json': True, 'order': 'error', 'debug': True }, { 'stock': 'error', 'country': 'spain', 'as_json': True, 'order': 'ascending', 'debug': True }, { 'stock': ['error'], 'country': 'spain', 'as_json': True, 'order': 'ascending', 'debug': True }, { 'stock': 'BBVA', 'country': 'spain', 'as_json': True, 'order': 'ascending', 'debug': 'error' }, ] for param in params: try: investpy.get_stock_recent_data(stock=param['stock'], country=param['country'], as_json=param['as_json'], order=param['order'], debug=param['debug']) except: pass params = [ { 'stock': 'FERR', 'country': 'spain', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'stock': None, 'country': 'spain', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'stock': 'BBVA', 'country': None, 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'stock': 'BBVA', 'country': ['error'], 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'stock': 'BBVA', 'country': 'greece', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'stock': 'BBVA', 'country': 'spain', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': 'error', 'order': 'ascending', 'debug': True }, { 'stock': 'BBVA', 'country': 'spain', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'error', 'debug': True }, { 'stock': 'BBVA', 'country': 'spain', 'from_date': 'error', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'stock': 'BBVA', 'country': 'spain', 'from_date': '01/01/2019', 'to_date': 'error', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'stock': 'error', 'country': 'spain', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'stock': ['error'], 'country': 'spain', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'stock': 'BBVA', 'country': 'spain', 'from_date': '01/01/1999', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'stock': 'BBVA', 'country': 'spain', 'from_date': '01/01/1900', 'to_date': '01/01/1950', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'stock': 'BBVA', 'country': 'spain', 'from_date': '01/01/1950', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'stock': 'BBVA', 'country': 'spain', 'from_date': '01/01/2019', 'to_date': '01/01/1999', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'stock': 'BBVA', 'country': 'spain', 'from_date': '01/01/2019', 'to_date': '01/03/2019', 'as_json': False, 'order': 'ascending', 'debug': 'error' }, ] for param in params: try: investpy.get_stock_historical_data(stock=param['stock'], country=param['country'], from_date=param['from_date'], to_date=param['to_date'], as_json=param['as_json'], order=param['order'], debug=param['debug']) except: pass params = [ { 'stock': None, 'country': 'spain', 'language': 'spanish' }, { 'stock': 'BBVA', 'country': None, 'language': 'spanish' }, { 'stock': 'BBVA', 'country': 'greece', 'language': 'spanish' }, { 'stock': 'BBVA', 'country': 'spain', 'language': 'error' }, { 'stock': 'error', 'country': 'spain', 'language': 'spanish' }, { 'stock': ['error'], 'country': 'spain', 'language': 'spanish' }, ] for param in params: try: investpy.get_stock_company_profile(stock=param['stock'], country=param['country'], language=param['language']) except: pass params = [ { 'stock': None, 'country': 'spain', }, { 'stock': ['error'], 'country': 'spain', }, { 'stock': 'bbva', 'country': ['error'], }, { 'stock': 'bbva', 'country': 'error', }, { 'stock': 'error', 'country': 'spain', }, { 'stock': 'ALUA', 'country': 'argentina', }, ] for param in params: try: investpy.get_stock_dividends(stock=param['stock'], country=param['country']) except: pass params = [ { 'by': None, 'value': 'BBVA', }, { 'by': ['error'], 'value': 'BBVA', }, { 'by': 'error', 'value': 'BBVA', }, { 'by': 'name', 'value': None, }, { 'by': 'name', 'value': ['error'], }, { 'by': 'isin', 'value': 'BBVA', }, ] for param in params: try: investpy.search_stocks(by=param['by'], value=param['value']) except: pass
import numpy as np import investpy import pandas as pd from cs50 import get_string import plotly.express as px ## Getting the symbol from user symbol = get_string("Symbol is ") print(symbol) ## Getting stocks' open price, close price, low price, close price, volume and currency with investpy df = investpy.get_stock_recent_data(stock=symbol, country='United States', as_json=False, order='ascending') ## Changing Pandas' database to csv df.to_csv('file.csv', index=True, encoding='utf-8') csv_file = 'file.csv' df = pd.read_csv(csv_file) print(df) ## Plotting close price against time fig = px.line(df, x='Date', y='Close', title='Apple Share Prices over a month') fig.show() ## Plotting volume traded against time fig1 = px.line(df, x='Date', y='Volume', title='Volume traded over the past month')
def test_investpy_stocks(): """ This function checks that stock data retrieval functions listed in investpy work properly. """ params = [ { 'country': 'spain', }, { 'country': None, }, ] for param in params: investpy.get_stocks(country=param['country']) investpy.get_stocks_list(country=param['country']) params = [ { 'country': None, 'columns': ['full_name', 'name'], 'as_json': True }, { 'country': None, 'columns': ['full_name', 'name'], 'as_json': False }, { 'country': 'spain', 'columns': ['full_name', 'name'], 'as_json': True }, { 'country': 'spain', 'columns': ['full_name', 'name'], 'as_json': False }, { 'country': 'spain', 'columns': None, 'as_json': False }, ] for param in params: investpy.get_stocks_dict(country=param['country'], columns=param['columns'], as_json=param['as_json']) investpy.get_stock_countries() params = [ { 'as_json': True, 'order': 'ascending', }, { 'as_json': False, 'order': 'ascending', }, { 'as_json': True, 'order': 'descending', }, { 'as_json': False, 'order': 'descending', }, ] for param in params: investpy.get_stock_recent_data(stock='BBVA', country='spain', as_json=param['as_json'], order=param['order'], interval='Daily') investpy.get_stock_historical_data(stock='BBVA', country='spain', from_date='01/01/1990', to_date='01/01/2019', as_json=param['as_json'], order=param['order'], interval='Daily') for value in ['spanish', 'english']: investpy.get_stock_company_profile(stock='BBVA', country='spain', language=value) params = [ { 'stock': 'bbva', 'country': 'spain', 'as_json': False }, { 'stock': 'bbva', 'country': 'spain', 'as_json': True }, { 'stock': 'HSBK', 'country': 'kazakhstan', 'as_json': False } ] for param in params: investpy.get_stock_information(stock=param['stock'], country=param['country'], as_json=param['as_json']) params = [ { 'country': 'spain', 'as_json': True, 'n_results': 50 }, { 'country': 'united states', 'as_json': False, 'n_results': 50 }, { 'country': 'bosnia', 'as_json': False, 'n_results': 50 }, { 'country': 'palestine', 'as_json': False, 'n_results': 50 }, { 'country': 'dubai', 'as_json': False, 'n_results': 50 }, { 'country': 'ivory coast', 'as_json': False, 'n_results': 50 } ] for param in params: investpy.get_stocks_overview(country=param['country'], as_json=param['as_json'], n_results=param['n_results']) params = [ { 'stock': 'bbva', 'country': 'spain' }, { 'stock': 'entel', 'country': 'chile' } ] for param in params: investpy.get_stock_dividends(stock=param['stock'], country=param['country']) params = [ { 'stock': 'bbva', 'country': 'spain', 'summary_type': 'balance_sheet', 'period': 'annual' }, { 'stock': 'aapl', 'country': 'united states', 'summary_type': 'income_statement', 'period': 'quarterly' }, { 'stock': 'barc', 'country': 'united kingdom', 'summary_type': 'cash_flow_statement', 'period': 'annual' } ] for param in params: investpy.get_stock_financial_summary(stock=param['stock'], country=param['country'], summary_type=param['summary_type'], period=param['period']) investpy.search_stocks(by='name', value='BBVA')
def test_investpy_stocks(): """ This function checks that stock data retrieval functions listed in investpy work properly. """ params = [ { 'country': 'spain', }, { 'country': None, }, ] for param in params: investpy.get_stocks(country=param['country']) investpy.get_stocks_list(country=param['country']) params = [ { 'country': None, 'columns': ['full_name', 'name'], 'as_json': True }, { 'country': None, 'columns': ['full_name', 'name'], 'as_json': False }, { 'country': 'spain', 'columns': ['full_name', 'name'], 'as_json': True }, { 'country': 'spain', 'columns': ['full_name', 'name'], 'as_json': False }, { 'country': 'spain', 'columns': None, 'as_json': False }, ] for param in params: investpy.get_stocks_dict(country=param['country'], columns=param['columns'], as_json=param['as_json']) investpy.get_stock_countries() params = [ { 'as_json': True, 'order': 'ascending', 'debug': False }, { 'as_json': False, 'order': 'ascending', 'debug': True }, { 'as_json': True, 'order': 'descending', 'debug': False }, { 'as_json': False, 'order': 'descending', 'debug': False }, ] for param in params: investpy.get_stock_recent_data(stock='BBVA', country='spain', as_json=param['as_json'], order=param['order'], debug=param['debug']) investpy.get_stock_historical_data(stock='BBVA', country='spain', from_date='01/01/1990', to_date='01/01/2019', as_json=param['as_json'], order=param['order'], debug=param['debug']) for value in ['spanish', 'english']: investpy.get_stock_company_profile(stock='BBVA', country='spain', language=value) investpy.get_stock_dividends(stock='BBVA', country='spain') investpy.search_stocks(by='name', value='BBVA')
print(f'ticker {ticker} SUCCESS.....') time.sleep(13) #%% import investpy # https://investpy.readthedocs.io/_info/introduction.html # Retrieve all the available stocks as a Python list stocks = investpy.get_stocks_list() funds = investpy.get_funds_list() etfs = investpy.get_etfs_list() # Retrieve the recent historical data (past month) of a stock as a pandas.DataFrame on ascending date order df_bbva = investpy.get_stock_recent_data(stock='bbva', country='spain', as_json=False, order='ascending') # Retrieve the company profile of the introduced stock on english profile = investpy.get_stock_company_profile(stock='bbva', country='spain', language='english') df_aapl = investpy.get_stock_historical_data(stock='AAPL', country='United States', from_date='01/01/2010', to_date='01/01/2020') df_fund = investpy.get_fund_historical_data( fund='bbva plan multiactivo moderado pp', country='spain',
import investpy import yaml with open('stock.yaml') as f: data = yaml.load(f, Loader=yaml.FullLoader) for item in data["stocks"]: print(item["name"]) df = investpy.get_stock_recent_data(stock=item["name"],country=item["country"]) print(df) for item in data["etfs"]: print(item["name"]) df = investpy.get_etf_recent_data(etf=item["name"],country=item["country"]) print(df) # df = investpy.get_stock_recent_data(stock='PEUP',country='france') # print(df) # search_results = investpy.search_etfs(by='isin', value='LU1681043599') # print(search_results) # df = investpy.get_etf_recent_data(etf='Amundi MSCI World UCITS',country='france') # print(df) # df = investpy.get_etf_historical_data(etf='Amundi MSCI World UCITS',country='france',from_date='01/12/2019',to_date='06/02/2020') # print(df)
df3 = pandas.DataFrame({ 'apple': apple["adjclose"].pct_change(), 'mcrsft': mcrsft["adjclose"].pct_change(), 'DJI': DJI["adjclose"].pct_change()}) dfna = df3.dropna() #investing.com #pip install investpy==0.9.7 import investpy df = investpy.get_stock_recent_data(stock='BBVA', country='spain') print(df.head()) df = investpy.get_stock_historical_data(stock='BBVA', country='spain', from_date='01/01/2010', to_date='01/01/2019') print(df.head()) df = investpy.get_indices_historical_data(indices='ise-100', country='turkey', from_date='01/01/2010', to_date='01/01/2019') print(df.head())
def get_stock_price(ticker): return investpy.get_stock_recent_data(stock=ticker, country='iceland', as_json=False, order='ascending')['Close'][-1]