Exemplo n.º 1
0
def _get_mc(p, mc_budget, mp, dw):
    # Bi
    up_level = 0.06
    dn_level = -0.02
    cut_loss = 0.05
    bi_level = 4
    slope = np.zeros(len(p.columns))
    c = 0.0
    mc = mc_budget.copy()
    for e in range(0, len(p.columns)):
        cA, cD = ts_swt(p[p.columns[e]].get_values(), bi_level + 1)
        # Slope
        est_trend, s = est_trend_1(cA[bi_level])
        # calc mc_budget
        if s[-1] > up_level:
            #mc[e] = 0.9*mc_budget[e]
            if max(p[p.columns[e]]) > p[p.columns[e]].iloc[-1] * (1 +
                                                                  cut_loss):
                if mc_budget[e] > 0.5:
                    mc[e] = 0.95 * mc_budget[e]
        elif s[-1] < dn_level:
            mc[e] = 1.01 * mc_budget[e]
        if mc[e] < 0.0001:
            mc[e] = 0.0
        c = c + mc[e]
        slope[e] = s[-1]
    if c > 1:
        for e in range(0, len(mc)):
            mc[e] = mc[e] / c
    return mc, slope
Exemplo n.º 2
0
def _get_mc(p, mc_budget, mp, dw):
    # Bi
    mc_start = [0.8, 0.2]
    up_level = 0.08
    dn_level = -0.08
    up_step = 0.05
    dn_step = 0.20
    cut_loss = 0.05
    bi_level = 4
    est_trend = np.zeros(len(p.columns))
    slope = np.zeros(len(p.columns))
    c = mc_budget[0]
    mc = mc_budget.copy()
    for e in range(1,len(p.columns)):
        cA, cD = ts_swt(p[p.columns[e]].get_values(), bi_level+1)
        # Slope
        t, s= est_trend_1(cA[bi_level])
        # calc mc
        if t[-1] > 0 and t[-2] >= 0: #正趋势中
            if s[-1] <= up_level:
                # 如果现值大于mc_start,则表示没止损过,继续定投;否则继续等待
                if mc[e] > mc_start[e]:
                    mc[e] = mc_budget[e] + up_step
                    if mc[e] > 1.0:
                        mc[e] = 1.0
            elif s[-1] > up_level:
                # cut loss
                if max(p[p.columns[e]]) > p[p.columns[e]].iloc[-1]*(1+cut_loss):
                    mc[e] = mc_start[e]
                    mc[0] = mc_start[0]
                    c = mc[0]
        elif t[-1] >= 0 and t[-2] < 0: #开始正趋势中
            mc[e] = mc_budget[e] + up_step
        elif t[-1] < 0:
            mc[e] = mc_budget[e] - dn_step
            if mc[e] < mc_start[e]:
                mc[e] = mc_start[e]
            if s[-1] < dn_level:
                mc[e] = mc_budget[e] + up_step
        if mc[e] < 0.0001:
            mc[e] = 0.0
        c= c + mc[e]
        slope[e] = s[-1]
        est_trend[e] = t[-1]
    for e in range(0,len(mc)):
        mc[e] = mc[e]/c
    w = mc
    return w, mc, est_trend #slope
# 准备测试数据
end_year = 2018
start_year = 2013
hist_p = source_data[source_data.index.year <= end_year]
hist_p = hist_p[hist_p.index.year >= start_year]
wp = hist_p.resample('W', loffset=pd.offsets.timedelta(days=-6)).mean()
df = wp.copy()  #[3:n]

# Calc Bench (Wavelet) and Post_Bi
bi_level = 4
cA, cD = swt(df[etf_tickers].get_values(), bi_level + 1)
df['bi_post'] = bi(df[etf_tickers].get_values(),
                   df[etf_tickers].get_values(),
                   bench=cA[bi_level])
cA, cD = ts_swt(df[etf_tickers].get_values(), bi_level + 1)
df['wt_long'] = cA[bi_level]
# calc sub_indicator
df['dn_line'] = -0.02
df['up_line'] = 0.08
#
df['est_trend'], df['slope'] = est_trend_1(df['wt_long'])

