def copy(ctx, optsrc, optdst, optoverride):
    ''' create new stock portfolio by copying existing one
    '''

    if optsrc is None or optdst is None:

        click.echo(
            click.style(
                f'\n Both source id and destination id are required to perform copy.',
                fg='red'))
        return

    src_portfolio_id = optsrc
    dst_portfolio_id = optdst

    if asset_sp_stock_portfolio.load_by_id(
            portfolio_ids=[src_portfolio_id]).shape[0] == 0:

        click.echo(
            click.style(
                f'\n Stock portfolio {src_portfolio_id} doesn\'t exist.',
                fg='red'))
        return

    if not optoverride and asset_sp_stock_portfolio.load_by_id(
            portfolio_ids=[dst_portfolio_id]).shape[0] > 0:

        click.echo(
            click.style(
                f'\n Stock portfolio {dst_portfolio_id} already exists.',
                fg='red'))
        return

    df_stock_portfolio_info = asset_sp_stock_portfolio.load_by_id(
        portfolio_ids=[src_portfolio_id])
    df_stock_portfolio_info.rename(index={src_portfolio_id: dst_portfolio_id},
                                   inplace=True)
    asset_sp_stock_portfolio.save(dst_portfolio_id, df_stock_portfolio_info)

    df_stock_portfolio_argv = asset_sp_stock_portfolio_argv.load(
        portfolio_id=src_portfolio_id)
    df_stock_portfolio_argv.rename(index={src_portfolio_id: dst_portfolio_id},
                                   level='globalid',
                                   inplace=True)
    asset_sp_stock_portfolio_argv.save(dst_portfolio_id,
                                       df_stock_portfolio_argv)

    click.echo(
        click.style(
            f'\n Successfully created stock portfolio {dst_portfolio_id}!',
            fg='green'))
def pos_n_nav(ctx, optid, opttype, begin_date, end_date, cpu_count):
    ''' calculate pos and nav of stock portfolio to update
    '''

    if optid is not None:
        list_portfolio_id = [s.strip() for s in optid.split(',')]
    else:
        list_portfolio_id = None

    if opttype is not None:
        list_type = [s.strip() for s in opttype.split(',')]
    else:
        list_type = None

    if list_portfolio_id is None and list_type is None:

        click.echo(
            click.style(
                f'\n Either stock portfolio id or type is required to perform pos n nav.',
                fg='red'))
        return

    if list_portfolio_id is not None:
        df_stock_portfolio_info = asset_sp_stock_portfolio.load_by_id(
            portfolio_ids=list_portfolio_id)
    else:
        df_stock_portfolio_info = asset_sp_stock_portfolio.load_by_type(
            types=list_type)

    for _, stock_portfolio_info in df_stock_portfolio_info.iterrows():
        pos_n_nav_update(stock_portfolio_info, begin_date, end_date)
def create(ctx, optid, optname, opttype, optalgo, optargv, optoverride):
    ''' create new stock portfolio
    '''

    if optid is None:

        click.echo(
            click.style(
                f'\n Stock portfolio id is required to perform create.',
                fg='red'))
        return

    portfolio_id = optid
    portfolio_name = optname
    portfolio_type = opttype
    portfolio_algo = optalgo
    list_portfolio_argv = [[s2.strip() for s2 in s1.split(':')]
                           for s1 in optargv.split(',')]

    if not optoverride and asset_sp_stock_portfolio.load_by_id(
            portfolio_ids=[portfolio_id]).shape[0] > 0:

        click.echo(
            click.style(f'\n Stock portfolio {portfolio_id} already exists.',
                        fg='red'))
        return

    df_stock_portfolio_info = pd.DataFrame(
        [[portfolio_name, portfolio_type, portfolio_algo]],
        columns=['sp_name', 'sp_type', 'sp_algo'])
    df_stock_portfolio_info['globalid'] = portfolio_id
    df_stock_portfolio_info = df_stock_portfolio_info.set_index('globalid')
    asset_sp_stock_portfolio.save(portfolio_id, df_stock_portfolio_info)

    df_stock_portfolio_argv = pd.DataFrame(
        list_portfolio_argv, columns=['sp_key', 'sp_value', 'sp_desc'])
    df_stock_portfolio_argv['globalid'] = portfolio_id
    df_stock_portfolio_argv = df_stock_portfolio_argv.set_index(
        ['globalid', 'sp_key'])
    asset_sp_stock_portfolio_argv.save(portfolio_id, df_stock_portfolio_argv)

    click.echo(
        click.style(f'\n Successfully created stock portfolio {portfolio_id}!',
                    fg='green'))
