Python获取A股的每年每月每周的初始交易日期

获取A股的每年每月每周的初始交易日期

直接上代码,可保存为本地数据库,方便随时调用


import numpy as np
import pandas as pd
from sqlalchemy import create_engine

# 创建数据库引擎
engine = create_engine('********************')

def get_trade_cal(): # 查询交易日历 trade_cal
    sql = "select cal_date,is_open from trade_cal"
    return pd.read_sql(sql, engine)

def get_trade_cal_ymd():
    # 先调用交易日历,然后计算 week/month/querter/year sart/end 属性
    cal_dates = get_trade_cal()
    cal_dates.rename(columns={'cal_date':'calendarDate'}, inplace = True)
    cal_dates.rename(columns={'is_open':'isOpen'}, inplace = True)
    cal_dates['isWeekStart'] = 0
    cal_dates['isWeekEnd'] = 0
    cal_dates['isMonthStart'] = 0
    cal_dates['isMonthEnd'] = 0
    cal_dates['isQuarterStart'] = 0
    cal_dates['isQuarterEnd'] = 0
    cal_dates['isYearStart'] = 0
    cal_dates['isYearEnd'] = 0
    previous_i = -1
    previous_open_week = -1
    previous_open_month = -1
    previous_open_year = -1
 
    for i in range(len(cal_dates.calendarDate)):
        str_date = cal_dates.loc[i]['calendarDate']
        isOpen = cal_dates.loc[i]['isOpen']
        if not isOpen:
            continue
        date = str_date
        #设置isWeekStart和isWeekEnd
        current_open_week = date.isocalendar()[1]
        if current_open_week != previous_open_week:
            cal_dates.loc[i, 'isWeekStart'] = 1
            if previous_open_week != -1:
                cal_dates.loc[previous_i, 'isWeekEnd'] = 1
            
        #设置isMonthStart和isMonthEnd
        current_open_month = date.month
        if current_open_month != previous_open_month:
            cal_dates.loc[i, 'isMonthStart'] = 1
            if previous_open_month != -1:
                cal_dates.loc[previous_i, 'isMonthEnd'] = 1
            #顺便根据月份设置isQuarterStart和isQuarterEnd
            if current_open_month in [1, 4, 7, 10]:
                cal_dates.loc[i, 'isQuarterStart'] = 1 
                if previous_open_month != -1:
                    cal_dates.loc[previous_i, 'isQuarterEnd'] = 1
            #有个特殊情况是交易所开始第一天应为QuarterStart
            if previous_open_month == -1:
                cal_dates.loc[i, 'isQuarterStart'] = 1
                
        #设置isYearStart和isYearEnd
        current_open_year = date.year
        if current_open_year != previous_open_year:
            cal_dates.loc[i, 'isYearStart'] = 1
            if previous_open_year != -1:
                cal_dates.loc[previous_i, 'isYearEnd'] = 1            
 
        previous_i = i
        previous_open_week = current_open_week
        previous_open_month = current_open_month
        previous_open_year = current_open_year
 
    return cal_dates   

# 调用每周的开始交易日期的列表
cal_dates = get_trade_cal_ymd()
print(cal_dates[cal_dates['isWeekStart']==1] )

get_trade_cal_ymw函数的调用和使用


# 获取本周/本月/本年的首个交易日期

caldate = get_trade_cal_ymw()

day_start = caldate[caldate['isOpen']==1]['calendarDate'].tolist()[-1].strftime('%Y-%m-%d')
week_start = caldate[caldate['isWeekStart']==1]['calendarDate'].tolist()[-1].strftime('%Y-%m-%d')
month_start = caldate[caldate['isMonthStart']==1]['calendarDate'].tolist()[-1].strftime('%Y-%m-%d')
quarter_start = caldate[caldate['isQuarterStart']==1]['calendarDate'].tolist()[-1].strftime('%Y-%m-%d')
year_start = caldate[caldate['isYearStart']==1]['calendarDate'].tolist()[-1].strftime('%Y-%m-%d')

你可能感兴趣的:(Python相关,python,开发语言)