Portfolio¶
A portfolio is a collection of financial investments like stocks, bonds, derivatives, commodities, cash, and cash equivalents. Each security can be traded in a specific local currency. The portfolio has a base currency that is required to reflect each FX risk.
We can start using the Portfolio entity by running the following import statement:
Below we have the attributes used in the Portfolio entity
-
workspace: Refers to the workspace which the Portfolio entity will reside. -
name: The name of the portfolio. -
tags: The tags of the portfolio. Used for filtering and searching -
nlv: The NLV for the portfolio. -
base_currency: Which currency will be used as the standard. -
date: The current date of the portfolio. -
securities: The securities of the portfolio. Generally comes in a list format filled with dictionaries inside of typeSecurities.
Convert Portfolio to a Dictionary¶
Most times when working with entities, there might be a point where we need to convert the entity to a dictionary, and to achieve this we simply use the to_dict() method:
port = Portfolio()
type(port)
everysk.sdk.entities.portfolio.base.Portfolio
to_dict = port.to_dict()
type(to_dict)
# dict
Convert Portfolio to a CSV¶
Another useful conversion for our Portfolio entity, is the ability to convert it to a CSV type using the to_csv() method:
For demonstration purposes let's grab some sample data for instantiating our Portfolio entity:
portfolio_data = {
'id': 'port_1234567891011211234567890',
'workspace': 'SampleWorkspace',
'name': 'SamplePortfolio',
'tags': ['tag1', 'tag2'],
'link_uid': 'ABC',
'description': 'Description',
'nlv': 1000.0,
'base_currency': 'USD',
'date': DateTime(2023, 9, 9, 9, 9, 9, 9),
'securities': Securities([{'symbol': 'AAPL'}]),
'version': '1.0',
'created_on': DateTime(2023, 9, 9, 9, 9, 9, 9),
'updated_on': DateTime(2023, 9, 9, 9, 9, 9, 9),
'level': 'v1',
'outstanding_shares': 1000,
'source_hash': 'XYZ',
'status': 'OK',
'portfolio_uid': 'UID123',
'check_securities': False
}
Now let's use the data above in our Portfolio:
port = Portfolio(**portfolio_data)
to_csv = port.to_csv()
type(to_csv)
to_csv
# 'SamplePortfolio,20230909 09:09:09,USD,1000.0,Description,tag1 tag2,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\r\nstatus,id,symbol,quantity,instrument_class,ticker,label,name,isin '
As seen above we have our portfolio entity converted to a CSV format, ready to be used.
Validate the entity¶
Before actually using the entity we may want to validate its arguments using the validate() method, otherwise we might get unwanted behavior when using:
In the case of something going wrong, the method will point out the invalid field. Let's see how that works in practice by setting an invalid value for the securities attribute:
port = Portfolio()
port.securities = 'Invalid'
# FieldValueError: Key securities mus be <class 'everysk.sdk.entities.portfolio.securities.Securities'>.