def remove(ctx, optid, optexcept):

    if optid is None:

        click.echo(
            click.style(
                f'\n Stock portfolio id is required to perform remove.',
                fg='red'))
        return

    if optexcept is not None:
        list_except_portfolio_id = [s.strip() for s in optexcept.split(',')]
    else:
        list_except_portfolio_id = []

    list_portfolio_id = list(
        asset_sp_stock_portfolio.load_by_id(portfolio_ids=[optid]).index)
    list_portfolio_id = list(
        set(list_portfolio_id) - set(list_except_portfolio_id))

    engine = database.connection('asset')
    metadata = MetaData(bind=engine)
    table_sp = Table('sp_stock_portfolio', metadata, autoload=True)
    table_sp_argv = Table('sp_stock_portfolio_argv', metadata, autoload=True)
    table_sp_pos = Table('sp_stock_portfolio_pos', metadata, autoload=True)
    table_sp_nav = Table('sp_stock_portfolio_nav', metadata, autoload=True)

    for portfolio_id in list_portfolio_id:

        table_sp.delete(table_sp.c.globalid == portfolio_id).execute()
        table_sp_argv.delete(
            table_sp_argv.c.globalid == portfolio_id).execute()
        table_sp_pos.delete(table_sp_pos.c.globalid == portfolio_id).execute()
        table_sp_nav.delete(table_sp_nav.c.globalid == portfolio_id).execute()

    click.echo(
        click.style(
            f'\n Successfully remove stock portfolios {list_portfolio_id}!',
            fg='green'))
def devide(ctx, optsrc, optoverride):
    ''' create new stock portfolio for industries
    '''

    if optsrc is None:

        click.echo(
            click.style(f'\n Src id is required to perform devide.', fg='red'))
        return

    src_portfolio_id = optsrc

    if src_portfolio_id[-2:] != '00':

        click.echo(
            click.style(f'\n Source id {src_portfolio_id} is invalid.',
                        fg='red'))
        return

    if asset_sp_stock_portfolio.load_by_id(
            portfolio_ids=[src_portfolio_id]).shape[0] == 0:

        click.echo(
            click.style(
                f'\n Stock portfolio {src_portfolio_id} doesn\'t exist.',
                fg='red'))
        return

    if not optoverride and asset_sp_stock_portfolio.load_by_id(
            portfolio_ids=[src_portfolio_id[:-2]]).shape[0] > 1:

        click.echo(
            click.style(
                f'\n Stock portfolio for industries {src_portfolio_id[:-2]} already exists.',
                fg='red'))
        return

    sw_industry_code_pool = caihui_tq_sk_basicinfo.load_sw_industry_code_info()
    num_sw_industry = sw_industry_code_pool.shape[0]
    list_dst_portfolio_id = [
        f'{src_portfolio_id[:-2]}{sw_industry_code[:2]}'
        for sw_industry_code in sw_industry_code_pool.index
    ]

    for dst_portfolio_id in list_dst_portfolio_id:

        df_stock_portfolio_info = asset_sp_stock_portfolio.load_by_id(
            portfolio_ids=[src_portfolio_id])
        df_stock_portfolio_info.rename(
            index={src_portfolio_id: dst_portfolio_id}, inplace=True)
        asset_sp_stock_portfolio.save(dst_portfolio_id,
                                      df_stock_portfolio_info)

        df_stock_portfolio_argv = asset_sp_stock_portfolio_argv.load(
            portfolio_id=src_portfolio_id)
        df_stock_portfolio_argv.rename(
            index={src_portfolio_id: dst_portfolio_id},
            level='globalid',
            inplace=True)
        asset_sp_stock_portfolio_argv.save(dst_portfolio_id,
                                           df_stock_portfolio_argv)

    click.echo(
        click.style(
            f'\n Successfully created stock portfolios for industries {list_dst_portfolio_id}!',
            fg='green'))