How can I save the information from yfinance responses - python

import yfinance as yf
from datetime import datetime, timedelta
from pandas import DataFrame
startDate = datetime.now ()- timedelta(100)
endday = datetime.now()-timedelta(-1)
stockNo = xxx # xxx1, xxx2, ...stock code
start = startDate, end = endday
stock = yf.Ticker(stockNo)
stock_df = pd.DataFrame(stock.history(start = startDate, end = endday ))
yfinance responses as follow, how can i save this information as list or dataframe
xxx1: No data found, symbol may be delisted
xxx2: No data found, symbol may be delisted

This is a really late answer, but considering there are none yet:
import yfinance as yf
import pandas as pd
ticker = "ENTER_YOUR_TICKER_HERE"
yahootickerdata = yf.Ticker(f"{ticker}")
# this is already a dataframe so no need to read it in as one
# but this depends on what you're requesting from yfinance
earnings = yahootickerdata.earnings
# save to CSV
earnings.to_csv(f"data/{ticker}-yahoo-earnings.csv")
# and next time you can just load the CSV
df = pd.read_csv(f"data/{ticker}-yahoo-earnings.csv")

Related

take date from datetime, to string then integrate with API python

so I want to integrate my code with python API
# Install required library
!pip install xlrd
import pandas as pd
from datetime import time, timedelta, datetime
import openpyxl
import math
!pip install pytanggalmerah
from pytanggalmerah import TanggalMerah
# Mount google drive
from google.colab import drive
drive.mount('/content/drive')
# Read the Excel file
path = '/content/drive/MyDrive/Colab Notebooks/Book2.xls'
df = pd.read_excel(path)
# Convert the 'Tgl/Waktu' column to datetime format
df['Tgl/Waktu'] = pd.to_datetime(df['Tgl/Waktu'])
# Extract the date and time from the 'Tgl/Waktu' column
df['Date'] = df['Tgl/Waktu'].dt.date
a = df['Date'].drop_duplicates()
print(a)
with that code, it will have output as
0 2022-12-17
2 2022-12-19
4 2022-12-20
6 2022-12-21
8 2022-12-22
10 2022-12-23
Name: Date, dtype: object
and for the API i will use pytanggalmerah which will need the input to be
t.set_date("2019", "02", "05") #the order is Year, Month, Date
t.check()
how do i change my date object into string then make a for loop with my string to check whether is it true or false
how do i do it? how to integrate it?
You can use a list comp:
dates = [(str(x.year), str(x.month), str(x.day)) for x in df["Tgl/Waktu"].unique().tolist()]
for date in dates:
year, month, day = date
t.set_date(year, month, day)
is_holiday question:
import numpy as np
holidays = pd.DataFrame(holiday_data).rename(columns={"Date": "Day"})
cols = ["Year", "Month", "Day"]
holidays = holidays.assign(Date=pd.to_datetime(holidays[cols]).dt.date).drop(columns=cols)
df["is_holiday"] = np.where(df["Tgl/Waktu"].isin(holidays["Date"].to_list()), True, False)
print(df)

fetch data between two dates using pandas

I Am trying to extract all the previous days data from google spreadsheet, When i hardcode the dates the data comes in perfectly but when i try to make it more dynamic so that i can automate the process it does not work.
This is what i tried, if someone can help
import pandas as pd
import re
import datetime
from dateutil import parser
sheet_id = "19SzfcL3muVeISycG5eFYUqwrwwReGETZsNtl-euGU"
sheet_name = "October-2022"
url=f"https://docs.google.com/spreadsheets/d/{sheet_id}/gviz/tq?tqx=out:csv&sheet={sheet_name}"
ct = datetime.datetime.today()
pt= datetime.datetime.today() - datetime.timedelta(1)
#print(ct)
#print(pt)
df = pd.read_csv(url)
df['Timestamp1'] = pd.to_datetime(df['Timestamp'], format='%Y-%m-%d')
#filtered_df = df.loc[(df['Timestamp1'] > '2022-10-13') & (df['Timestamp1'] < '2022-10-14')]
filtered_df = df.loc[(df['Timestamp1'] > 'pt') & (df['Timestamp1'] < 'ct')]
filtered_df
When you filter the dataframe you are comparing the Timestamp with a string variable. The solution you provided should be correct if you remove the quotation marks:
filtered_df = df.loc[(df['Timestamp1'] > pt) & (df['Timestamp1'] < ct)]

