Created
November 26, 2023 14:41
-
-
Save ycytai/29569b82ff14df3946d661842accb3c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from io import StringIO | |
import requests | |
import time | |
import random | |
def get_put_call_ratio_df(q_start_date: str, q_end_date:str): | |
url = 'https://www.taifex.com.tw/cht/3/pcRatioDown' | |
payload = { | |
'queryStartDate': q_start_date, | |
'queryEndDate': q_end_date, | |
} | |
res = requests.post(url, data=payload) | |
data_file = StringIO(res.text) | |
df = pd.read_csv(data_file, index_col=False) | |
return df | |
start_date = '2003-11-20' | |
end_date = '2023-11-15' | |
date_range = pd.date_range(start=start_date, end=end_date, freq='30D') | |
final_pc_ratio_df = pd.DataFrame() | |
for i in range(1, len(date_range)): | |
q_start_date = date_range[i-1].strftime('%Y/%m/%d') | |
q_end_date = date_range[i].strftime('%Y/%m/%d') | |
pc_ratio_df = get_put_call_ratio_df(q_start_date, q_end_date) | |
final_pc_ratio_df = pd.concat([final_pc_ratio_df, pc_ratio_df], axis=0) | |
time.sleep(random.uniform(1, 2)) | |
final_pc_ratio_df.to_csv('put_call_ratio.csv') | |
print(f'{q_end_date} done') | |
final_pc_ratio_df = final_pc_ratio_df.drop_duplicates() | |
final_pc_ratio_df.columns = ['Date', 'Put Volume', 'Call Volume', 'Put/Call Volume Ratio', 'Put OI', 'Call OI', 'Put/Call OI Ratio'] | |
final_pc_ratio_df.index = pd.to_datetime(final_pc_ratio_df.index) | |
final_pc_ratio_df.sort_index().to_csv('put_call_ratio.csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment