#!/usr/bin/env python # coding: utf-8 # # Pull Portfolio Performance Data # # When you create and upload positions to a portfolio in Marquee, GS Quant makes it easy to pull historical performance data with just a few lines of code. # # ## Prerequisites # # Before you begin this tutorial, you must have a portfolio in Marquee with positions and reports that have already been scheduled and completed. If that's not the case, please navigate to our tutorial on [creating a new portfolio](../tutorials/Create%20New%20Portfolio.ipynb) and/or [updating positions and running reports](../tutorials/Update%20Historical%20Portfolio.ipynb). # # Your application also needs to have the **run_analytics** scope. If it doesn't, please request it on your [My Applications Page](https://developer.gs.com/go/apps/view). If you have any other questions please reach out to the [Marquee sales team](mailto:gs-marquee-sales@gs.com). # ## Step 1: Authenticate and Initialize Your Session # # First you will import the necessary modules and add your client id and client secret. # In[4]: from gs_quant.markets.portfolio_manager import PortfolioManager from gs_quant.markets.report import PerformanceReport from gs_quant.session import GsSession, Environment client = 'ENTER CLIENT ID' secret = 'ENTER CLIENT SECRET' GsSession.use(Environment.PROD, client_id=client, client_secret=secret, scopes=('run_analytics',)) # ## Step 2: Get all Portfolio Reports # # The `PortfolioManager` class allows for easy retrieval and scheduling of portfolio reports. Simply running: # In[8]: all_reports = PortfolioManager('ENTER PORTFOLIO ID').get_reports() # will return a list of `Report` objects that represent the reports associated with the portfolio. # # ## Step 3: Find Portfolio Performance Analytics Report and Pull Data # # 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. # # In[9]: performance_report = list(filter(lambda report: isinstance(report, PerformanceReport), all_reports))[0] pnl = performance_report.get_pnl() gross_exposure = performance_report.get_gross_exposure() net_exposure = performance_report.get_net_exposure() print(f'PnL: \n{pnl.__str__()}') print(f'Gross Exposure: \n{gross_exposure.__str__()}') print(f'Net Exposure: \n{net_exposure.__str__()}') # ### Quick Tip! # If you would like to pull multiple performance measures in one dataframe, you can use the `get_many_measures` function: # In[ ]: aggregated_performance_results = performance_report.get_many_measures(measures=['pnl', 'grossExposure', 'netExposure']) print(aggregated_performance_results) # # ### Quick Tip! # If you would only like to pull data for a specific date range, all the functions used above also take in function parameters `start_date` and `end_date`, in which you can pass `datetime.date` objects to specify the range you would like. # # # ### You're all set, Congrats! What's next? # # * [Creating and scheduling a new factor risk report](../examples/marquee/00_create_factor_risk_report.ipynb) # # * [Updating the portfolio with new positions](../tutorials/Update%20Historical%20Portfolio.ipynb) # # * [Retrieving the portfolio's performance analytics](../tutorials/Pull%20Portfolio%20Performance%20Data.ipynb) # # # *Other questions? Reach out to the [Portfolio Analytics team](mailto:gs-marquee-analytics-support@gs.com)!*