AttributeError: 'str' object has no attribute 'option_chain'

I am attempting to download stock option data via the library yfinance for a list of stock tickers represented by the variable: "tickers" for options with an expiration date represented by: "exp_date".
Frankly, I don't even know if my loop/append is correct as I am new to coding, but I am getting the error message:
"AttributeError: 'str' object has no attribute 'option_chain'".
I only get the error message if I try to do this loop for all stock tickers. If I manually input a single ticker and manually input the expiration date instead of using the "exp_date" variable, it works perfectly.
import yfinance as yf
import pandas as pd
import datetime
# Get Friday of current week for expiration date
current_time = datetime.datetime.now()
exp_date = (current_time.date() - datetime.timedelta(days=current_time.weekday()) + datetime.timedelta(days=4))
# Get options data and put in dataframe
opt_df = pd.DataFrame()
for ticker in tickers:
opt = ticker.option_chain(exp_date)
opt_df.pd.Dataframe.append(opt)
ticker is the ticker symbol, not the ticker information returned by yfinance.
for symbol in tickers:
ticker = yf.Ticker(symbol)
opt = ticker.option_chain(exp_date)
opt_df.pd.Dataframe.append(opt)

How to get the closing price of apple stock for the last Friday for each month?

I want to get closing price of apple stock for the last Friday for each month. How can i do it ? Thx
import pandas as pd
import numpy as np
import yfinance as yf
apple=yf.Ticker("AAPL")
apple=apple.history(period="5y")
Here's a way of doing it:
import pandas as pd
import numpy as np
import yfinance as yf
apple=yf.Ticker("AAPL")
apple=apple.history(period="5y")
apple['weekday'] = apple.index.weekday
apple['month_year'] = apple.index.to_period('M')
apple['date'] = apple.index
friday_groupy = apple[apple['weekday'] == 4].groupby(['month_year'])
apple.loc[friday_groupy['date'].idxmax()]
An example solution:
import pandas as pd
import yfinance as yf
import calendar
# create dataframe
df = yf.Ticker("AAPL")
df = df.history(period="5y")
df.head()
# define helper function for retrieving last fridays for a given year
def last_fridays(year):
result = []
for month in range(1, 13):
last_friday = max(week[calendar.FRIDAY] for week in calendar.monthcalendar(year, month))
result.append('{:4d}-{:02d}-{:02d}'.format(year, month, last_friday))
return result
last_fridays = [frd for y in pd.DatetimeIndex(df.index).year for frd in last_fridays(y)]
# filter dataframe
df.loc[df.index.strftime('%Y-%m-%d').isin(last_fridays)]

Why is the ticker and date different

Here is my early attempts in using Python. I am getting stock data from Yahoo but I can see that the ticker, date column headers are lower than the high low open close.
I am definitely missing something. What is it?
import pandas as pd
import numpy as np
import datetime
import pandas_datareader as pdr
py.init_notebook_mode(connected=True)
# we download the stock prices for each ticker and then we do a mapping between data and name of the ticker
def get(tickers, startdate, enddate):
def data(ticker):
return (pdr.get_data_yahoo(ticker, start=startdate, end=enddate))
datas = map (data, tickers)
return(pd.concat(datas, keys=tickers, names=['ticker', 'date']))
# Define the stocks to download. We'll download of Apple, Microsoft and the S&P500 index.
tickers = ['AAPL','IBM']
# We would like all available data from 01/01/2000 until 31/12/2018.
start_date = datetime.datetime(2016, 1, 1)
end_date = datetime.datetime(2019, 12, 31)
all_data = get(tickers, start_date, end_date)
Screenshot
This dataframe uses a hierarchical index. ticker and date aren't columns, but are both part of the index. This means the rows are grouped firstly by ticker and then by date.
For more information on hierarchical indexes check out the Pandas docs

Categories

Resources