复制链接
克隆策略
In [1]:
from bigdatasource.api import DataSource
from biglearning.api import M
from biglearning.api import tools as T
from biglearning.module2.common.data import Outputs
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore')

#并行任务
from joblib import Parallel, delayed
import copy
In [2]:
m1 = M.instruments.v2(
    start_date='2021-01-01',
    end_date='2023-03-31',
    market='CN_STOCK_A',
    instrument_list='',
    max_count=0
)

m2 = M.input_features.v1(
    features="""

收益=return_0

收盘=close_0

收盘=close_0

开盘=open_0


"""
)


m3 = M.general_feature_extractor.v7(
    instruments=m1.data,
    features=m2.data,
    start_date='',
    end_date='',
    before_start_days=500
)

m4 = M.derived_feature_extractor.v3(
    input_data=m3.data,
    features=m2.data,
    date_col='date',
    instrument_col='instrument',
    drop_na=False,
    remove_extra_columns=False,
    user_functions={}
)

m5 = M.chinaa_stock_filter.v1(
    input_data=m4.data,
    index_constituent_cond=['全部'],
    board_cond=['上证主板', '深证主板', '创业板'],
    industry_cond=['全部'],
    st_cond=['正常'],
    delist_cond=['非退市'],
    output_left_data=False
)
[2023-05-04 18:54:53.957416] INFO moduleinvoker: instruments.v2 开始运行..
[2023-05-04 18:54:54.064341] INFO moduleinvoker: instruments.v2 运行完成[0.106885s].
[2023-05-04 18:54:54.072433] INFO moduleinvoker: input_features.v1 开始运行..
[2023-05-04 18:54:54.125016] INFO moduleinvoker: input_features.v1 运行完成[0.05256s].
[2023-05-04 18:54:54.151292] INFO moduleinvoker: general_feature_extractor.v7 开始运行..
[2023-05-04 18:54:54.926094] INFO 基础特征抽取: 年份 2019, 特征行数=330913
[2023-05-04 18:54:56.030546] INFO 基础特征抽取: 年份 2020, 特征行数=944770
[2023-05-04 18:54:57.211724] INFO 基础特征抽取: 年份 2021, 特征行数=1061527
[2023-05-04 18:54:58.373809] INFO 基础特征抽取: 年份 2022, 特征行数=1171038
[2023-05-04 18:54:58.667257] INFO 基础特征抽取: 年份 2023, 特征行数=300168
[2023-05-04 18:54:58.774153] INFO 基础特征抽取: 总行数: 3808416
[2023-05-04 18:54:58.779334] INFO moduleinvoker: general_feature_extractor.v7 运行完成[4.628121s].
[2023-05-04 18:54:58.825715] INFO moduleinvoker: derived_feature_extractor.v3 开始运行..
[2023-05-04 18:55:03.603505] INFO derived_feature_extractor: 提取完成 收益=return_0, 0.005s
[2023-05-04 18:55:03.610025] INFO derived_feature_extractor: 提取完成 收盘=close_0, 0.004s
[2023-05-04 18:55:03.617222] INFO derived_feature_extractor: 提取完成 开盘=open_0, 0.005s
[2023-05-04 18:55:04.429425] INFO derived_feature_extractor: /y_2019, 330913
[2023-05-04 18:55:05.522256] INFO derived_feature_extractor: /y_2020, 944770
[2023-05-04 18:55:06.928348] INFO derived_feature_extractor: /y_2021, 1061527
[2023-05-04 18:55:08.469302] INFO derived_feature_extractor: /y_2022, 1171038
[2023-05-04 18:55:09.008501] INFO derived_feature_extractor: /y_2023, 300168
[2023-05-04 18:55:09.195286] INFO moduleinvoker: derived_feature_extractor.v3 运行完成[10.369567s].
[2023-05-04 18:55:09.211997] INFO moduleinvoker: chinaa_stock_filter.v1 开始运行..
[2023-05-04 18:55:10.822579] INFO A股股票过滤: 过滤 /y_2019, 315660/0/330913
[2023-05-04 18:55:14.347252] INFO A股股票过滤: 过滤 /y_2020, 869775/0/944770
[2023-05-04 18:55:18.273985] INFO A股股票过滤: 过滤 /y_2021, 940451/0/1061527
[2023-05-04 18:55:22.486372] INFO A股股票过滤: 过滤 /y_2022, 1003220/0/1171038
[2023-05-04 18:55:23.577540] INFO A股股票过滤: 过滤 /y_2023, 252673/0/300168
[2023-05-04 18:55:23.587562] INFO A股股票过滤: 过滤完成, 3381779 + 0
[2023-05-04 18:55:23.626341] INFO moduleinvoker: chinaa_stock_filter.v1 运行完成[14.41435s].
In [3]:
df  = m5.data.read()
In [4]:
df
Out[4]:
close_0 date instrument open_0 return_0 收益 收盘 开盘
0 1636.449707 2019-08-20 000001.SZA 1628.807861 1.004692 1.004692 1636.449707 1628.807861
1 1577.498169 2019-08-21 000001.SZA 1623.349365 0.963976 0.963976 1577.498169 1623.349365
2 1562.214478 2019-08-22 000001.SZA 1572.039795 0.990311 0.990311 1562.214478 1572.039795
3 1599.332153 2019-08-23 000001.SZA 1568.764648 1.023760 1.023760 1599.332153 1568.764648
4 1555.664307 2019-08-26 000001.SZA 1574.223145 0.972696 0.972696 1555.664307 1574.223145
... ... ... ... ... ... ... ... ...
3769217 10.576841 2023-03-27 605599.SHA 10.566523 1.000000 1.000000 10.576841 10.566523
3769218 10.483972 2023-03-28 605599.SHA 10.535566 0.991220 0.991220 10.483972 10.535566
3769219 10.473653 2023-03-29 605599.SHA 10.483972 0.999016 0.999016 10.473653 10.483972
3769220 10.514929 2023-03-30 605599.SHA 10.463334 1.003941 1.003941 10.514929 10.463334
3769221 10.597480 2023-03-31 605599.SHA 10.545885 1.007851 1.007851 10.597480 10.545885

