This tutorial uses a pre-populated portfolio (accessible to all internal users) and walks through pulling performance and factor risk data (for demo, not client distribution, purposes).
import datetime as dt
from gs_quant.markets.portfolio_manager import PortfolioManager
from gs_quant.markets.report import PerformanceReport, FactorRiskReport
from gs_quant.session import GsSession, Environment
GsSession.use(Environment.PROD)
The PortfolioManager
class allows for easy retrieval and scheduling of portfolio reports. Simply running:
all_reports = PortfolioManager('MPZV9A0F1EMQGG79').get_reports()
will return a list of Report
objects that represent the reports associated with the portfolio.
The GS Quant Report
class is inherited by report subclasses, like FactorRiskReport
and PerformanceReport
, each of which corresponds to a type of Marquee report. Each subclass then has additional functions specific to its report type. In this case, we'd like to find the PerformanceReport
associated with this portfolio, and leverage its functions to retrieve data. In this example, we will pull all historical PnL, gross exposure, and net exposure available.
performance_report = list(filter(lambda report: isinstance(report, PerformanceReport), all_reports))[0]
pnl = performance_report.get_pnl(start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))
gross_exposure = performance_report.get_gross_exposure(start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))
net_exposure = performance_report.get_net_exposure(start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))
print(f'PnL: \n{pnl.__str__()}')
print(f'Gross Exposure: \n{gross_exposure.__str__()}')
print(f'Net Exposure: \n{net_exposure.__str__()}')
Now let's find a factor risk report and pull its correlating FactorRiskReport
.
risk_model_id = 'BARRA_USSLOWL'
factor_risk_reports = list(filter(lambda report: isinstance(report, FactorRiskReport) and report.get_risk_model_id() == risk_model_id, all_reports))
if len(factor_risk_reports) == 0:
print(f'This portfolio does not have a factor risk report with the risk model {risk_model_id}. Please create a new factor risk report and schedule it before proceeding.')
factor_risk_report = factor_risk_reports[0]
print(f'Factor risk model found with ID: "{factor_risk_report.id}".')
Now that we have our factor risk report, we can leverage the functionality of the FactorRiskReport
class to pull attribution and risk data. In this example, let's pull historical data on factor, specific, and total PnL for the first five months of 2021:
factor_pnl = factor_risk_report.get_factor_pnl(factor_name='Factor', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))
specific_pnl = factor_risk_report.get_factor_pnl(factor_name='Specific', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))
total_pnl = factor_risk_report.get_factor_pnl(factor_name='Total', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))
print(f'Factor: \n{factor_pnl.__str__()}')
print(f'Specific: \n{specific_pnl.__str__()}')
print(f'Total: \n{total_pnl.__str__()}')
Now let's pull the breakdown of proportion of risk among the different factor types over time:
market_prop_of_risk = factor_risk_report.get_factor_proportion_of_risk(factor_name='Market', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))
style_prop_of_risk = factor_risk_report.get_factor_proportion_of_risk(factor_name='Style', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))
industry_prop_of_risk = factor_risk_report.get_factor_proportion_of_risk(factor_name='Industry', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))
country_prop_of_risk = factor_risk_report.get_factor_proportion_of_risk(factor_name='Country', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))
print(f'Market: \n{market_prop_of_risk.__str__()}')
print(f'Style: \n{style_prop_of_risk.__str__()}')
print(f'Industry: \n{industry_prop_of_risk.__str__()}')
print(f'Country: \n{country_prop_of_risk.__str__()}')