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

目录

写在前面:

图形结果显示:

数据设计:

代码:

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

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


写在前面:

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

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

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

3 聚焦某段图形

图形结果显示:

python_PyQt5日周月K线纵向对齐显示_1_数据处理_第1张图片

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

数据设计:

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

0 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 36 37 38 39

那周线级别对应的横轴为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表格输出,查看数据情况

python_PyQt5日周月K线纵向对齐显示_1_数据处理_第2张图片

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

你可能感兴趣的:(python杂项,python,开发语言)