class pyrtfolio.StockPortfolio.StockPortfolio

StockPortfolio is the main class of pyrtfolio which is going to manage all the introduced stocks.

This class is the one that contains the stocks information and the one that will be used by the user in order to generate a custom portfolio. So on, this function implements the methods to calculate all the values required in a basic portfolio and, as already mentioned, the method to add stocks.


this list contains all the introduced stocks, which will later be used to generate the portfolio.

Type:list, protected

this list contains all the introduced Stock objects, in order to be able to refresh them.

Type:list, protected

it is the generated portfolio, once the addition of every stock is validated.


This is the init method of StockPortfolio class which is launched every time the user instances it.

This method is the init method of this class, StockPortfolio, and its main function is to init all the attributes contained in it. Every time this class is instanced, the attributes values are restored and, so on, the portfolio’s data is lost if existing for that instance. This class does not take any parameters since they are filled once the class is instanced.


list of weak references to the object (if defined)

add_stock(stock_symbol, stock_country, purchase_date, num_of_shares, cost_per_share)

Method to add a stock to the portfolio.

This method adds a stock to the custom portfolio data. Some parameters need to be specified for the introduced stock such as the purchase date of the shares, the number of shares bought and the price payed (cost) per every share. From this data, the portfolio will be created and the specified calculations will be done, so to give the user an overview of his/her own portfolio.

  • stock_symbol (str) – symbol of the Stock that is going to be added to the StockPortfolio.
  • stock_country (str) – country from where the specified stock_symbol is, so to validate it.
  • purchase_date (str) – date when the shares of the introduced stock were bought, formatted as dd/mm/yyyy.
  • num_of_shares (int) – amount of shares bought of the specified Stock in the specified date.
  • cost_per_share (float) – price of every share of the Stock in the specified date.
static calculate_gross_current_value(current_price, num_of_shares)

This method calculates the gross current value which is the total current value of the shares bought, which is the result of the multiplication of the current price with the number of bought shares.

static calculate_net_current_value(gross_current_value, total_dividends)

This method calculates the net current value which is the total of the gross current value and the value of the dividends, i.e., the total of returns from both capital gains and dividends.

static calculate_purchase_cost(cost_per_share, num_of_shares)

This method calculates the purchase cost, which is the paid value for every stock share hold by the user at the time that stock shares were bought.

static calculate_total_dividends(dividends, num_of_shares)

This method calculates the total dividend’s value which is the sum of all the dividends paid in the time range that the user owned/owns the stock shares. Each dividend is paid per the number of stock shares hold by the user.

static calculate_total_gain_loss(net_current_value, purchase_cost)

This method calculates the difference between the current value of the stock shares, including the dividends received, with the value that the user paid of them.

static calculate_total_gain_loss_percentage(total_gain_loss, purchase_cost)

This method compares the stock total gain loss to what the user paid for the stock shares owned and the result is a percentage that can show the total stock performance.

static get_current_price(data)

This method gets the current price value of the introduced stock, which is the last close value indexed in the pandas.DataFrame.


Method to refresh/reload the StockPortfolio information.

This method is used to refresh or reload the StockPortfolio information since the values may have changed since the last time the portfolio was generated. So on, this function will return to get the information from every Stock listed in the StockPortfolio.