3381779 rows × 8 columns

In [5]:
def calc_market_return(df):
    df['市场收益'] = df['收益'].mean()
    return df

df = df.groupby('date').apply(calc_market_return)
In [6]:
df['factor_1'] = np.abs(df['市场收益'] - df['收益'])  / (np.abs(df['市场收益']) + np.abs(df['收益']) + 0.1 )
In [7]:
def calc_adj_fac(df):
    df['决策分'] = df['收盘']/df['收盘'].shift()-1
    df['加权决策分'] = df['决策分'] * df['factor_1']
    df['惊恐收益'] = df['加权决策分'].rolling(20).mean()
    df['惊恐波动'] = df['加权决策分'].rolling(20).std()
    df['前瞻收益_1'] = df['收盘'].shift(-1) / df['收盘']
    return df

df = df.groupby('instrument').apply(calc_adj_fac)

魔改开始

魔改方式 用IC值对过往因子进行权重调整
x
corr = x.corr(return_-1)
In [8]:
df.dropna(inplace=True)
In [9]:
df
Out[9]:
close_0 date instrument open_0 return_0 收益 收盘 开盘 市场收益 factor_1 决策分 加权决策分 惊恐收益 惊恐波动 前瞻收益_1
20 1573.131470 2019-09-18 000001.SZA 1554.572632 1.011938 1.011938 1573.131470 1554.572632 1.001693 0.004847 0.011938 0.000058 -0.000047 0.000178 1.029840
21 1620.074341 2019-09-19 000001.SZA 1580.773315 1.029840 1.029840 1620.074341 1580.773315 1.010993 0.008804 0.029840 0.000263 -0.000002 0.000124 1.033693
22 1674.659058 2019-09-20 000001.SZA 1632.082886 1.033693 1.033693 1674.659058 1632.082886 1.000733 0.015442 0.033693 0.000520 0.000027 0.000169 1.002607
23 1679.025757 2019-09-23 000001.SZA 1674.659058 1.002607 1.002607 1679.025757 1674.659058 0.991636 0.005239 0.002607 0.000014 0.000013 0.000157 0.986996
24 1657.191895 2019-09-24 000001.SZA 1688.851074 0.986996 0.986996 1657.191895 1688.851074 1.002483 0.007412 -0.013004 -0.000096 0.000022 0.000146 1.037549
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
3769216 10.576841 2023-03-24 605599.SHA 10.762582 0.984630 0.984630 10.576841 10.762582 1.000949 0.007825 -0.015370 -0.000120 0.000025 0.000152 1.000000
3769217 10.576841 2023-03-27 605599.SHA 10.566523 1.000000 1.000000 10.576841 10.566523 0.999475 0.000250 0.000000 0.000000 0.000030 0.000150 0.991220
3769218 10.483972 2023-03-28 605599.SHA 10.535566 0.991220 0.991220 10.483972 10.535566 0.992684 0.000703 -0.008780 -0.000006 0.000029 0.000150 0.999016
3769219 10.473653 2023-03-29 605599.SHA 10.483972 0.999016 0.999016 10.473653 10.483972 0.995167 0.001838 -0.000984 -0.000002 0.000028 0.000150 1.003941
3769220 10.514929 2023-03-30 605599.SHA 10.463334 1.003941 1.003941 10.514929 10.463334 0.996530 0.003528 0.003941 0.000014 0.000029 0.000150 1.007851

