question

Upvotes
1 0 0 0

Portfolio ID and Guidance on PAM API Python

I am posting on behalf of our client, "Good evening, I would like to check where I can find the 'portfolio ID' of the portfolios I created in PAL for analysis purposes in Python. Additionally, I would appreciate guidance on how to use the PAM API in Python, including instructions from the initial setup. Thank you for your support."

#productpython api
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
15.5k 33 5 10
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvote
19.1k 86 39 63

Hi @Mayeen.Neri

Users can programmatically determine the portfolio ID or use the Portfolio Finder utility to help. That link also refers to the utility to download.

There is also an article around the usage of PAM, which does describe how to programmatically get the Portfolio ID.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

HI Team,

Please see response from our user

" I am still having difficulties accessing the PAM API. Some of the commands indicated in the references you sent did not work. It is still unclear to me if I need to perform any initial setup or import procedures before I can use the PAM API (do I need to install any libraries? run any Python scripts? import any APIs? Or just the Refinitiv data library is enough?). I searched for this information in the developers' community but could not find a precise answer.

If you know specifically how to perform this initial setup to use the PAM API, I would be very grateful.

Furthermore, if you know whether there is a variable to automatically pull portfolio returns, it would be very helpful. I tried running some tests with ‘TR.PriceClose’, but it doesn't work for portfolios, and ‘TR.PortfolioReturn’ doesn't exist, and ‘performance = pam.analytics.get_performance_attribution’ is not working, as I still could not access the PAM API features. "


Kindly advise. Thanks.

Hi @MarkJoseph.Canada ,

It’s unclear what the user has tried or whether they even downloaded the source code from the article I referenced above - it doesn’t appear so. Also, they did not mention if they attempted to use the utility to locate a portfolio ID. The article outlines a PAM package built specifically for the article, but is also quite useful for other projects - the user would have to include this package alongside their own source code.

Upvotes
1 0 0 0

Hi everyone, thanks for the responses. I have read the referenced articles, but I am still struggling to make progress.

When I run the following command:

# The portfolio search function defines the mechanism to pull down the required IDs.
help(pam.portfolios.search)

I get the following output:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[10], line 2
      1 # The portfolio search function defines the mechanism to pull down the required IDs. 
----> 2 help(pam.portfolios.search)

NameError: name 'pam' is not defined

In another test, when I try to run the first command referenced in the "Portfolio Finder" article, I get the following:

Input:

# The library responsible for accessing endpoints and services within LSEG
import refinitiv.data as rd

# The convenient package included within this source code example
import pam
from pam.portfolios import *
from pam.analytics import *

# Data management and presentation
import pandas as pd
import plotly.express as px

# Progress presentation for long-running tasks
from tqdm import tqdm

rd.__version__

Output:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 9
      5 # The convenient package included within this source code example
      7 import pam
----> 9 from pam.portfolios import *
     11 from pam.analytics import *
     15 # Data management and presentation

ModuleNotFoundError: No module named 'pam.portfolios'

Since Python is not recognizing the pam module and throws a "module not found" error, I suspect I may have missed an installation or configuration step that I should have done to use the PAM module. Could someone point out where I might be going wrong? Thanks again!

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hi @Leonardo Strambi

Instead of trying to incorporate the code outlined within the article, I would suggest you download the entire source from Git that includes the 'pam' directory/package and get that working. The notebook associated with the article includes everything and should work if you download and run. If you find success, that should help you organize your project.

Hi @Leonardo Strambi

As for the 'Portfolio Finder', you don't need to necessarily build and run the source code. You can download the binary from our developer portal. The link to the utility is above in my original answer.

Upvotes
1 0 0 0

Hi @nick.zincone

Thank you for your response. I tried following your instructions, but I'm still encountering issues. I tested running the first code block in the dailyreport.ipynb file, and I still get the following error output:

---------------------------------------------------------------------------

ModuleNotFoundError                       Traceback (most recent call last)

Cell In[1], line 6

      4 # The convenient package included within this source code example

      5 import pam

----> 6 from pam.portfolios import *

      7 from pam.analytics import *

      9 # Data management and presentation



ModuleNotFoundError: No module named 'pam.portfolios'

Moreover, it seems I'm configuring the access .json file incorrectly because when I try to run the rd.open_session() command, I get this error output:

[Error 401] - {'error': 'invalid_client', 'error_description': 'Invalid Application Credential.'}
<refinitiv.data.session.Definition object at 0x2264d04fef0 {name='dp'}>

In my other projects, I only use the 'app-key,' (without username and password) but with the sessions set to:

"sessions": { "default": "desktop.workspace" }

