Beispiel #1
0
 def batch_flush(self, codes):
     """刷新批量股票"""
     codes = ensure_list(codes)
     for code in codes:
         sess = get_session()
         self.flush(sess, code)
         sess.close()
Beispiel #2
0
def flush_gpgk(codes=None):
    """
    刷新股票概况信息
    
    说明:
        如初始化则包含所有曾经上市的股票代码,含已经退市
        否则仅包含当前在市的股票代码
    """
    def get_to_do(x):
        p1 = not_yet_updated(x, SpecialTreatment)
        p2 = not_yet_updated(x, ShortName)
        return sorted(list(p1.union(p2)))

    if codes is None:
        codes = get_all_codes(False)
    else:
        codes = ensure_list(codes)
    to_do = get_to_do(codes)
    for i in range(1, 10):
        logger.info('股票代码数量:{}'.format(len(to_do)))
        to_do = batch_flush(to_do)
        to_do = get_to_do(to_do)
        if len(to_do) == 0:
            break
        time.sleep(1)
        if i > 1:
            logger.info('第{}次尝试,其中{}个股票代码失败'.format(i, len(to_do)))
Beispiel #3
0
 def __init__(self,
              asset,
              inds,
              start=pd.datetime.today() - pd.Timedelta(days=60),
              end=pd.datetime.today()):
     self._asset = asset
     self._inds = ensure_list(inds)
     self._start = start
     self._end = end
     self._fig = None
Beispiel #4
0
def flush_adjustment(codes=None):
    """
    刷新股票分红派息数据
    
    说明:
        如初始化则包含所有曾经上市的股票代码,含已经退市
        否则仅包含当前在市的股票代码
    """
    if codes is None:
        codes = get_all_codes(False)
    else:
        codes = ensure_list(codes)
    flush(codes)
Beispiel #5
0
def flush_reports(codes=None):
    """
    刷新财务报告
    
    说明:
        如初始化则包含所有曾经上市的股票代码,含已经退市
        否则仅包含当前在市的股票代码
    """
    if codes is None:
        codes = get_all_codes(False)
    else:
        codes = ensure_list(codes)
    for item in VALID_ITEMS:
        fresher = ReportRefresher(item)
        fresher.batch_flush(codes)
Beispiel #6
0
def _gen_fig(asset, inds, start, end):
    start = pd.Timestamp(start)
    end = pd.Timestamp(end)
    assert end > start, '结束日期必须大于开始日期'
    inds = ensure_list(inds)
    asset = symbols([asset])[0]
    asset_name = asset.asset_name
    # 前移半年
    d_start = pd.Timestamp(start) - pd.Timedelta(days=180)
    df = ohlcv(asset, d_start, end)
    limit_start = df.index[df.index >= start][0]
    limit_end = df.index[df.index <= end][-1]
    # 颜色
    # colors = ['red' if x >= 0 else 'green' for x in df.close.pct_change()]
    diff = (df.close - df.open).loc[limit_start:limit_end]
    colors = ['red' if x >= 0 else 'green' for x in diff]
    title = '{}技术分析图({}到{})'.format(asset_name,
                                    limit_start.strftime(r'%Y-%m-%d'),
                                    limit_end.strftime(r'%Y-%m-%d'))
    fig_setting = _fig_default_setting()
    kwargs = {
        'df': df,
        'limit_start': limit_start,
        'limit_end': limit_end,
        'colors': colors,
    }
    kwargs.update(fig_setting)
    traces = []
    # 主图
    traces.extend(_Candlestick(kwargs))
    traces.extend(_BBANDS(kwargs))
    # 成交量图
    traces.extend(_Volume(kwargs))
    traces.extend(_SMA(kwargs))
    # 其他技术图
    for ind in inds:
        if ind.upper() not in ALLOWED:
            raise NotImplementedError('不支持指标{}\n,可接受的指标:{}'.format(
                ind, ALLOWED))
        traces.extend(_indicators_factory(ind, kwargs))
    fig = _relayout(traces)
    # 不使用Rangeslider及不显示x刻度
    layout = dict(
        title=title,
        showlegend=False,
        xaxis=dict(rangeslider=dict(visible=False), showticklabels=False))
    fig['layout'].update(layout)
    return fig
Beispiel #7
0
def write_dynamic_data_to_bcolz(tables=None):
    """
    将每日变动数据以bcolz格式存储,提高数据集加载速度
    """
    if not tables:
        to_does = DYNAMIC_TABLES
    else:
        to_does = ensure_list(to_does)
    for table in to_does:
        if table in DYNAMIC_TABLES:
            ndays = 0
            logger.info('预处理表:{}'.format(table))
            expr = STOCK_DB[table]
            _write_by_expr(expr, ndays)
        else:
            raise ValueError('要写入的数据表{}不在"{}"范围'.format(table, DYNAMIC_TABLES))
