0

So I got this part of code that I want to make shorter:

df_1 = investpy.stocks.get_stock_recent_data('Eco','Colombia',False)
df_2 = investpy.stocks.get_stock_recent_data('JPM','United States',False)
df_3 = investpy.stocks.get_stock_recent_data('TSM','United States',False)
df_5 = investpy.stocks.get_stock_recent_data('CSCO','United States',False)
df_8 = investpy.stocks.get_stock_recent_data('NVDA','United States',False)
df_9 = investpy.stocks.get_stock_recent_data('BLK','United States',False)

As I use the same code and only a few things change from one line to another I think I migth solve this using a function. I create this one:

def _get_asset_data(ticker, country, state):
    investpy.stocks.get_stock_recent_data(ticker, country, state)

So I tried this:

_get_asset_data('TSLA', 'United States', False) print(_get_asset_data)

<function _get_asset_data at 0x7f323c912560>

However, I do not know how to make each set of data that I receive as a result of this function to be stored in a data frame for each company.I tried a for loop but got nowhere.

Any ideas? ¡Thank you in advance for your attention and cooperation!

Tomaxto
  • 3
  • 2
  • Does this answer your question? [Create multiple dataframes in loop](https://stackoverflow.com/questions/30635145/create-multiple-dataframes-in-loop) – nathan liang Apr 05 '22 at 17:50
  • How many price data will there be for each company several (closing, opening, maximum, minimum) or one value, for example, the closing price? – inquirer Apr 05 '22 at 17:58
  • Hello! Only the close price. – Tomaxto Apr 05 '22 at 18:36

1 Answers1

1

Here is one approach based on the code given. You should refrain from using it in practice, as it contains redundant code, which makes it hard to maintain. You'll find a more flexible approach below.

Based on your solution

import investpy
import pandas as pd

def _get_asset_data(ticker, country, state=False):
    return investpy.stocks.get_stock_recent_data(ticker, country, state)

df_1 = _get_asset_data('Eco','Colombia')
df_2 = _get_asset_data('JPM','United States')
df_3 = _get_asset_data('TSM','United States')
df_5 = _get_asset_data('CSCO','United States')
df_8 = _get_asset_data('NVDA','United States')
df_9 = _get_asset_data('BLK','United States')

final = pd.concat([df_1, df_2, df_3, df_5, df_8, df_9], axis=1)
final

More versatile solution:

import investpy
import pandas as pd


def _get_asset_data(ticker, country, state=False):
    return investpy.stocks.get_stock_recent_data(ticker, country, state)


stocks = [
    ('Eco', 'Colombia'),
    ('JPM', 'United States'),
    ('TSM', 'United States'),
    ('CSCO', 'United States'),
    ('NVDA', 'United States'),
    ('BLK', 'United States'),
    ]

results = []

for stock in stocks:
    result = _get_asset_data(*stock)
    results.append(result)

final = pd.concat(results, axis=1)
final
KarelZe
  • 1,466
  • 1
  • 11
  • 21
  • 1
    +1 for the second one. Maybe you could add a note that the first one is discouraged due to the high amount of redundancy or something along the lines? – fsimonjetz Apr 05 '22 at 18:13
  • @fsimonjetz Sure. Brilliant idea! – KarelZe Apr 05 '22 at 18:15
  • Wow. This is my firts time usign Stack Overflow and I am very grateful for your help. My sincere thanks. I hope you gave a good day :D! – Tomaxto Apr 05 '22 at 18:32
  • @Tomaxto Glad to hear and happy coding. Can you please [accept the answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), even if it is not mandatory? – KarelZe Apr 05 '22 at 18:42
  • @KarelZe Done! Again, my sincere thanks. – Tomaxto Apr 06 '22 at 20:20