3288505 rows × 15 columns

In [25]:
type(df['惊恐收益'].iloc[1])
Out[25]:
numpy.float64
In [26]:
type(df['前瞻收益_1'].iloc[1])
Out[26]:
numpy.float32
In [10]:
def calc_mul(df,instrument):
    
    df_ = df.copy()
    df_ = df_[df_['instrument'] == instrument]
    df_.reset_index(inplace=True)
    df_['惊恐收益_mul'] = df_['惊恐收益'].rolling(20).corr(df_['前瞻收益_1'])
    df_['惊恐波动_mul'] = df_['惊恐波动'].rolling(20).corr(df_['前瞻收益_1'])

    return df_
In [11]:
lst = df.instrument.unique().tolist()

results = Parallel(n_jobs=-1)(delayed(calc_mul)(df,ins) for ins in lst)
In [12]:
temp = pd.concat(results)
temp
Out[12]:
index close_0 date instrument open_0 return_0 收益 收盘 开盘 市场收益 factor_1 决策分 加权决策分 惊恐收益 惊恐波动 前瞻收益_1 惊恐收益_mul 惊恐波动_mul
0 20 1573.131470 2019-09-18 000001.SZA 1554.572632 1.011938 1.011938 1573.131470 1554.572632 1.001693 0.004847 0.011938 0.000058 -0.000047 0.000178 1.029840 NaN NaN
1 21 1620.074341 2019-09-19 000001.SZA 1580.773315 1.029840 1.029840 1620.074341 1580.773315 1.010993 0.008804 0.029840 0.000263 -0.000002 0.000124 1.033693 NaN NaN
2 22 1674.659058 2019-09-20 000001.SZA 1632.082886 1.033693 1.033693 1674.659058 1632.082886 1.000733 0.015442 0.033693 0.000520 0.000027 0.000169 1.002607 NaN NaN
3 23 1679.025757 2019-09-23 000001.SZA 1674.659058 1.002607 1.002607 1679.025757 1674.659058 0.991636 0.005239 0.002607 0.000014 0.000013 0.000157 0.986996 NaN NaN
4 24 1657.191895 2019-09-24 000001.SZA 1688.851074 0.986996 0.986996 1657.191895 1688.851074 1.002483 0.007412 -0.013004 -0.000096 0.000022 0.000146 1.037549 NaN NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
6 3742214 56.849998 2023-03-24 603307.SHA 57.599998 0.981865 0.981865 56.849998 57.599998 1.000949 0.009163 -0.018135 -0.000166 -0.000220 0.000319 0.986807 NaN NaN
7 3742215 56.099998 2023-03-27 603307.SHA 56.560001 0.986807 0.986807 56.099998 56.560001 0.999475 0.006072 -0.013193 -0.000080 -0.000220 0.000319 0.991087 NaN NaN
8 3742216 55.599998 2023-03-28 603307.SHA 56.700001 0.991087 0.991087 55.599998 56.700001 0.992684 0.000766 -0.008913 -0.000007 -0.000220 0.000319 0.988849 NaN NaN
9 3742217 54.980000 2023-03-29 603307.SHA 55.900002 0.988849 0.988849 54.980000 55.900002 0.995167 0.003031 -0.011151 -0.000034 -0.000181 0.000289 1.006548 NaN NaN
10 3742218 55.340000 2023-03-30 603307.SHA 54.820000 1.006548 1.006548 55.340000 54.820000 0.996530 0.004763 0.006548 0.000031 -0.000153 0.000280 1.001265 NaN NaN