# select data to plot
df = df[df.index.year >= start_year + 1]
df['Adj Close'] = df[etf_tickers]
# plot ohlc candlestick
plot_chart(df,
           sub_indicator_cols=['slope', 'dn_line', 'up_line'],
           bi_cols=['bi_post'])
Exemplo n.º 4
0
def _get_mc(p, mc_budget, mp, dw):
    # Bi
    bi_level = 4    
    up_step = 0.05
    dn_step = 0.20
    cut_loss = 0.05
    # each asset weight limit
    main_asset_mc_limit = 0.95
    single_asset_mc_limit = 0.10
    # mc_start
    single_asset_mc_init = 0.0
    mc_start = [0.8, 0.2]
    if len(p.columns)>2:
        for e in range(2,len(p.columns)):
            mc_start.append(single_asset_mc_init)
    est_trend = np.zeros(len(p.columns))
    slope = np.zeros(len(p.columns))
    mc = mc_budget.copy()    
    for e in range(1,len(p.columns)):
        cA, cD = ts_swt(p[p.columns[e]].get_values(), bi_level+1)
        # 指数ETF
        if e <= 1:
            # Slope
            t, s= est_trend_1(cA[bi_level])
            s_std = s.std()
            up_level = 2.0*s_std
            #dn_level = -2.0*s_std
            # calc mc
            if t[-1] > 0 and t[-2] >= 0: #正趋势中
                if s[-1] <= up_level:
                    # 如果现值大于mc_start,则表示没止损过,继续定投;否则继续等待
                    if mc[e] >= mc_start[e]:
                        mc = _get_up_step(e, mc, up_step, main_asset_mc_limit)
                elif s[-1] > up_level:
                    # cut loss
                    if max(p[p.columns[e]]) > p[p.columns[e]].iloc[-1]*(1+cut_loss):
                        mc = _cut_loss(e, mc, mc_start, up_step)
            elif t[-1] >= 0 and t[-2] < 0: #开始正趋势中
                mc = _get_up_step(e, mc, up_step, main_asset_mc_limit)
            elif t[-1] < 0:                
                mc = _get_dn_step(e, mc, dn_step, mc_start)
                # 有限的抄底?
                #if s[-1] < dn_level:
                #    mc = _get_up_step(e, mc, up_step, main_asset_mc_limit)
            if mc[e] < 0.0001:
                mc[e] = 0.0
            slope[e] = s[-1]
            est_trend[e] = t[-1]
        # 行业ETF
        elif e > 1:
            # Slope
            t2, s2= est_trend_1(cA[bi_level]) 
            # calc mc 正趋势且强于指数
            if t2[-1] > 0 and sum(s2[-5:-1])>sum(s[-5:-1]):
                if s2[-1] > -100.0:
                    # 如果现值大于mc_start,则表示没止损过,继续定投;否则继续等待
                    if mc[e] >= mc_start[e]:
                        mc = _get_up_step(e, mc, up_step, single_asset_mc_limit)
                elif s2[-1] > 0.0:
                    # cut loss
                    if max(p[p.columns[e]]) > p[p.columns[e]].iloc[-1]*(1+cut_loss):
                        mc = _cut_loss(e, mc, mc_start, up_step)
            elif t2[-1] < 0:
                mc = _get_dn_step(e, mc, dn_step, mc_start)
            slope[e] = s2[-1]
            est_trend[e] = t2[-1]
        if mc[e]<0.0001:
            mc[e]=0.0
    mc = _rebalance(mc)
    w = mc
    return w, mc, est_trend #slope
                          parse_dates=True,
                          index_col=0)

# 准备测试数据
end_year = 2018
start_year = 2013
hist_p = source_data[source_data.index.year <= end_year]
hist_p = hist_p[hist_p.index.year >= start_year]
df = hist_p.copy()

# Calc Bench (Wavelet) and Post_Bi
bi_level = 4
cA, cD = swt(df['Adj Close'], bi_level + 1)
if 'High' in df.columns.values:
    df['bi_post'] = bi(df['High'], df['Low'], bench=cA[bi_level])
else:
    df['bi_post'] = bi(df['Adj Close'], df['Adj Close'], bench=cA[bi_level])
