Risk analysis

from qcs import AllocationSnapshot, Context

currency = "EUR"
positions = [
    {"ticker": "EQUITYEUR68", "size": 100.0},
    {"ticker": "EQUITYEUR70", "size": 200.0},
]

# Alternatively specify the positions by using allocation in portfolio currency
# positions = [
#     {"ticker": "EQUITYEUR68", "allocation": 10000.0},
#     {"ticker": "EQUITYEUR70", "allocation": 10000.0},
# ]

benchmark_snapshot = AllocationSnapshot(currency=currency, positions=positions)
snapshot = AllocationSnapshot(
    currency=currency, positions=positions, benchmark=benchmark_snapshot
)

# Or import snapshot and its benchmark from csv
# snapshot = qcs.import_snapshot_from_csv("portfolios.csv", code="PORT1", sep=",")

assets = [
    {
        "code": "<EQUITYEUR68>",
        "label": "Equity EUR n. 68",
        "type": "EQT",
        "currency": "EUR",
        "alias1": "EQUITYEUR68",
        "formula": "RTHST([CODE],[LABEL],[CURRENCY],[HISTORY],'116',[OTHER])",
        "country": "USA",
        "sector": "Materials",
        "history": "EQUITYEUR68",
    },
    {
        "code": "<EQUITYEUR70>",
        "label": "Equity EUR n. 70",
        "type": "EQT",
        "currency": "EUR",
        "alias1": "EQUITYEUR70",
        "formula": "RTHST([CODE],[LABEL],[CURRENCY],[HISTORY],'116',[OTHER])",
        "country": "USA",
        "sector": "Information Technology",
        "history": "EQUITYEUR70",
    },
]

# Or import assets from csv
# assets = qcs.import_assets_from_csv("examples/assets.csv", sep=",")

histories = [
    {
        "code": "EQUITYEUR68",
        "label": "EQUITYEUR68",
        "currency": "EUR",
        "time_series": [
            {"date": "2021-09-27", "value": 800.1},
            {"date": "2021-09-28", "value": 802.1},
            {"date": "2021-09-29", "value": 780.56},
        ],
    },
    {
        "code": "EQUITYEUR70",
        "label": "EQUITYEUR70",
        "currency": "EUR",
        "time_series": [
            {"date": "2021-09-27", "value": 45.26},
            {"date": "2021-09-28", "value": 48.22},
            {"date": "2021-09-29", "value": 47.11},
        ],
    },
]

# Or import histories from csv
# histories = qcs.import_histories_from_csv(
#     "examples/histories.csv", sep=",", date_format="%Y-%m-%d"
# )

context = Context(
    date="2023-10-19",
    horizon="1d",
    local_db={"assets": assets, "histories": histories},
)

pivot_fields = ["sector", "ticker"]
fields = ["MAV", "STD", "STD%", "\\\\div(STD,MAV)", "STD.BM%"]

pivot_view = snapshot.pivot_view(context, pivot_fields)
df_risks = pivot_view.get_risks(fields)

print("Full table:")
print(df_risks)

print('\nOnly "sector" drilldown level:')
print(
    df_risks[
        (df_risks.index.get_level_values(0) != "")
        & (df_risks.index.get_level_values(1) == "")
    ]
)

print('\nOnly "ticker" drilldown level where "sector" == "Materials":')
print(
    df_risks[
        (df_risks.index.get_level_values(0) == "Materials")
        & (df_risks.index.get_level_values(1) != "")
    ]
)