instead of platform.dp. I suspect this might be causing the issue. Apologies if this is a simple question, but would you happen to have a step-by-step guide to adjust the .json file appropriately (if you agree that this could be the root of the problem)? Thanks for your support!

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
19.1k 86 39 63

Hi @Leonardo Strambi,

I just cloned the Git repository within my /tmp directory on my laptop. I then started Jupyter lab and navigated to the directory containing the main notebook to run:
1729540096205.png

On the left, you can see the 'pam' directory and on the right, after I executed the code cell with all the imports, you can see no issues with importing. I can't tell if you have correctly cloned the repo such that it contains the package 'pam' as I have above. It's worth noting I'm running the DailyReport.ipnyb from the root directory and should not complain about "No module named 'pam.portfolios'".

As for the configuration, you will need to modify the 'refinitiv-data.config.json' file within the root directory. The configuration file presently defaults the session to a "platform" session - you will need to change it from:

"default": "platform.dp" ==> "desktop.codebook"

In my case, my platform ID was permissioned for access. If your desktop (LSEG Workspace) ID is permissioned, you will need to ensure Workspace is running and you change the above configuration and ensure you have an app-key defined here:

1729540688592.png


1729540096205.png (104.8 KiB)
1729540688592.png (99.3 KiB)
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
1 0 0 0

@nick.zincone

Thank you for the response. I was previously trying to run the code using VS Code, which is the platform I use on a daily basis.

Now that I’ve tried through Codebook, directly in Workspace, I was able to make some progress. I managed to run the command help(pam.portfolios.search) and get the correct output.

However, when attempting to run the command pam.portfolios.search(), I’m still receiving an error message. See the output below:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_185/1899528777.py in <module>
      1 # Pull down all user-defined portfolios.
      2 # The 'portfolioTypes' specifier is conveniently defaulted to all user-defined types
----> 3 pam.portfolios.search()


~/Untitled Folder/pam/portfolios/search.py in search(portfolioTypes, query, queryField, queryCondition, userSources, sort, maxCount, includeDefaultBenchmarkHeader)
     67 
     68     except Exception as e:
---> 69         raise RuntimeError(f"An error occurred: {str(e)}") from None
     70 
     71 
RuntimeError: An error occurred: HTTP Error. Code: 403. Reason: Forbidden
[{"error":{"id":"83b70ab0-90a5-4d29-aa75-11d9e614e1f3","code":"insufficient_scope","message":"access denied. Scopes required to access the resource: [trapi.userdata.portfolio-management.read]. Missing scopes: [trapi.userdata.portfolio-management.read]","status":"Forbidden"}}]


Could this be a limitation of my subscription?

I also tried to proceed with the following code as well and received the following output for this command:

# Example 1 - generate measures for our user-defined portfolio
# The date is based on the portfolio we created

df = generate_data("Selecao RF IPCA", "2020-07-31", "2023-10-31")


--------------------------------------------------------------------------- RuntimeError                              Traceback (most recent call last) /tmp/ipykernel_185/3405814232.py in <module>
1 # Example 1 - generate measures for our user-defined portfolio      
2 #             The date is based on the portfolio we created
----> 3 df = generate_data("Selecao RF IPCA", "2020/07/31", "2023/10/31") 

/tmp/ipykernel_185/3848221774.py in generate_data(ids, start_date, end_date)     
20      
21     # Step 1 - retrieve the portfolio labels/names
---> 22     portfolios = pam.portfolios.get_portfolios(ids)     
23      
24     # Step 2 - retrieve daily statistics 

~/Untitled Folder/pam/portfolios/portfolios.py in get_portfolios(ids, startDate, endDate, includePortfolioLevelAttributes, includeDefaultBenchmarkHeader, includeCarveOutBasePortfolioHeader, traverseCompositePositions)    
144     
145     except Exception as e: 
--> 146         raise RuntimeError(f"An error occurred: {str(e)}") from None    
147     
148  

RuntimeError: An error occurred: HTTP Error. Code: 403. Reason: Forbidden [{"error":{"id":"b217db98-29c5-48e8-94a9-7f4031a61888","code":"insufficient_scope","message":"access denied. Scopes required to access the resource: [trapi.userdata.portfolio-management.read]. Missing scopes: [trapi.userdata.portfolio-management.read]","status":"Forbidden"}}]


Also, Assuming this is not an issue related to my subscription access, would it be possible to run the code in VS Code, or only in Codebook?

Thank you very much for all your help!


icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hi @Leonardo Strambi

The “insufficient scope” indicates your desktop ID was not granted permissions to access the PAM APIs - you would have to contact your account manager to determine proper access for this service. As for VS Code, I have not tried but the key is to ensure the main notebook can find the ‘Pam’ subdirectory - you should have success if your working directory is where the Pam folder lives.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.