cA, cD = ts_swt(df['Adj Close'], bi_level + 1)
# calc sub_indicator
df['real_trend'] = real_trend(df['bi_post'])
df['mean'], df['std'] = ts_mean_std(df['Adj Close'])
df['diff'] = df['std'] * df['std'] + df['mean']
#df['cross'] = cross(df['diff'], v=0.01)
df['zeros'] = 0
#
df['est_trend'], df['slope'] = est_trend_1(df['diff'])

# select data to plot
df = df[df.index.year >= start_year + 1]
# plot ohlc candlestick
plot_chart(df, sub_indicator_cols=['diff', 'zeros'], bi_cols=['bi_post'])
def _get_mc(p, mc_budget, mp, dw):
    # Bi
    bi_level = 5
    up_step = 0.01
    dn_step = 0.05
    cut_loss = 0.05
    # each asset weight limit
    main_asset_mc_limit = 0.95
    single_asset_mc_limit = 0.10
    # mc_start
    single_asset_mc_init = 0.0
    mc_start = [0.8, 0.2]
    if len(p.columns)>2:
        for e in range(2,len(p.columns)):
            mc_start.append(single_asset_mc_init)
    est_trend = np.zeros(len(p.columns))
    slope = np.zeros(len(p.columns))
    mc = mc_budget.copy()    
    for e in range(1,len(p.columns)):
        cA, cD = ts_swt(p[p.columns[e]].get_values(), bi_level+1)
        # 指数ETF
        if e == 1:
            # Slope
            wt_long = cA[bi_level]
            wt_short = cA[bi_level-2]
            t1, s= est_trend_1(wt_long)
            # mean-std
            mean, std = ts_mean_std(p[p.columns[e]])
            diff = std*std + mean
            t2 = 0.0
            if diff[-1] < 0:
                t2 = -1.0
            else:
                t2 = 1.0
            t3 = t1[-1] + t2
            diff_wt = 10*(wt_short[-1]/wt_long[-1]-1)
            if t3<0:
                if mc[e] <= main_asset_mc_limit:              
                    mc[e] = mc[e]+up_step
                    mc[0] = mc[0]-up_step
            elif t3>0 and diff_wt>0.5:
                if mc[e] >= dn_step:
                    mc[e] = mc[e]-dn_step
                    mc[0] = mc[0]+dn_step
        # 行业ETF
        elif e > 1:
            # Slope
            t2, s2= est_trend_1(cA[bi_level]) 
            # calc mc 正趋势且强于指数
            if t2[-1] > 0 and sum(s2[-5:-1])>sum(s[-5:-1]):
                if s2[-1] > -100.0:
                    # 如果现值大于mc_start,则表示没止损过,继续定投;否则继续等待
                    if mc[e] >= mc_start[e]:
                        mc = _get_up_step(e, mc, up_step, single_asset_mc_limit)
                elif s2[-1] > 0.0:
                    # cut loss
                    if max(p[p.columns[e]]) > p[p.columns[e]].iloc[-1]*(1+cut_loss):
                        mc = _cut_loss(e, mc, mc_start, up_step)
            elif t2[-1] < 0:
                mc = _get_dn_step(e, mc, dn_step, mc_start)
            slope[e] = s2[-1]
            est_trend[e] = t2[-1]
        if mc[e]<0.0001:
            mc[e]=0.0
    mc = _rebalance(mc)
    w = mc
    return w, mc, est_trend #slope
