Example #1
0
File: c4.py Project: 3774257/abu
def sample_415():
    """
    4.1.5 重采样数据
    :return
    """
    days = pd.date_range('2017-1-1',
                         periods=stock_day_change.shape[1], freq='1d')
    stock_symbols = ['股票 ' + str(x) for x in
                     xrange(stock_day_change.shape[0])]
    df = pd.DataFrame(stock_day_change, index=stock_symbols, columns=days)
    df = df.T
    df_stock0 = df['股票 0']

    # 以5天为周期重采样(周k)
    df_stock0_5 = pd_resample(df_stock0.cumsum(), '5D', how='ohlc')
    # 以21天为周期重采样(月k),
    # noinspection PyUnusedLocal
    df_stock0_20 = pd_resample(df_stock0.cumsum(), '21D', how='ohlc')
    # 打印5天重采样,如下输出2017-01-01, 2017-01-06, 2017-01-11, 表4-6所示
    print('df_stock0_5.head():\n', df_stock0_5.head())

    from abupy import ABuMarketDrawing
    # 图4-2所示
    ABuMarketDrawing.plot_candle_stick(df_stock0_5.index,
                                       df_stock0_5['open'].values,
                                       df_stock0_5['high'].values,
                                       df_stock0_5['low'].values,
                                       df_stock0_5['close'].values,
                                       np.random.random(len(df_stock0_5)),
                                       None, 'stock', day_sum=False,
                                       html_bk=False, save=False)

    print('type(df_stock0_5.open.values):', type(df_stock0_5['open'].values))
    print('df_stock0_5.open.index:\n', df_stock0_5['open'].index)
    print('df_stock0_5.columns:\n', df_stock0_5.columns)
Example #2
0
def sample_415():
    """
    4.1.5 重采样数据
    :return
    """
    days = pd.date_range('2017-1-1',
                         periods=stock_day_change.shape[1],
                         freq='1d')
    stock_symbols = ['股票 ' + str(x) for x in xrange(stock_day_change.shape[0])]
    df = pd.DataFrame(stock_day_change, index=stock_symbols, columns=days)
    df = df.T
    df_stock0 = df['股票 0']

    # 以5天为周期重采样(周k)
    df_stock0_5 = pd_resample(df_stock0.cumsum(), '5D', how='ohlc')
    # 以21天为周期重采样(月k),
    # noinspection PyUnusedLocal
    df_stock0_20 = pd_resample(df_stock0.cumsum(), '21D', how='ohlc')
    # 打印5天重采样,如下输出2017-01-01, 2017-01-06, 2017-01-11, 表4-6所示
    print('df_stock0_5.head():\n', df_stock0_5.head())

    from abupy import ABuMarketDrawing
    # 图4-2所示
    ABuMarketDrawing.plot_candle_stick(df_stock0_5.index,
                                       df_stock0_5['open'].values,
                                       df_stock0_5['high'].values,
                                       df_stock0_5['low'].values,
                                       df_stock0_5['close'].values,
                                       np.random.random(len(df_stock0_5)),
                                       None,
                                       'stock',
                                       day_sum=False,
                                       html_bk=False,
                                       save=False)

    print('type(df_stock0_5.open.values):', type(df_stock0_5['open'].values))
    print('df_stock0_5.open.index:\n', df_stock0_5['open'].index)
    print('df_stock0_5.columns:\n', df_stock0_5.columns)
Example #3
0
# df_stock0=Trydataframe6['股票0']
# print(df_stock0)
# dt_stock0_draw = df_stock0.cumsum().plot()
# plt.show()

Trydataframe6_5 = Trydataframe6.resample('21D', how='ohlc')['股票0']
# print(Trydataframe6_5)
# print(Trydataframe6_5['open'])
print(Trydataframe6_5.columns)

ABuMarketDrawing.plot_candle_stick(Trydataframe6_5.index,
                                   Trydataframe6_5['open'].values,
                                   Trydataframe6_5['high'].values,
                                   Trydataframe6_5['low'].values,
                                   Trydataframe6_5['close'].values,
                                   np.random.random(len(Trydataframe6_5)),
                                   None,
                                   'stock',
                                   day_sum=False,
                                   html_bk=False,
                                   save=False)
# print(stock_day_change)
# print(stock_day_change[0:2,:5])
# tmp=stock_day_change[0:1,:5].copy()
# stock_day_change[0:2,0:5]=stock_day_change[-2:,-5:]
# print(stock_day_change[0:2,0:5],stock_day_change[-2:,-5:])
# mask = stock_day_change[0:2,0:5]>0.5
# print(mask)
# tmp_test = stock_day_change[0:2,0:5].copy()
# aa = tmp_test[mask]
# print(mask,aa)
# 2018-01-16 -6.890551 -6.091618 -6.890551 -6.827189
# 2018-01-21 -5.682790 -5.682790 -8.021937 -7.980051
"""
下面的代码使用abu量化系统中的ABuMarketDrawing.plot_candle_stick()方法,将数据的高开低收K线图绘制出来,
从而得到从日线级别的数据重新构建出轴线的数据,并且可视化
*注意下面K线线图的volum成交量通过np.random.random(len(df_stock5))生成的随机数据进行填充
*k线图的绘制将在数据可视化章节中详细的说明
"""
from abupy import ABuMarketDrawing

ABuMarketDrawing.plot_candle_stick(df_stock5.index,
                                   df_stock5['open'].values,
                                   df_stock5['high'].values,
                                   df_stock5['low'].values,
                                   df_stock5['close'].values,
                                   np.random.random(len(df_stock5)),
                                   None,
                                   'stock',
                                   day_sum=False,
                                   html_bk=False,
                                   save=False)
#结果如图pandas-2所示,类似的可以得到月k线
ABuMarketDrawing.plot_candle_stick(df_stock20.index,
                                   df_stock20['open'].values,
                                   df_stock20['high'].values,
                                   df_stock20['low'].values,
                                   df_stock20['close'].values,
                                   np.random.random(len(df_stock20)),
                                   None,
                                   'stock',
                                   day_sum=False,