Exemple #1
0
 def handle_bar(context, bar_dict):
     df = all_instruments('FenjiA')
     df_to_assert = df.loc[df['order_book_id'] == '150247.XSHE']
     assert df_to_assert.iloc[0, 0] == 'CMAJ'
     assert df_to_assert.iloc[0, 7] == '工银中证传媒A'
     assert all_instruments().shape >= (8000, 4)
     assert all_instruments('CS').shape >= (3000, 16)
     assert all_instruments('ETF').shape >= (120, 9)
     assert all_instruments('LOF').shape >= (130, 9)
     assert all_instruments('FenjiMu').shape >= (10, 9)
     assert all_instruments('FenjiA').shape >= (120, 9)
     assert all_instruments('FenjiB').shape >= (140, 9)
     assert all_instruments('INDX').shape >= (500, 8)
     assert all_instruments('Future').shape >= (3500, 16)
def init(context):
    """
        Description : 程序初始化
        Arg :
        Returns :
        Raises	 :
    """
    # 这里设置参数
    context.LST_MA = [5, 10, 20, 60]   # 均线。
    context.LST_DUOTOU = []     # 保存的是均线持续多头的列表。
    context.DUOTOU_DAYS = 7      # 均线排列持续天数判断为多头趋势
    context.BACK_MA = 10        # 回测均线
    context.NUM_STOCKS = 10      # 最多持有股票数量
    context.STOP_PERCENT = 10   # 止损比例

    # 获得所有股票,
    _all_cn_stock = list(all_instruments('CS')['order_book_id'])
    # 删除ST股票, 不对ST股票做回测。
    # 上市90天的股票不做回测,因为前几天都是疯涨。然后暴跌。
    _all_cn_stock = [
        _stock for _stock in _all_cn_stock
        if not is_st_stock(_stock, 1)
        and is_predate_listed_date(_stock, 90)]

    for _book_id in _all_cn_stock:
        # 在这里添加指标的相关数据
        # 如下先取得K线数据
        _close = get_bars_all(_book_id, '1d', "close")
        _open = get_bars_all(_book_id, '1d', "open")
        _high = get_bars_all(_book_id, '1d', "high")
        _low = get_bars_all(_book_id, '1d', "low")
        _limit_down = get_bars_all(_book_id, '1d', "limit_down")
        _limit_up = get_bars_all(_book_id, '1d', "limit_up")
        _total_turnover = get_bars_all(_book_id, '1d', "total_turnover")
        _volume = get_bars_all(_book_id, '1d', "volume")
        # 计算指标
        # _ma_5 = talib.SMA(_close, context.TIME_PERIOD)
        # 将所有的均线保存在这个数组中。
        _lst_ma = []
        for _ma in context.LST_MA:
            _lst_ma.append(talib.SMA(_close, _ma))
        # 然后取得一个逻辑数组,是判断是否是均线多头的。
        _bool_up = []
        for _i in range(len(_lst_ma) - 1):
            # 每个都跟后边的比较一下。
            _bool_up = _bool_up and (_lst_ma[_i + 1] > _lst_ma[_i])
        # 将指标数据保存
        add_indicator(_book_id, '1d', "duotou", _bool_up)
        # 然后有个变量看看是否回撤的
        _ma_huiche = talib.SMA(_close, context.BACK_MA)
        _huiche = _close < _ma_huiche
        # 保存这个回撤的
        add_indicator(_book_id, '1d', "huiche", _huiche)
    # 设置进入股票池
    update_universe(_all_cn_stock)
    pass
Exemple #3
0
    def handle_bar(context, bar_dict):
        date = context.now.replace(hour=0, minute=0, second=0)
        df = all_instruments('CS')
        assert (df['listed_date'] <= date).all()
        assert (df['de_listed_date'] >= date).all()
        assert all(not is_suspended(o) for o in df['order_book_id'])
        assert (df['type'] == 'CS').all()

        df1 = all_instruments('Stock')
        assert sorted(df['order_book_id']) == sorted(df1['order_book_id'])

        df2 = all_instruments('Future')

        assert (df2['type'] == 'Future').all()
        assert (df2['listed_date'] <= date).all()
        assert (df2['de_listed_date'] >= date).all()

        df3 = all_instruments(['Future', 'Stock'])
        assert sorted(list(df['order_book_id']) + list(df2['order_book_id'])) == sorted(df3['order_book_id'])
Exemple #4
0
    def handle_bar(context, bar_dict):
        date = context.now.replace(hour=0, minute=0, second=0)
        df = all_instruments('CS')
        assert (df['listed_date'] <= date).all()
        assert (df['de_listed_date'] >= date).all()
        assert all(not is_suspended(o) for o in df['order_book_id'])
        assert (df['type'] == 'CS').all()

        df1 = all_instruments('Stock')
        assert sorted(df['order_book_id']) == sorted(df1['order_book_id'])

        df2 = all_instruments('Future')

        assert (df2['type'] == 'Future').all()
        assert (df2['listed_date'] <= date).all()
        assert (df2['de_listed_date'] >= date).all()

        df3 = all_instruments(['Future', 'Stock'])
        assert sorted(list(df['order_book_id']) + list(df2['order_book_id'])) == sorted(df3['order_book_id'])
Exemple #5
0
def init(context):
    """
        Description : 程序初始化
        Arg :
        Returns :
        Raises	 :
    """
    # 这里设置参数
    context.TIME_PERIOD = 5

    context.N = 20  # 计算多少日内的
    context.M = 10  # %M
    context.loss = 10  # 止损百分比
    context.min_up = 3  # 最低上涨
    # 获得所有股票,
    _all_cn_stock = list(all_instruments('CS')['order_book_id'])
    # 删除ST股票, 不对ST股票做回测。
    # 上市90天的股票不做回测,因为前几天都是疯涨。然后暴跌。
    _all_cn_stock = [
        _stock for _stock in _all_cn_stock
        if not is_st_stock(_stock, 1) and is_predate_listed_date(_stock, 90)
    ]

    # 保存股票列表
    context.stocks = _all_cn_stock

    for _book_id in _all_cn_stock:
        pass
        # 在这里添加指标的相关数据
        # 如下先取得K线数据
        # _close = get_bars_all(_book_id, '1d', "close")
        # _open = get_bars_all(_book_id, '1d', "open")
        # _high = get_bars_all(_book_id, '1d', "high")
        # _low = get_bars_all(_book_id, '1d', "low")
        # _limit_down = get_bars_all(_book_id, '1d', "limit_down")
        # _limit_up = get_bars_all(_book_id, '1d', "limit_up")
        # _total_turnover = get_bars_all(_book_id, '1d', "total_turnover")
        # _volume = get_bars_all(_book_id, '1d', "volume")
        # # 计算指标
        # _ma_5 = talib.SMA(_close, context.TIME_PERIOD)
        # # 将指标数据保存
        # add_indicator(_book_id, '1d', "ma_5", _ma_5)
        #
    # 设置进入股票池
    update_universe(_all_cn_stock)
    pass