def test_gg_beta(symbol='000895.sz', start='2018-01-01', plot_price=True, align_to='gg', plot_k=True, ): ''' align_to: str, ['gg', 'dp'], 數據對齊的方式 'gg': 表示對齊到個股, 改變大盤的數據, 以對齊到個股上 'dp': 表示對齊到大盤, 改變個股的數據, 以對齊到大盤上 >>> symbol='000895.sz' >>> start='2018-01-01' >>> beta, df = test_gg_beta(align_to='gg', plot_k=False) >>> beta2, df2 = test_gg_beta(align_to='dp', plot_k=False) >>> beta, df, plots = test_gg_beta('000933.sz') >>> beta, df = test_gg_beta('000933.sz', plot_price=False, plot_k=False) 結果是: 0.83(雙匯發展 @ 2018年) 1.64(神火股份 @ 2018年) ''' _date = datetime.datetime.now().date().isoformat() title='QC圖件: 用於計算個股的Beta係數(DP:399317) \n 製做日期: {}'.format(_date) c=Context(symbol) stk = Stock(c,start) stk.grab_data_from_tdxhq() stk.qfq() stk.grab_index_from_tdxhq() stk.indicator() # 個股一般會有停牌的時候, 因此須要對齊 # 好比以個股爲準, 把大盤對齊到個股是時間戳裏: # 將大盤的收盤線, 插入到個股的ohlc數據框裏(用assign方法) if align_to == 'gg': f = stk.ohlc.close[0] / stk.aindex.close[0] df=stk.ohlc.assign(dp=stk.aindex.close * f) df=df.loc[:, ['close', 'dp']] beta = ttr.estimateBeta(df.close, df.dp) title += '\n{:s}({:s})的Beta係數: {:.4f}(數據對齊到個股)'.format(stk.context.name,stk.context.code, beta) if plot_price: plt.figure() df.plot(title=title) elif align_to == 'dp': f = stk.aindex.close[0] / stk.ohlc.close[0] df2 = stk.aindex.assign(gg=stk.ohlc.close * f) df2 = df2.loc[:, ['close', 'gg']] df2 = df2.fillna(method='ffill') # 向將來填充 (用老數據向下填充) beta2 = ttr.estimateBeta(df2.gg, df2.close) title += '\n{:s}({:s})的Beta係數: {:.4f}(數據對齊到大盤)'.format(stk.context.name,stk.context.code, beta2) if plot_price: plt.figure() df2.plot(title=title) if plot_k: # fig,ax = plt.subplots(1,1) #stk.mycandlestick_ohlc(ax, [20,60]) #stk.mycandlestick_ohlc(ax, with_raw_quotes=True) # stk.mycandlestick_ohlc(ax, with_raw_quotes=False) subset = slice(-120*3,None) # '2017-07' '2017' subset = None plots = pl.Plotter(stk.context, stk, subset) #plot stk data # plots.plot_candle_only( 'lday') plots.plot_candle_vol('lday') #plots.plot_candle_vol('lday', savefig=True) if align_to=='gg': return beta, df else: return beta2, df2
用276天大盤交易日(同期個股是271個交易日)的數據, 採用兩種方法, 獲得的beta值爲:
0.833 vs 0.830
很是接近.python
小於1的beta, 揭示了該股在大盤下跌階段的優異表現.app
def study_block_beta(subset=(8,18,1),show=False): ''' >>> beta, res = study_block_beta() >>> beta, res = study_block_beta(show=True) ''' syms_, syms = read_zxg(subset) betas, names, codes=[],[],[] for enum, sym in enumerate(syms): stk=load_data(sym) sname = stk.context.name code = stk.context.code names.append(sname) codes.append(code) pw=sname_print_width(sname) sdt = str(stk.sdt)[:10] edt = str(stk.edt)[:10] prompt = 'loaded data: {:<{pw}} {} {} {}'.format(sname, code, sdt, edt, pw=pw) print(prompt) beta=ttr.estimateBeta(stk.ohlc.close, stk.aindex.close) betas.append(beta) if show: #make plot close = stk.ohlc.close close = close/close[0] * 10.0 #if not isinstance(fig, plt.Figure): if enum==0: fig=plt.figure(); type(fig) aindex = stk.aindex.close/stk.aindex.close[0] * 10. ax = aindex.plot(label='國證A指', lw=4, logy=True, ) #use_index=) close.plot(axes=ax, label=sname, logy=True,) else: if enum< len(syms) - 1: close.plot(axes=ax, label=sname,logy=True,) else: close.plot(axes=ax, label=sname,logy=True, title='池子裏的收盤線') ax.legend() resS = '{} {:8} {:6} {:6} \n'.format('Num','name', 'code', 'Beta') resS += '-' * 33 + '\n' for i in range(len(names)): pw=sname_print_width(names[i]) resS += '{:3d} {:{pw}} {} {:.3f} \n'.format(i, names[i], codes[i], betas[i], pw=pw) print( resS) return betas, resS
0 深圳燃氣 601139 0.732
1 分衆傳媒 002027 1.392
2 海康威視 002415 1.378
3 雙匯發展 000895 0.839
4 柳 工 000528 1.336
5 上海銀行 601229 0.561
6 華蘭生物 002007 0.880
7 兗州煤業 600188 1.251
8 雲鋁股份 000807 1.586
9 神火股份 000933 1.6273d
300個交易日: 2017-11-28 -- 2019-02-22 時間段的數據:
code
520個交易日: 2017-01-03 -- 2019-02-22 時間段的數據:
orm