-1
Hello to the python community.
I am a student of economics and beginner in python.
I’m developing a code to pursue return on stock purchase, buy and hold. However, I have a problem with the search in the yahoo Finance API, the idea of the code is to be able to input several shares purchased on different dates, but what happens is that when I extract the information the dataframe comes with only one initial date (the most recent date)not with the dates added in each input. For example: When I enter the values of the shares and the purchase date, for example ITUB4.SA 12-03-2019 and ITSA4.SA 23-12-2019, when running for in to download the closing data of the shares, he considers the date 23-12-2019 for the two actions and not separate dates for each action. My problem, I believe, is in fact. I apologize for the rudimentary way of writing the code. I accept tips that can improve code.
Follows the code:
import numpy as np
from pandas_datareader import data as wb
import matplotlib.pyplot as plt
import pandas as pd
from datetime import date
#Criar input de quantas loop for precisa rodar
n = int(input('Quantas ações você tem em sua carteira?'))
#cria o dicionario das acoes
acoes_dictionary = {}
#Roda o loop for dos input das acoes e salva no dicionario
for x in range(n):
new_key = input('Digite o código da sua ação')
new_date = input('Digite a data de compra da sua ação ')
acoes_dictionary[new_key] = new_date
#cria uma lista a partir das keys(código) das ações
cod_acoes = list(acoes_dictionary.keys())
#Cria uma lista a partir dos values(data de compra) das ações
date_acoes = list(acoes_dictionary.values())
#Cria um data framde para salvar a busca da internet das acoes
df = pd.DataFrame()
#Busca o fechamento as ações no yahoo finance
for i,j in zip(cod_acoes,date_acoes):
df[i,j] = wb.DataReader(i, data_source='yahoo', start =j)['Adj Close']
print(df)```
Hello John, missed to explain the problem better. The problem is in yahoo-Finance? What would you like to do? Join the data that returns from yahoo-Finance with the ones you enter manually? I ask you to explain better the problem that needs to be solved
– Pedro Costa
Thanks for the return, I think the problem is in : for i,j in zip(cod_acoes,date_acoes): df[i,j] = Wb.Datareader(i, data_source='yahoo', start=j). Possibly in this j, because what happens, if I type for example ITUB4.SA 12-03-2019 and ITSA4.SA 23.12.2019. When in search on the Reader date, it considers only the 23.12.2019 date for the two stocks and not the date of each one.
– João Augusto Temporin