def main(): parser = argparse.ArgumentParser(description='scrap yahoo earning') parser.add_argument( '-output_dir', type=str, default='../stock_data_local/raw_history_investing_stock/', help='output directory') parser.add_argument('-skip', type=int, help='skip tickers') args = parser.parse_args() country = 'united states' stocks_dict = investpy.get_stocks_dict(country) today = datetime.datetime.now().strftime('%d/%m/%Y') for count, stock_row in enumerate(stocks_dict): if args.skip is not None: if count < args.skip: continue ticker = stock_row['symbol'] print('downloading...', ticker, '-', count, '/', len(stocks_dict)) try: df_stock = investpy.get_stock_historical_data( ticker, country, '1/1/1990', today) try: df_dividend = investpy.get_stock_dividends(ticker, country) df_dividend.set_index('Date', inplace=True) df = pd.concat([df_stock, df_dividend], axis=1) except Exception as e: print(e) df = df_stock df.to_csv(args.output_dir + ticker + '.csv') except Exception as e: print(e) time.sleep(scrap_delay) return 0
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
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')
def __get_stock_info(self, stock): """ Method to get the stock information once it is validated. This method retrieves the historical data of the introduced Stock in order to get its current price which will be used for the required calculations to generate the StockPortfolio. So on, this function is both going to retrieve Stock data and calculate the required values for the StockPortfolio. Args: stock (:obj:`pyrtfolio.Stock`): Stock object with all its information after validated. Returns: :obj:`dict` - stock_information: Returns a :obj:`dict` which contains all the values from the introduced Stock in order to create its portfolio row. """ stocks = investpy.get_stocks(country=stock.stock_country) stock_name = stocks.loc[(stocks['symbol'].str.lower() == stock.stock_symbol.lower()).idxmax(), 'name'] data = investpy.get_stock_historical_data( stock=stock.stock_symbol, country=stock.stock_country, from_date=stock.purchase_date, to_date=date.today().strftime("%d/%m/%Y")) currency = data['Currency'][0] purchase_cost = self.calculate_purchase_cost( cost_per_share=stock.cost_per_share, num_of_shares=stock.num_of_shares) current_price = self.get_current_price(data=data) gross_current_value = self.calculate_gross_current_value( current_price=current_price, num_of_shares=stock.num_of_shares) dividends = investpy.get_stock_dividends(stock=stock.stock_symbol, country=stock.stock_country) dividends = dividends.loc[dividends['Payment Date'] < pd.to_datetime( stock.purchase_date, dayfirst=True)].reset_index(drop=True) if len(dividends) > 0: total_dividends = self.calculate_total_dividends( dividends=dividends, num_of_shares=stock.num_of_shares) else: total_dividends = 0 net_current_value = self.calculate_net_current_value( gross_current_value=gross_current_value, total_dividends=total_dividends) total_gain_loss = self.calculate_total_gain_loss( net_current_value=net_current_value, purchase_cost=purchase_cost) total_gain_loss_percentage = self.calculate_total_gain_loss_percentage( total_gain_loss=total_gain_loss, purchase_cost=purchase_cost) info = { 'stock_symbol': stock.stock_symbol, 'stock_name': stock_name, 'stock_country': stock.stock_country, 'stock_currency': currency, 'purchase_date': stock.purchase_date, 'num_of_shares': stock.num_of_shares, 'cost_per_share': stock.cost_per_share, 'purchase_cost': purchase_cost, 'current_price': current_price, 'gross_current_value': gross_current_value, 'total_dividends': total_dividends, 'net_current_value': net_current_value, 'total_gain_loss': total_gain_loss, 'total_gain_loss_percentage': total_gain_loss_percentage, } return info