3288505 rows × 18 columns

In [13]:
temp.dropna(inplace=True)
In [14]:
temp['惊恐收益_mul_adj'] = temp.groupby('instrument')['惊恐收益_mul'].shift()
temp['惊恐波动_mul_adj'] = temp.groupby('instrument')['惊恐波动_mul'].shift()
In [15]:
temp
Out[15]:
index close_0 date instrument open_0 return_0 收益 收盘 开盘 市场收益 factor_1 决策分 加权决策分 惊恐收益 惊恐波动 前瞻收益_1 惊恐收益_mul 惊恐波动_mul 惊恐收益_mul_adj 惊恐波动_mul_adj
19 39 1792.562012 2019-10-22 000001.SZA 1846.055054 0.972173 0.972173 1792.562012 1846.055054 1.011575 0.018909 -0.027827 -0.000526 0.000127 0.000340 1.001827 -0.438745 -0.359873 NaN NaN
20 40 1795.837036 2019-10-23 000001.SZA 1781.645020 1.001827 1.001827 1795.837036 1781.645020 0.994456 0.003516 0.001827 0.000006 0.000125 0.000340 1.025532 -0.321070 -0.237141 -0.438745 -0.359873
21 41 1841.688232 2019-10-24 000001.SZA 1801.295532 1.025532 1.025532 1841.688232 1801.295532 0.999637 0.012185 0.025532 0.000311 0.000127 0.000342 1.000593 -0.217108 -0.108253 -0.321070 -0.237141
22 42 1842.779907 2019-10-25 000001.SZA 1831.863037 1.000593 1.000593 1842.779907 1831.863037 1.006461 0.002785 0.000593 0.000002 0.000101 0.000330 0.986967 -0.243150 -0.166894 -0.217108 -0.108253
23 43 1818.762695 2019-10-28 000001.SZA 1853.696899 0.986967 0.986967 1818.762695 1853.696899 1.018085 0.014783 -0.013033 -0.000193 0.000091 0.000336 1.015006 -0.434080 -0.319567 -0.243150 -0.166894
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
23 3737373 24.980000 2023-03-30 603173.SHA 25.370001 0.985016 0.985016 24.980000 25.370001 0.996530 0.005532 -0.014984 -0.000083 -0.000022 0.000346 0.997198 0.102973 -0.414729 0.131198 -0.309003
19 3741398 49.650002 2023-03-27 603281.SHA 51.700001 0.963891 0.963891 49.650002 51.700001 0.999475 0.017246 -0.036109 -0.000623 -0.000175 0.000469 0.988117 0.087411 0.148067 NaN NaN
20 3741399 49.060001 2023-03-28 603281.SHA 49.590000 0.988117 0.988117 49.060001 49.590000 0.992684 0.002195 -0.011883 -0.000026 -0.000177 0.000468 0.990828 -0.168132 -0.004915 0.087411 0.148067
21 3741400 48.610001 2023-03-29 603281.SHA 49.349998 0.990828 0.990828 48.610001 49.349998 0.995167 0.002080 -0.009172 -0.000019 -0.000181 0.000466 1.036206 -0.057833 0.113704 -0.168132 -0.004915
22 3741401 50.369999 2023-03-30 603281.SHA 48.650002 1.036206 1.036206 50.369999 48.650002 0.996530 0.018604 0.036206 0.000674 -0.000119 0.000495 0.983919 -0.060049 0.095219 -0.057833 0.113704