Beispiel #8
0
def symbols(symbols_, symbol_reference_date=None, handle_missing='log'):
    """
    Convert a or a list of str and int into a list of Asset objects.
    
    Parameters:	
        symbols_ (str, int or iterable of str and int)
            Passed strings are interpreted as ticker symbols and 
            resolved relative to the date specified by symbol_reference_date.
        symbol_reference_date (str or pd.Timestamp, optional)
            String or Timestamp representing a date used to resolve symbols 
            that have been held by multiple companies. Defaults to the current time.
        handle_missing ({'raise', 'log', 'ignore'}, optional)
            String specifying how to handle unmatched securities. Defaults to ‘log’.

    Returns:	

    list of Asset objects – The symbols that were requested.
    """
    symbols_ = ensure_list(symbols_)

    allowed_dtype = [str, int, Asset, Equity]

    res = {0: [], 1: [], 2: [], 3: []}
    for s in symbols_:
        try:
            pos = allowed_dtype.index(type(s))
            res[pos].append(s)
        except ValueError as e:
            raise Exception(
                '{} is not str、int or zipline.assets.Asset'.format(s))

    if symbol_reference_date is not None:
        asof_date = pd.Timestamp(symbol_reference_date, tz='UTC')
    else:
        asof_date = pd.Timestamp('today', tz='UTC')

    res[0] = finder.lookup_symbols(res[0], asof_date)
    res[1] = finder.retrieve_all(res[1])

    ret = []
    for s in res.values():
        ret.extend(s)
    return ret
Beispiel #9
0
def flush_stockdaily(codes=None, init=False):
    """
    刷新股票日线数据
    
    说明:
        如初始化则包含所有曾经上市的股票代码,含已经退市
        否则仅包含当前在市的股票代码
    """
    sess = get_session()
    end = sess.query(func.max(TradingCalendar.date)).filter(
        TradingCalendar.is_trading == True).scalar()
    sess.close()
    if end is None:
        raise NotImplementedError('尚未初始化交易日历数据!')
    if init or codes is None:
        codes = get_all_codes(init)
    else:
        codes = ensure_list(codes)
    # 删除无效数据
    _delete()
    flush(codes, end)
Beispiel #10
0
def write_financial_data_to_bcolz(tables=None):
    """
    将财务报告相关表以bcolz格式存储,提高数据集加载速度

    Notes:
    ------
        写入财务报告数据时,默认财务报告公告日期为报告期后45天
    """
    # 首先要刷新数据
    flush_report_dates()
    if not tables:
        to_does = QUARTERLY_TABLES
    else:
        to_does = ensure_list(to_does)
    for table in to_does:
        if table in QUARTERLY_TABLES:
            logger.info('预处理表:{}'.format(table))
            expr = STOCK_DB[table]
            _write_by_expr(expr)
        else:
            raise ValueError('要写入的数据表{}不在"{}"范围'.format(
                table, QUARTERLY_TABLES))
Beispiel #11
0
def flush_shareholder(codes=None):
    """
    刷新股票股东信息
    
    说明:
        如初始化则包含所有曾经上市的股票代码,含已经退市
        否则仅包含当前在市的股票代码
    """
    # if init:
    #     insert_preprocessed_data()
    if codes is None:
        codes = get_all_codes(False)
    else:
        codes = ensure_list(codes)
    # 按代码循环
    for code in codes:
        with session_scope() as sess:
            # 按类型循环
            for type_ in (ShareholderType.main,
                          ShareholderType.circulating,
                          ShareholderType.fund):
                _flush_by(sess, code, type_)
Beispiel #12
0
def flush_dealdetail(codes=None, init=False):
    """
    刷新股票分时交易数据
    
    说明:
        如初始化则包含所有曾经上市的股票代码,含已经退市
        否则仅包含当前在市的股票代码
    """
    sess = get_session()
    if init:
        # 最多前溯2周?
        start = datetime.today().date() - timedelta(days=14)
    else:
        start = datetime.today().date() - timedelta(days=1)
    if init or codes is None:
        codes = get_all_codes(init)
    else:
        codes = ensure_list(codes)
    dates = sess.query(
        TradingCalendar.date).filter(TradingCalendar.date >= start).filter(
            TradingCalendar.is_trading == True).all()
    sess.close()
    flush(codes, dates)
Beispiel #13
0
def symbols(symbols_, symbol_reference_date=None, handle_missing='log'):
    """
    Convert a string or a list of strings into Asset objects.
    Parameters:	

        symbols_ (String or iterable of strings.)
            Passed strings are interpreted as ticker symbols and 
            resolved relative to the date specified by symbol_reference_date.
        symbol_reference_date (str or pd.Timestamp, optional)
            String or Timestamp representing a date used to resolve symbols 
            that have been held by multiple companies. Defaults to the current time.
        handle_missing ({'raise', 'log', 'ignore'}, optional)
            String specifying how to handle unmatched securities. Defaults to ‘log’.

    Returns:	

    list of Asset objects – The symbols that were requested.
    """
    finder = bundle_data.asset_finder
    symbols_ = ensure_list(symbols_)
    first_type = type(symbols_[0])
    for s in symbols_:
        type_ = type(s)
        if type_ != first_type:
            raise TypeError('symbols_列表不得存在混合类型')
    if first_type is Equity:
        return symbols_
    elif first_type is int:
        symbols_ = [str(s).zfill(6) for s in symbols_]

    if symbol_reference_date is not None:
        asof_date = pd.Timestamp(symbol_reference_date, tz='UTC')
    else:
        asof_date = pd.Timestamp('today', tz='UTC')
    res = finder.lookup_symbols(symbols_, asof_date)
    # 返回的是列表
    return res
Beispiel #14
0
 def test_ensure_list(self):
     x = '1'
     self.assertTrue(type(ensure_list(x)), list)
     x = (1, 2, 3)
     self.assertTrue(type(ensure_list(x)), list)