示例#1
0
def test_get_or_create_library_not_exist():
    lib_name = '{}{}'.format(RUN_TEST_LIBRARY, random.randint(1, 100))
    lib = models.get_or_create_library(lib_name)
    assert isinstance(lib, arctic.store.version_store.VersionStore)

    models.drop_library(lib_name)
    assert models.get_library(lib_name) is None
示例#2
0
 def __init__(self, coll_name):
     self._lib_name = conf.CN_STOCK_LIBNAME
     self._coll_name = coll_name
     self._library = get_or_create_library(self._lib_name)
     self._unused_cols = [
         'price_change', 'p_change', 'ma5', 'ma10', 'ma20', 'v_ma5',
         'v_ma10', 'v_ma20', 'turnover'
     ]
     self._new_added_colls = []
示例#3
0
    def get_params(cls, stock_id):
        """
        Get the params of the stock_id for this strategy.
        :param stockid:
        :return: dict(like dict(ma_periods=dict(ma_period_s=0, ma_period_l=0, stock_id='0')))
        """
        lib = get_or_create_library(conf.STRATEGY_PARAMS_LIBNAME)
        symbol = cls.name

        params_list = lib.read(symbol).data
        params = params_list.loc[stock_id, 'params']

        return params
示例#4
0
    def write_daily_alert(cls, symbol, stock_id, action):
        """
        write daily stock alert to MongoDB.
        :param symbol: Arctic symbol
        :param data: dict, like: {'stock': '000651', 'action': 'buy/sell'}
        :return: None
        """

        lib = get_or_create_library(conf.DAILY_STOCK_ALERT_LIBNAME)

        data = {'stock': stock_id, 'action': action}
        df = pd.DataFrame([data], columns=data.keys())
        if symbol in lib.list_symbols():
            lib.append(symbol, df)
        else:
            lib.write(symbol, df)
示例#5
0
def test_get_or_create_library_exist():
    lib = models.get_or_create_library(RUN_TEST_LIBRARY)
    assert isinstance(lib, arctic.store.version_store.VersionStore)
示例#6
0
    """

    pool = gevent.pool.Pool(pool_size)
    for i in range(len(stocks) // pool_size + 1):
        start = i * pool_size
        end = (i + 1) * pool_size
        lst = stocks[start:end]
        logger.debug(f'download delta data for stock list: {lst}')
        for stock in lst:
            pool.spawn(bdt.TsHisData.download_one_delta_data, stock)
        pool.join(timeout=30)


if __name__ == '__main__':
    # make sure the library exists
    models.get_or_create_library(conf.CN_STOCK_LIBNAME)

    # download_delta_data(['000651', '000001'])

    # hs300s = ts.get_hs300s()
    # stock_pools = hs300s['code'].tolist() if 'code' in hs300s else []

    # 查询当前所有正常上市交易的股票列表
    data = pro.stock_basic(exchange='SZSE', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
    stock_pools = data['ts_code'].tolist() if 'ts_code' in data else []

    # stock_pools = ['600000.SH','600036.SH']

    if not stock_pools:
        logger.warning('can not stock pools return empty.')
        stock_pools = models.get_cn_stocks()
示例#7
0
    :return: None
    """

    task = btasks.Task(bsm.MATrendStrategy, stock)
    params = task.train()
    # write stock params to MongoDB
    symbol = conf.STRATEGY_PARAMS_MA_SYMBOL
    models.save_training_params(symbol, params)


def main(stock_pools):
    """
    Get all stocks and train params for each stock.
    :param stock_pools: list, the stock code list.
    :return: None
    """

    for stock in stock_pools:
        train(stock)


if __name__ == '__main__':
    # create params library if not exist
    models.get_or_create_library(conf.STRATEGY_PARAMS_LIBNAME)

    cn_stocks = models.get_cn_stocks()
    main(cn_stocks)

    # training 时会写数据到 `conf.DAILY_STOCK_ALERT_LIBNAME`
    models.drop_library(conf.DAILY_STOCK_ALERT_LIBNAME)