Exemple #1
0
def add_watchlist_securities(name, id_type, ids):
    oper = OpOperation(schema.Mutation)
    equities = [schema.PositionSetEquityIdInput(**{id_type: id}) for id in ids]
    securities = schema.WatchlistSecuritiesInput(equities=equities)
    oper.add_watchlist_securities(watchlist_id=get_watchlist_id(name),
                                  securities=securities)
    oper()
Exemple #2
0
def upload_portfolio_positions(name, df, nav=None):
    if "model_provider_id" in df.columns:
        id_type = "model_provider_id"
    else:
        id_type = "sedol"
    if name not in [p.name for p in get_portfolios()]:
        create_portfolio(name)

    port_id = get_portfolio_id(name)
    for date in list(df.date.unique()):
        oper = OpOperation(schema.Mutation)
        equities = [
            schema.PositionSetEquityInput(
                id=schema.PositionSetEquityIdInput(**{id_type: r[1][id_type]}),
                economic_exposure=r[1].economic_exposure,
            ) for r in df.iterrows() if r[1].date == date
        ]
        if nav is not None:
            position_set = schema.PositionSetDateInput(date=date,
                                                       equities=equities,
                                                       equity=nav)
        else:
            position_set = schema.PositionSetDateInput(date=date,
                                                       equities=equities)
        oper = OpOperation(schema.Mutation)
        oper.upload_position_set_date(portfolio_id=port_id, data=position_set)
        oper()
Exemple #3
0
def df_to_position_set(df: pd.DataFrame):
    dates = []
    for dt in df.date.unique():
        equities = [
            schema.PositionSetEquityInput(
                id=schema.PositionSetEquityIdInput(sedol=row.sedol),
                economic_exposure=row.economic_exposure,
            ) for _, row in df[df.date == dt].iterrows()
        ]
        dates += [schema.PositionSetDateInput(equities=equities, date=dt)]
    return schema.PositionSetInput(dates=dates)
Exemple #4
0
def get_market_impact_date(mi_date,
                           deltas,
                           nav,
                           denominator=None,
                           model_id=DEFAULT_MODEL_ID):
    if denominator == None:
        denominator = nav
    equities = [
        schema.PositionSetEquityInput(
            id=schema.PositionSetEquityIdInput(model_provider_id=id),
            economic_exposure=pct_equity * nav,
        ) for id, pct_equity in deltas.items()
    ]
    ps_delta = schema.PositionSetDateInput(date=mi_date, equities=equities)
    oper = OpOperation(schema.Query)
    market_impact = (oper.model(id=model_id).simulation(
        position_set=schema.PositionSetInput()).market_impact(
            position_set_delta=ps_delta,
            equity_id_format="MODEL_PROVIDER_ID",
            scale_format="DEFAULT",
        ))
    market_impact.cost()
    market_impact.contributors().__fields__("id", "cost")
    res = oper()
    total_cost = res.model.simulation.market_impact.cost
    total_cost_pct = total_cost / denominator
    df_total = pd.DataFrame(
        data=[[mi_date, total_cost, total_cost_pct]],
        columns=["date", "total_cost", "total_cost_pct"],
    )

    contributors = [(mi_date, c.id, c.cost, c.cost / denominator)
                    for c in res.model.simulation.market_impact.contributors]
    df_contrib = pd.DataFrame(
        data=contributors,
        columns=["date", "model_provider_id", "cost", "cost_pct"])
    return df_total, df_contrib
Exemple #5
0
def load_optimized_portfolio(df,
                             portfolio_name,
                             nav,
                             forecast_horizon,
                             objective,
                             model_id=DEFAULT_MODEL_ID):
    if portfolio_name not in [p.name for p in op.get_portfolios()]:
        op.create_portfolio(portfolio_name)

    errors = []
    for dt in df.date.unique():
        rows = list(row for index, row in df[df.date == dt].iterrows())
        oper = op.OpOperation(schema.Query)
        long_len = len([r for r in rows if r.expected_return > 0])
        if long_len > 0:
            long_base = nav / long_len
        short_len = len([r for r in rows if r.expected_return <= 0])
        if short_len > 0:
            short_base = -nav / short_len
        equities = [
            schema.PositionSetEquityInput(
                id=schema.PositionSetEquityIdInput(sedol=row.sedol),
                economic_exposure=long_base
                if row.expected_return > 0 else short_base,
            ) for row in rows
        ]

        position_set_dates = [
            schema.PositionSetDateInput(equities=equities, date=dt)
        ]
        position_set = schema.PositionSetInput(dates=position_set_dates)

        forecast_equities = [
            schema.ForecastEquityInput(
                id=schema.PositionSetEquityIdInput(sedol=row.sedol),
                expected_percent_return=row.expected_return,
            ) for row in rows
        ]
        forecast = schema.ForecastInput(horizon=forecast_horizon,
                                        equities=forecast_equities)

        constants = schema.OptimizationConstantsInput(equity=nav)
        optimization = oper.model(id=model_id).optimization(
            position_set=position_set,
            objective=[objective],
            constants=constants,
            constraints=constraints,
            forecast=forecast,
        )
        opt_dates = optimization.positions().dates
        opt_dates.date()
        opt_dates.equities().id().sedol()
        opt_dates.equities().__fields__("id", "economic_exposure")
        results = None
        try:
            results = oper()
        except op.GqlError as gql_err:
            errors.append((dt, gql_err))
        values = [[pos_date.date, equity.id.sedol, equity.economic_exposure]
                  for pos_date in results.model.optimization.positions.dates
                  for equity in pos_date.equities]
        columns = ["date", "sedol", "economic_exposure"]
        df_pos = pd.DataFrame(data=values, columns=columns)
        upload_portfolio_positions(portfolio_name, df_pos, nav=nav)
    return errors