3204465 rows × 20 columns

In [16]:
temp_ = temp.copy()
In [17]:
temp_.dropna(inplace=True)
temp_['惊恐收益'] = temp_['惊恐收益'] * temp_['惊恐收益_mul_adj']
temp_['惊恐波动'] = temp_['惊恐波动'] * temp_['惊恐波动_mul_adj']
In [18]:
temp_
Out[18]:
index close_0 date instrument open_0 return_0 收益 收盘 开盘 市场收益 factor_1 决策分 加权决策分 惊恐收益 惊恐波动 前瞻收益_1 惊恐收益_mul 惊恐波动_mul 惊恐收益_mul_adj 惊恐波动_mul_adj
20 40 1795.837036 2019-10-23 000001.SZA 1781.645020 1.001827 1.001827 1795.837036 1781.645020 0.994456 0.003516 0.001827 0.000006 -0.000055 -0.000123 1.025532 -0.321070 -0.237141 -0.438745 -0.359873
21 41 1841.688232 2019-10-24 000001.SZA 1801.295532 1.025532 1.025532 1841.688232 1801.295532 0.999637 0.012185 0.025532 0.000311 -0.000041 -0.000081 1.000593 -0.217108 -0.108253 -0.321070 -0.237141
22 42 1842.779907 2019-10-25 000001.SZA 1831.863037 1.000593 1.000593 1842.779907 1831.863037 1.006461 0.002785 0.000593 0.000002 -0.000022 -0.000036 0.986967 -0.243150 -0.166894 -0.217108 -0.108253
23 43 1818.762695 2019-10-28 000001.SZA 1853.696899 0.986967 0.986967 1818.762695 1853.696899 1.018085 0.014783 -0.013033 -0.000193 -0.000022 -0.000056 1.015006 -0.434080 -0.319567 -0.243150 -0.166894
24 44 1846.055054 2019-10-29 000001.SZA 1822.037720 1.015006 1.015006 1846.055054 1822.037720 0.985499 0.014047 0.015006 0.000211 -0.000046 -0.000107 0.971614 -0.248660 -0.118867 -0.434080 -0.319567
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
22 3737372 25.360001 2023-03-29 603173.SHA 25.780001 0.984090 0.984090 25.360001 25.780001 0.995167 0.005327 -0.015910 -0.000085 -0.000001 -0.000013 0.985016 0.131198 -0.309003 0.072428 -0.038865
23 3737373 24.980000 2023-03-30 603173.SHA 25.370001 0.985016 0.985016 24.980000 25.370001 0.996530 0.005532 -0.014984 -0.000083 -0.000003 -0.000107 0.997198 0.102973 -0.414729 0.131198 -0.309003
20 3741399 49.060001 2023-03-28 603281.SHA 49.590000 0.988117 0.988117 49.060001 49.590000 0.992684 0.002195 -0.011883 -0.000026 -0.000015 0.000069 0.990828 -0.168132 -0.004915 0.087411 0.148067
21 3741400 48.610001 2023-03-29 603281.SHA 49.349998 0.990828 0.990828 48.610001 49.349998 0.995167 0.002080 -0.009172 -0.000019 0.000030 -0.000002 1.036206 -0.057833 0.113704 -0.168132 -0.004915
22 3741401 50.369999 2023-03-30 603281.SHA 48.650002 1.036206 1.036206 50.369999 48.650002 0.996530 0.018604 0.036206 0.000674 0.000007 0.000056 0.983919 -0.060049 0.095219 -0.057833 0.113704

3200049 rows × 20 columns

In [19]:
df = temp_
In [20]:
df['groups_1'] = pd.qcut(df['惊恐收益'],q=7,labels=False)
df['groups_2'] = pd.qcut(df['惊恐波动'],q=7,labels=False)
In [21]:
df=df[['date','instrument','前瞻收益_1','惊恐波动','惊恐收益','groups_1','groups_2']]
In [23]:
def calc_return_1(df):
    mean = df['前瞻收益_1'].mean()
    df['return_2']  = mean
    return df

def calc_return_2(df):
    mean = df['前瞻收益_1'].mean()
    df['return_2']  = mean
    return df