def _get_mc(p, mc_budget, mp, dw):
    # Bi
    mc_start = [0.8, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    up_step = 0.05
    dn_step = 0.20
    cut_loss = 0.05
    bi_level = 4
    est_trend = np.zeros(len(p.columns))
    slope = np.zeros(len(p.columns))
    c = mc_budget[0]
    mc = mc_budget.copy()
    for e in range(1, len(p.columns)):
        cA, cD = ts_swt(p[p.columns[e]].get_values(), bi_level + 1)
        if e <= 1:
            # Slope
            t, s = est_trend_1(cA[bi_level])
            s_std = s.std()
            up_level = 2.0 * s_std
            dn_level = -2.0 * s_std
            # calc mc
            if t[-1] > 0 and t[-2] >= 0:  #正趋势中
                if s[-1] <= up_level:
                    # 如果现值大于mc_start,则表示没止损过,继续定投;否则继续等待
                    if mc[e] > mc_start[e]:
                        mc[e] = mc_budget[e] + up_step
                        if mc[e] > 1.0:
                            mc[e] = 1.0
                elif s[-1] > up_level:
                    # cut loss
                    if max(p[p.columns[e]]) > p[p.columns[e]].iloc[-1] * (
                            1 + cut_loss):
                        mc[e] = mc_start[e]
                        mc[0] = mc_start[0]
                        c = mc[0]
            elif t[-1] >= 0 and t[-2] < 0:  #开始正趋势中
                mc[e] = mc_budget[e] + up_step
            elif t[-1] < 0:
                mc[e] = mc_budget[e] - dn_step
                if mc[e] < mc_start[e]:
                    mc[e] = mc_start[e]
                if s[-1] < dn_level:
                    mc[e] = mc_budget[e] + up_step
            if mc[e] < 0.0001:
                mc[e] = 0.0
            slope[e] = s[-1]
            est_trend[e] = t[-1]
        elif e > 1:
            # Slope
            t2, s2 = est_trend_1(cA[bi_level])
            # calc mc 正趋势且强于指数
            if t2[-1] > 0 and sum(s2[-5:-1]) > sum(s[-5:-1]):
                if s2[-1] > -100.0:
                    # 如果现值大于mc_start,则表示没止损过,继续定投;否则继续等待
                    if mc[e] >= mc_start[e]:
                        mc[e] = mc_budget[e] + up_step
                        if mc[e] > min(0.2, 0.5 / (len(mc) - 2)):
                            mc[e] = min(0.2, 0.5 / (len(mc) - 2))
                elif s2[-1] > 0.0:
                    # cut loss
                    if max(p[p.columns[e]]) > p[p.columns[e]].iloc[-1] * (
                            1 + cut_loss):
                        mc[e] = mc_start[e]
            elif t2[-1] < 0:
                mc[e] = mc_budget[e] - dn_step
                if mc[e] < mc_start[e]:
                    mc[e] = mc_start[e]
            slope[e] = s2[-1]
            est_trend[e] = t2[-1]
        if mc[e] < 0.0001:
            mc[e] = 0.0
        c = c + mc[e]
    for e in range(0, len(mc)):
        mc[e] = mc[e] / c
    w = mc
    return w, mc, est_trend  #slope
Exemplo n.º 8
0
# calc main_indicator
#df['bolu'], df['bold'] = ts_boll(df['Adj Close'], dw=125, n_std=1.5)
# Calc Bench (Wavelet) and Post_Bi
bi_level = 5
cA, cD = swt(df['Adj Close'], bi_level + 1)
if 'High' in df.columns.values:
    df['bi_post'] = bi(df['High'], df['Low'], bench=cA[bi_level])
else:
    df['bi_post'] = bi(df['Adj Close'], df['Adj Close'], bench=cA[bi_level])
cA, cD = ts_swt(df['Adj Close'], bi_level + 1)
df['wt_long'] = cA[bi_level]
df['wt_short'] = cA[bi_level - 2]
df['diff_wt'] = (df['wt_short'] / df['wt_long'] - 1) * 10
# calc sub_indicator
df['real_trend'] = real_trend(df['bi_post'])
df['est_trend_1'], df['slope'] = est_trend_1(df['wt_long'], v=0.5)
#print(display_match_trend(get_match_trend(df['real_trend'], df['est_trend_1'])))
df['zeros'] = 0
# mean-std
df['mean'], df['std'] = ts_mean_std(df['Adj Close'])
df['diff'] = 2 * df['std'] * df['std'] + df['mean']
df['est_trend_2'] = 0.0
df.est_trend_2.loc[df['diff'] < 0] = -0.8
df.est_trend_2.loc[df['est_trend_1'] > 0] = 0.0
df.est_trend_2.loc[df['diff_wt'] > 0.5] = 0.8
df['risk'] = 1 / df['std'] / 100
# select data to plot
df = df[df.index.year >= start_year + 1]

# plot ohlc candlestick
plot_chart(df,