Exemplo n.º 1
0
def plot_gain_prod(df_gain, time_col='日期', N=None, show_gain=True, **kwargs):
    '''
    绘制盈亏净值曲线图
    df_gain须包含列[time_col, '盈亏%']
    **kwargs为plot_Series可接受参数
    '''

    N = df_gain.shape[0] if N is None or N < 1 or N > df_gain.shape[0] else N
    df = df_gain.reindex(columns=[time_col, '盈亏%'])
    if N >= df.shape[0]:
        df.sort_values(time_col, ascending=True, inplace=True)
        tmp = pd.DataFrame(columns=[time_col, '盈亏%'])
        tmp.loc['tmp'] = ['start', 0]
        df = pd.concat((tmp, df), axis=0)
    else:
        df = df.sort_values(time_col, ascending=True).iloc[-N - 1:, :]

    df.set_index(time_col, inplace=True)
    df.loc[df.index[0], '盈亏%'] = 0
    df['净值'] = (1 + df['盈亏%'] / 100).cumprod()
    gain_pct = round(100 * (df['净值'].iloc[-1] - 1), 2)

    if not 'title' in kwargs:
        if N == df_gain.shape[0]:
            kwargs['title'] = f'期权账户净值曲线\n(收益率: {gain_pct}%)' \
                              if show_gain else '期权账户净值曲线'
        else:
            kwargs['title'] = \
                    f'期权账户近{N}个交易日净值曲线\n(收益率: {gain_pct}%)' \
                    if show_gain else f'期权账户近{N}个交易日净值曲线'

    plot_Series(df, {'净值': '-ro'}, **kwargs)
Exemplo n.º 2
0
def plot_gain_act(df_gain, time_col='日期', N=None, **kwargs):
    '''
    绘制实际盈亏曲线图
    df_gain须包含列[time_col, '实际总盈亏%']
    **kwargs为plot_Series可接受参数
    '''

    N = df_gain.shape[0] if N is None or N < 1 or N > df_gain.shape[0] else N
    df = df_gain.reindex(columns=[time_col, '实际总盈亏%'])
    if N == df_gain.shape[0]:
        df.sort_values(time_col, ascending=True, inplace=True)
        tmp = pd.DataFrame(columns=['日期', '实际总盈亏%'])
        tmp.loc['tmp'] = ['start', 0]
        df = pd.concat((tmp, df), axis=0)
    else:
        df = df.sort_values(time_col, ascending=True).iloc[-N - 1:, :]

    df.set_index(time_col, inplace=True)

    if not 'title' in kwargs:
        if N == df_gain.shape[0]:
            kwargs['title'] = '期权账户实际总盈亏(%)走势'
        else:
            kwargs['title'] = '期权账户近{}个交易日实际总盈亏(%)走势'.format(N)

    plot_Series(df, {'实际总盈亏%': '-ro'}, **kwargs)
Exemplo n.º 3
0
    objf = TestFuncs.F5
    parms_func = {
        'func_name': objf.__name__,
        'x_lb': -10,
        'x_ub': 10,
        'dim': 5,
        'kwargs': {}
    }
    parms_opter = {'opter_name': 'GWO-test', 'PopSize': 20, 'Niter': 1000}
    # logger = simple_logger()
    logger = get_logger('./test/GWO_test.txt', screen_show=True)
    # parms_log = {'logger': logger, 'nshow': 10}
    parms_log = {'logger': logger, 'nshow': 100}

    func_opter_parms = FuncOpterInfo(parms_func, parms_opter, parms_log)
    func_opter_parms = GWO(objf, func_opter_parms)

    vals = pd.DataFrame({
        'fval_best': func_opter_parms.convergence_curve,
        'fval_mean': func_opter_parms.convergence_curve_mean
    })
    plot_Series(vals, {'fval_best': '-r', 'fval_mean': '-b'}, figsize=(10, 6))

    # best_x = func_opter_parms.best_x
    # func_opter_parms.parms_log['logger'].info(f'best x: {best_x}')

    close_log_file(logger)

    print(f'used time: {round(time.time()-strt_tm, 6)}.')
Exemplo n.º 4
0
    
if __name__ == '__main__':
    strt_tm = time.time()
    
    objf = TestFuncs.F1
    parms_func = {'func_name': objf.__name__,
                  'x_lb': -10, 'x_ub': 10, 'dim': 5, 'kwargs': {}}
    parms_opter = {'opter_name': 'GA-test',
                   'PopSize': 20, 'Niter': 1000,
                   'Pcrs': 0.7, 'Pmut': 0.1, 'Ntop': 2}
    # logger = simple_logger()
    logger = get_logger('./test/GA_test.txt', screen_show=True)
    # parms_log = {'logger': logger, 'nshow': 10}
    parms_log = {'logger': logger, 'nshow': 100}
    
    func_opter_parms = FuncOpterInfo(parms_func, parms_opter, parms_log)
    func_opter_parms = GA(objf, func_opter_parms)
    
    vals = pd.DataFrame({'fval_best': func_opter_parms.convergence_curve,
                         'fval_mean': func_opter_parms.convergence_curve_mean})
    plot_Series(vals, {'fval_best': '-r', 'fval_mean': '-b'}, figsize=(10, 6),
                title='GA优化目标函数值收敛过程')
    
    # best_x = func_opter_parms.best_x
    # func_opter_parms.parms_log['logger'].info(f'best x: {best_x}')
    
    close_log_file(logger)
    
    
    print(f'used time: {round(time.time()-strt_tm, 6)}.')
    