plot_pnl = df.groupby(['date','groups_1']).apply(calc_return_1)
plot_pnl = plot_pnl.groupby(['date','groups_2']).apply(calc_return_2)
In [12]:
test_1 = plot_pnl.drop_duplicates(subset=['date','groups_1'])


def cal_cum_prod(df):
    df['equity_1'] = df['return_1'].cumprod()
    return df 



test_1 = test_1.groupby('groups_1').apply(cal_cum_prod)
In [13]:
import plotly.graph_objects as go


# 创建一个折线图对象
fig = go.Figure()


# 添加折线到图表
fig.add_trace(go.Scatter(x=test_1.date.unique(), y=test_1[test_1['groups_1']==0]['equity_1'], mode='lines', name='group_0'))
fig.add_trace(go.Scatter(x=test_1.date.unique(), y=test_1[test_1['groups_1']==1]['equity_1'], mode='lines', name='group_1'))
fig.add_trace(go.Scatter(x=test_1.date.unique(), y=test_1[test_1['groups_1']==2]['equity_1'], mode='lines', name='group_2'))
fig.add_trace(go.Scatter(x=test_1.date.unique(), y=test_1[test_1['groups_1']==3]['equity_1'], mode='lines', name='group_3'))
fig.add_trace(go.Scatter(x=test_1.date.unique(), y=test_1[test_1['groups_1']==4]['equity_1'], mode='lines', name='group_4'))
fig.add_trace(go.Scatter(x=test_1.date.unique(), y=test_1[test_1['groups_1']==5]['equity_1'], mode='lines', name='group_5'))
fig.add_trace(go.Scatter(x=test_1.date.unique(), y=test_1[test_1['groups_1']==6]['equity_1'], mode='lines', name='group_6'))


# 设置图表标题和坐标轴标签
fig.update_layout(title='惊恐收益因子分层净值', xaxis_title='日期', yaxis_title='PNL')


# 显示图表
fig.show()
In [14]:
test_2 = plot_pnl.drop_duplicates(subset=['date','groups_2'])

def cal_cum_prod(df):
    df['equity_2'] = df['return_2'].cumprod()
    return df 

test_2 = test_2.groupby('groups_2').apply(cal_cum_prod)
In [20]:
test_2[test_2['date']=='2023-03-07']
Out[20]:
close_0 date instrument open_0 return_0 收益 收盘 开盘 市场收益 factor_1 决策分 加权决策分 惊恐收益 惊恐波动 groups_1 groups_2 f_return_0 return_1 return_2 equity_2
3508288 1559.786743 2023-03-07 000001.SZA 1578.016479 0.988448 0.988448 1559.786743 1578.016479 0.979665 0.004247 -0.011552 -0.000049 0.000009 0.000267 3.0 2.0 0.988313 1.007139 1.008557 1.879702
3508347 2787.651611 2023-03-07 000002.SZA 2842.955322 0.980547 0.980547 2787.651611 2842.955322 0.979665 0.000428 -0.019453 -0.000008 -0.000042 0.000112 1.0 1.0 0.997520 1.009099 1.006917 1.871952
3508524 214.556900 2023-03-07 000006.SZA 220.397842 0.975221 0.975221 214.556900 220.397842 0.979665 0.002163 -0.024779 -0.000054 0.000164 0.000783 5.0 4.0 1.009074 1.008640 1.010597 1.250360
3508583 63.037647 2023-03-07 000007.SZA 63.783165 0.987030 0.987030 63.037647 63.783165 0.979665 0.003563 -0.012970 -0.000046 -0.000005 0.000080 2.0 0.0 1.009198 1.008448 1.006012 1.942577
3509055 12.528378 2023-03-07 000017.SZA 13.038097 0.962887 0.962887 12.528378 13.038097 0.979665 0.008215 -0.037113 -0.000305 0.000184 0.000949 5.0 5.0 1.010707 1.008640 1.010632 1.403674
3509822 81.712112 2023-03-07 000034.SZA 85.298714 0.948932 0.948932 81.712112 85.298714 0.979665 0.015150 -0.051068 -0.000774 0.000273 0.001703 6.0 6.0 1.014885 1.011141 1.014402 0.736563
3510176 18.091261 2023-03-07 000040.SZA 18.656614 0.969697 0.969697 18.091261 18.656614 0.979665 0.004864 -0.030303 -0.000147 0.000005 0.000448 3.0 3.0 1.009615 1.007139 1.010208 1.749559
In [16]:
import plotly.graph_objects as go

