-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions_work_progress.py
More file actions
35 lines (25 loc) · 1.28 KB
/
options_work_progress.py
File metadata and controls
35 lines (25 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#from yahoo_fin import options
import pandas as pd
# Define the ticker symbol for which you want to pull the options data
ticker_symbol = "AAPL"
# Get the list of expiration dates for the ticker
expiration_dates = options.get_expiration_dates(ticker_symbol)
# Initialize a DataFrame to hold all options data
all_options_data = pd.DataFrame()
# Loop through each expiration date and get the options data for that date
for date in expiration_dates:
options_data = options.get_options_chain(ticker_symbol, date)
# Convert the options data for each date into a pandas DataFrame
calls_data = pd.DataFrame(options_data['calls'])
puts_data = pd.DataFrame(options_data['puts'])
# Add the expiration date and type of the option as columns to the DataFrames
calls_data['Expiration Date'] = date
calls_data['Option Type'] = 'Call'
puts_data['Expiration Date'] = date
puts_data['Option Type'] = 'Put'
# Concatenate the calls and puts data
combined_data = pd.concat([calls_data, puts_data])
# Append the combined data to the all_options_data DataFrame
all_options_data = pd.concat([all_options_data, combined_data], ignore_index=True)
# Save the options data to a CSV file
all_options_data.to_csv(f'{ticker_symbol}_options_data.csv', index=False)