python_PyQt5日周月K线纵向对齐显示_1_数据处理

目录

写在前面:

图形结果显示:

数据设计:

代码:

从日数据中计算周数据、月数据

生成图形显示需要的数据格式


写在前面:

“PyQt5日周月K线纵向对齐显示”,将分三篇博文描述

1 数据处理。将数据处理成适合图形显示的格式

2 显示工具开发。用pyqtgraph开发

3 聚焦某段图形

图形结果显示:

显示的结果是,周线级别K线与本周日数据的最后一个交易日对齐,月线级别K线与本月日数据的最后一个交易日对齐。

数据设计:

假设有40个日数据,日线级别的横轴为0,1,2,3,4,...39

012345678910111213141516171819
周三
2021222324252627282930313233343536373839

那周线级别对应的横轴为2,7,12,17,22,27,32,37

月线级别与周线一样的提取方式,这里就不再赘述

代码:

(注意:这里的原始数据来自优矿,所以对于收开高低,交易日的字段对应优矿)

从日数据中计算周数据、月数据

def caculate_week_month_from_day(df):        
        df['row_i'] = [i for i in range(len(df))]
        df['o_date'] = pd.to_datetime(df['tradeDate'])
        df['ma'] = talib.MA(df['closePrice'],timeperiod=20)
        df['vol_ma'] = talib.MA(df['turnoverVol'],timeperiod=20)
        df['value_ma'] = talib.MA(df['turnoverValue'],timeperiod=20)
        week_group = df.resample('W-FRI',on='o_date')
        month_group = df.resample('M',on='o_date')

        week_df = week_group.last()
        week_df['row_i'] = week_group.last()['row_i']
        week_df['openPrice'] = week_group.first()['openPrice']
        week_df['lowestPrice'] = week_group.min()['lowestPrice']
        week_df['highestPrice'] = week_group.max()['highestPrice']
        week_df['turnoverVol'] = week_group.sum()['turnoverVol']
        week_df['turnoverValue'] = week_group.sum()['turnoverValue']
        week_df = week_df.loc[:,self.multi_columns_list].copy()
        week_df.dropna(axis=0,how='any',subset=['closePrice'],inplace=True)
        week_df['ma'] = talib.MA(week_df['closePrice'],timeperiod=20)
        week_df['vol_ma'] = talib.MA(week_df['turnoverVol'],timeperiod=20)
        week_df['value_ma'] = talib.MA(week_df['turnoverValue'],timeperiod=20)

        month_df = month_group.last()
        month_df['row_i'] = month_group.last()['row_i']
        month_df['openPrice'] = month_group.first()['openPrice']
        month_df['lowestPrice'] = month_group.min()['lowestPrice']
        month_df['highestPrice'] = month_group.max()['highestPrice']
        month_df['turnoverVol'] = month_group.sum()['turnoverVol']
        month_df['turnoverValue'] = month_group.sum()['turnoverValue']
        month_df = month_df.loc[:,self.multi_columns_list].copy()
        month_df.dropna(axis=0,how='any',subset=['closePrice'],inplace=True)
        month_df['ma'] = talib.MA(month_df['closePrice'],timeperiod=20)
        month_df['vol_ma'] = talib.MA(month_df['turnoverVol'],timeperiod=20)
        month_df['value_ma'] = talib.MA(month_df['turnoverValue'],timeperiod=20)

return daily_df,week_df,month_df

为了便于说明,这里将日周月数据按Excel表格输出,查看数据情况

day,week,month的row_i分别是日、周、月的横轴位置

生成图形显示需要的数据格式

(要显示K线图和成交量图,所以会分别生成K线数据和成交量数据)

def caculate_show_data(df):
        k_height_num = 400
        vol_height_num = 100
        candle_data = df.loc[:,['row_i','openPrice','closePrice','lowestPrice','highestPrice']].values.tolist()
        curve_data = {
            'x':df['row_i'].values.tolist(),
            'y':df['ma'].values.tolist()
        }
        one = {
            'height_num':k_height_num,
            'yMin':df['lowestPrice'].min(),
            'yMax':df['highestPrice'].max(),
            'data_list':[
                {
                    'type':'candle',
                    'data':candle_data
                },
                {
                    'type':'curve',
                    'data':curve_data
                }
            ]
        }
        bar_data = df.loc[:,['row_i','openPrice','closePrice','turnoverVol']].values.tolist()
        curve_data2 = {
            'x':df['row_i'].values.tolist(),
            'y':df['vol_ma'].values.tolist()
        }
        two = {
            'height_num':vol_height_num,
            'yMin':0,
            'yMax':df['turnoverVol'].max(),
            'data_list':[
                {
                    'type': 'bar',
                    'data':bar_data
                },
                {
                    'type':'curve',
                    'data':curve_data2
                }
            ]
        }
        return one,two
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值