Home > Net >  Attempting to extract stock prices using python
Attempting to extract stock prices using python

Time:07-04

I am currently trying to extract stock prices from a list of stock codes contained on a csv file by using pandas and yfinance.

I have 145 companies I need to do this for, is there a way of doing it? As I have tried over a period of 5 days without success.

I just need to know if its possible and what would you recommend to achieve this.

CodePudding user response:

yfinance.Ticker(ticker).history(start=start_date) gets you the data that you desire.

if you have a giant csv, with a field "ticker", you can create a pandas dataframe with the below:

import pandas as pd
import yfinance
def read_create_giant_df(file_in):
    df = pd.read_csv(file_in)
    out = []
    for item in df["ticker"]:
       ticker_df = yfinance.Ticker(item).history(start="1930-01-01")
       ticker_df["ticker"] = item
       out.append(ticker_df)
   return pd.concat(out)

CodePudding user response:

below code should work , if any module missing use command to install it .

pip install yfinance 
pip install yahoofinancials

Run below code to get the data for Amazon-AMZN

import pandas as pd
import yfinance as yf
from yahoofinancials import YahooFinancials
tsla_df = yf.download(tickers=['AMZN','TSLA'], 
                  start='2019-01-01', 
                  end='2022-07-01', 
                  progress=False,auto_adjust=True)
tsla_df.head()
  • Related