Python Environment Configuration#
For detailed installation steps, please refer to Python Installation and Environment Configuration Detailed Tutorial
iFind Python Environment Configuration#
- Click to Download Tonghuashun Data Interface
- For first-time download users, please open the SuperCommand file in the folder and log in to your account
- After logging in, the system will prompt you to repair the relevant programming language environment. Please follow the instructions to repair the related environment
- Alternatively, after logging in, click on the environment settings option in the tools menu, select Python, and then click the OK button
- Then select the installed Python path and click Continue
Install Pycharm#
For detailed installation steps, please refer to Pycharm Installation Tutorial
Running Python Programs#
"""
Author: binxin
Date: 2023/11/18 14:44
"""
import os
import numpy as np
import pandas as pd
from iFinDPy import *
import datetime
# Login function
def login(username, password):
thsLogin = THS_iFinDLogin(username, password)
if thsLogin != 0:
print('Login failed')
else:
print('Login successful')
# Get data
def get_data(edate):
get_str = 'edate=' + edate + ';zqlx=全部'
data_p00868 = THS_DR('p00868', get_str, 'p00868_f027:Y,p00868_f022:Y', 'format:list')
return data_p00868
# Save data to Excel
def save_to_excel(file_name, str_date, premium):
if not os.path.exists(file_name):
data = {"Date": [str_date], "Convertible Premium%": [premium]}
df = pd.DataFrame(data)
else:
df = pd.read_excel(file_name)
new_data = pd.DataFrame({"Date": [str_date], "Convertible Premium%": [premium]})
df = pd.concat([df, new_data], ignore_index=True)
df.to_excel(file_name, index=False)
# Calculate median
def calculate_median(data):
# Convert value range
max_value = 101
min_value = 99
float_values = []
data_f022 = data['p00868_f022']
data_f027 = data['p00868_f027']
for f027, f022 in zip(data_f027, data_f022):
if '--' in f027 or '--' in f022:
continue
f027_value = float(f027)
f022_value = float(f022)
if min_value <= f027_value <= max_value:
float_values.append(f022_value)
return np.median(float_values) if float_values else None
# Get data - single day
def get_today_data():
today = datetime.date.today()
edate = today.strftime("%Y%m%d")
return get_data(edate)
# Get data - interval
def get_interval_data(start_date, end_date):
delta = datetime.timedelta(days=1)
data_list = []
while start_date <= end_date:
edate = start_date.strftime("%Y%m%d")
data = get_data(edate)
if data.data is not None:
data_list.append((start_date.strftime("%Y/%m/%d"), data))
start_date += delta
return data_list
# Main function
def main():
username = "username"
password = "password"
login(username, password)
# Get data for today
# today_data = get_today_data()
# if today_data.data is not None:
# today_median = calculate_median(today_data.data[0]['table'])
# if today_median is not None:
# save_to_excel("Convertible Premium Rate Records (Conversion Value).xlsx", datetime.date.today().strftime("%Y/%m/%d"), today_median)
# Get data for interval
start_date = datetime.date(2023, 11, 1)
end_date = datetime.date(2023, 11, 18)
interval_data = get_interval_data(start_date, end_date)
for date, data in interval_data:
median_value = calculate_median(data.data[0]['table'])
if median_value is not None:
save_to_excel("Convertible Premium Rate Records (Conversion Value).xlsx", date, median_value)
if __name__ == '__main__':
main()