Exemplo n.º 5
0
    from utils_hoo.utils_plot.plot_Common import plot_Series

    # 50ETF日线行情------------------------------------------------------------
    fpath = './test/510050_daily_pre_fq.csv'
    data = load_csv(fpath)
    data.set_index('date', drop=False, inplace=True)

    data['cci'] = CCI(data)
    data['cci_100'] = data['cci'].apply(lambda x: 1 if x > 100 else \
                                                    (-1 if x < -100 else 0))

    plot_Series(
        data.iloc[-200:, :], {'close': ('.-k', False)},
        cols_styl_low_left={'cci': ('.-c', False)},
        cols_to_label_info={
            'cci': [['cci_100', (-1, 1), ('r^', 'bv'), False]]
        },
        xparls_info={'cci': [(100, 'r', '-', 1.3), (-100, 'r', '-', 1.3)]},
        figsize=(8, 7),
        grids=True)

    start_ends_1 = get_con_start_end(data['cci_100'], lambda x: x == -1)
    start_ends1 = get_con_start_end(data['cci_100'], lambda x: x == 1)
    data['cci_100_'] = 0
    for start, end in start_ends_1:
        if end + 1 < data.shape[0]:
            data.loc[data.index[end + 1], 'cci_100_'] = -1
    for start, end in start_ends1:
        if end + 1 < data.shape[0]:
            data.loc[data.index[end + 1], 'cci_100_'] = 1
Exemplo n.º 6
0
    
    #%%
    # 参数汇总
    Results = pd.DataFrame({'ga': ga_parms.best_x,
                            'pso': pso_parms.best_x,
                            'cs': cs_parms.best_x,
                            'gwo': gwo_parms.best_x,
                            'woa': woa_parms.best_x,
                            'hho': hho_parms.best_x})
    Results.index = ['w1', 'w2', 'b']
    Results = Results.transpose()
    print(Results)
    
    for n in Results.index:
        w_b = list(Results.loc[n])
        w, b = w_b[:-1], w_b[-1]
        plot_result(data, w, b, n)
    
    #%%
    # 作图比较
    plot_Series(fvals.iloc[:, :],
                {'GA': '-', 'PSO': '-', 'CS': '-', 'GWO': '-', 'WOA': '-',
                  'HHO': '-'},
                figsize=(10, 6))
    
    #%%
    close_log_file(logger)
    
    #%%
    print(f'used time: {round(time.time()-strt_tm, 6)}s.')
Exemplo n.º 7
0
        1.19, 1.17, 1.17, 1.18, 1.19, 1.23, 1.24, 1.25, 1.24, 1.25, 1.24, 1.25,
        1.24, 1.25, 1.24, 1.27, 1.23, 1.22, 1.18, 1.2, 1.22, 1.25, 1.25, 1.27,
        1.26, 1.31, 1.32, 1.31, 1.33, 1.33, 1.36, 1.33, 1.35, 1.38, 1.4, 1.42,
        1.45, 1.43, 1.46, 1.48, 1.52, 1.53, 1.52, 1.55, 1.54, 1.53, 1.55, 1.54,
        1.52, 1.53, 1.53, 1.5, 1.45, 1.43, 1.42, 1.41, 1.43, 1.42, 1.45, 1.45,
        1.49, 1.49, 1.51, 1.54, 1.53, 1.56, 1.52, 1.53, 1.58, 1.58, 1.58, 1.61,
        1.63, 1.61, 1.59
    ]
    data = pd.DataFrame(values, columns=['values'])
    # maxDown, (strt_idx, end_idx) = get_MaxDown(data['values'])
    maxDown, (strt_idx, end_idx) = get_MaxUp(data['values'])
    data['in_maxDown'] = 0
    data.loc[data.index[strt_idx:end_idx + 1], 'in_maxDown'] = 1
    plot_Series(data, {'values': '.-b'},
                cols_to_label_info={
                    'values': [['in_maxDown', (1, ), ('.-r', ), ('最大回撤区间', )]]
                },
                grids=True,
                figsize=(11, 7))

    fpath = '../test/510050_daily_pre_fq.csv'
    data = load_csv(fpath)
    data.set_index('date', drop=False, inplace=True)
    data = data.iloc[:, :][['close']]

    plot_Series(data, {'close': '.-b'}, grids=True, figsize=(11, 7))

    maxDown, _, (strt_idx, end_idx) = get_MaxDown_pd(data['close'])
    maxDown = str(round(maxDown, 4))
    data['in_maxDown'] = 0
    data.loc[data.index[strt_idx:end_idx + 1], 'in_maxDown'] = 1
    plot_Series(data, {'close': '.-b'},
Exemplo n.º 8
0
    strt_tm = time.time()

    # 50ETF日线行情
    fpath = '../test/510050_daily_pre_fq.csv'
    df = load_csv(fpath)
    df.set_index('date', drop=False, inplace=True)
    # df = df.reindex(columns=['high', 'low', 'close'])

    # CCI --------------------------------------------------------------------
    N = 14
    r = 0.015
    df['cci'] = CCI(df, N=N, r=r)

    plot_Series(
        df.iloc[-200:, :], {'close': ('.-k', False)},
        cols_styl_low_left={'cci': ('.-b', False)},
        xparls_info={'cci': [(100, 'r', '-', 1.3), (-100, 'r', '-', 1.3)]},
        figsize=(8.5, 7),
        grids=True)

    # EXPMA-------------------------------------------------------------------
    N = 5
    df['expma' + str(N)] = EXPMA(df, N, 'close')

    # MACD--------------------------------------------------------------------
    macds = MACD(df['close'])
    df = df.merge(macds, how='left', left_index=True, right_index=True)

    # 九转序列------------------------------------------------------------------
    N, Lag = 9, 4
    df['dmktd'] = DeMarkTD(df['close'], N, Lag)