# 创建一个折线图对象
fig = go.Figure()

# 添加折线到图表
fig.add_trace(go.Scatter(x=test_1.date.unique(), y=test_2[test_2['groups_2']==0]['equity_2'], mode='lines', name='group_0'))
fig.add_trace(go.Scatter(x=test_1.date.unique(), y=test_2[test_2['groups_2']==1]['equity_2'], mode='lines', name='group_1'))
fig.add_trace(go.Scatter(x=test_1.date.unique(), y=test_2[test_2['groups_2']==2]['equity_2'], mode='lines', name='group_2'))
fig.add_trace(go.Scatter(x=test_1.date.unique(), y=test_2[test_2['groups_2']==3]['equity_2'], mode='lines', name='group_3'))
fig.add_trace(go.Scatter(x=test_1.date.unique(), y=test_2[test_2['groups_2']==4]['equity_2'], mode='lines', name='group_4'))
fig.add_trace(go.Scatter(x=test_1.date.unique(), y=test_2[test_2['groups_2']==5]['equity_2'], mode='lines', name='group_5'))
fig.add_trace(go.Scatter(x=test_1.date.unique(), y=test_2[test_2['groups_2']==6]['equity_2'], mode='lines', name='group_6'))


# 设置图表标题和坐标轴标签
fig.update_layout(title='惊恐波动因子分层净值', xaxis_title='日期', yaxis_title='PNL')

# 显示图表
fig.show()
In [17]:
re = test_1[test_1['date']=='2023-02-22']
In [18]:
re
Out[18]:
close_0 date instrument open_0 return_0 收益 收盘 开盘 市场收益 factor_1 决策分 加权决策分 惊恐收益 惊恐波动 groups_1 groups_2 f_return_0 return_1 return_2 equity_1
3508279 1597.385620 2023-02-22 000001.SZA 1595.106934 0.994326 0.994326 1597.385620 1595.106934 1.001255 0.003306 -0.005674 -0.000019 -0.000014 0.000257 2.0 2.0 1.002140 1.000573 0.997816 1.642057
3508338 3012.322754 2023-02-22 000002.SZA 3008.866211 0.988656 0.988656 3012.322754 3008.866211 1.001255 0.006029 -0.011344 -0.000068 -0.000069 0.000198 1.0 2.0 1.002295 1.001292 0.997816 1.478266
3508515 236.363052 2023-02-22 000006.SZA 227.017563 1.042955 1.042955 236.363052 227.017563 1.001255 0.019448 0.042955 0.000835 0.000216 0.000753 6.0 4.0 0.978583 0.989534 0.996623 1.133134
3508692 112.870277 2023-02-22 000009.SZA 113.672035 0.987529 0.987529 112.870277 113.672035 1.001255 0.006571 -0.012471 -0.000082 0.000007 0.000078 3.0 0.0 0.997632 0.998668 1.000111 2.106093
3508810 47.519764 2023-02-22 000011.SZA 47.111519 0.997429 0.997429 47.519764 47.111519 1.001255 0.001823 -0.002571 -0.000005 0.000037 0.000181 4.0 1.0 1.000859 0.997270 0.998860 1.822032
3508928 67.316887 2023-02-22 000014.SZA 67.120430 0.995160 0.995160 67.316887 67.120430 1.001255 0.002907 -0.004840 -0.000014 0.000090 0.000386 5.0 3.0 1.000973 0.996078 0.997700 1.316344
3512349 257.451721 2023-02-22 000333.SZA 258.524841 0.985805 0.985805 257.451721 258.524841 1.001255 0.007403 -0.014195 -0.000105 -0.000076 0.000225 0.0 2.0 1.011178 1.005967 0.997816 0.969056