Big Caps Optimization Using Python in IDX

Photo by Lesley Juarez via Unsplash

Did you ever go to local market? Get some fruits and veggies? Comparing price of same products one to another ? Did you ever observe what sellers do ? If second to last questions were no, maybe you got too dizzy by how buzzing the market was or you were busy remembering what to buy.

That’s a simple overview of stock market. Various stocks are available and millions of transaction occurred in second, it can be buzzing sometimes. You are allowed to purchased anything, however, not every stock will be picked. It can be your budget isn’t sufficient enough to take everything on table or stocks aren’t worth enough.

For this reason, the importance of optimization in determining items are needed. By optimizing, we are ensure to get the best proportional allocation given to the limited fund. Modern Portfolio Theory helps you in determining the composition of ‘groceries’.

Modern Portfolio Theory was first introduced by Harry Markowitz in 1952. The idea behind is maximizing expected return at given level of risk which expressed in form of standard deviation. By combining individual asset to portfolio helps to reduce risk and increasing expected return. In 1990, Harry Markowitz was awarded for his findings along with Merton H. Miller and William F. Sharpe.

This article will give you an understanding of optimized portfolio at maximum level of Sharpe ratio. Sharpe ratio is used to help investors understand the return of an investment compared to its risk.

So, how to use the theory? Let’s get started.


This material has been prepared for general informational purposes only and is not intended to be relied upon as professional advice. The information is being presented without consideration of the investment objectives, risk tolerance or financial circumstances .

1. General Information (1/2)

According to Bisnis, top five big caps equally worth IDR 1885.21 Trillion in Q3 2020, decreased from 2438.72 Trillion in Q4 2019.

  • Stocks are BBCA, BBRI, UNVR, TLKM, BMRI.
  • Data are taken from Yahoo.
  • ‘Adjusted Price’ will be used instead of ‘Price’.
  • Start date of observation at end of Q3 2010.
  • End date of observation at end of Q3 2020.

2. Import Library & Preview

Fig-1 | Import Library
Fig-2 | Head of Data Frame

The reason behind why numbers are seemed pretty misfit is because it’s ‘adjusted close’ instead of ‘close’. Adjusted price reflects as if stocks were influenced by any corporate actions in past time. Please don’t get confused for tickers name, it’s just suffix added as data taken from Yahoo.

3. Covariance Matrix

Fig-3 | Covariance Matrix

Covariance measures the degree in which assets move together through scenarios or through time. In addition, I didn’t annualized data at this point but later on in a function.

4. General Information (2/2)

Fig-4 | General Information
  • Logged returns for every assets to get normal distribution.
  • Indonesia 10-Year Bond Yield as standard base with slight higher estimation for risk free.
  • Generated 1 million of possible portfolio with various random weight.

5. Results

Fig-5 | Annualized Return and Allocation
  • Our annual portfolio return based on 10-years historical data is 0.19
  • Given stock allocation is quite centralized
Fig-6 | Portfolio Efficient Frontier
  • Any return above the frontier is impossible.
  • Red cross is tangency of capital market line (CML) and portfolio.
  • Coloured dots are made of num_portfolio variable we made.


  • Variance (or its square root, the standard deviation) is the most frequently used quantitative measure of risk.
  • Variance can be used two-dimensional either backward looking(measuring dispersion of historical mean) or forward looking (measuring dispersion of expected mean).
  • Diversification helps to eliminate idiosyncratic risk or risk of uniqueness of the asset.


  • Theory says assets correlation should be static over time. However, in 2008 when crisis occurred most of asset classes correlation went rocketing to 1.
  • Historical data is considerably taken which unfortunately has no guarantee of performances in the future.
  • High sensitivity to variance.


  • Markowitz, H.M. (March 1952). “Portfolio Selection”. The Journal of Finance. 7 (1): 77–91. doi:10.2307/2975974. JSTOR 2975974.
  • Rice, B. (2017). The upside of the downside of modern portfolio theory. Investment & Wealth Monitor, Investment Management Consultants Association, 13–18.
  • Wijst, D. V. (2013). Finance: A quantitative introduction. Cambridge: Cambridge University Press.




A Data Science learner || Jakarta, ID

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Day 121 of #NLP365: NLP Papers Summary — Concept Pointer Network for Abstractive Summarization

My Journey towards Data-Driven Organization (DDO)

Steps In Conducting A Case Study

Weekly Digest for Data Science and AI: Python and R (Volume 1)

Predictive Analytics Models

What is Machine Learning and How does it Work?

Extract Federal Fund Rate Data from FRED for OLS Regression Analysis: A Complete Tutorial

What is Predictive Analytics?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


A Data Science learner || Jakarta, ID

More from Medium

Python OOPS Concepts(Types Of Variables)

How to Pull Stock Data into your Python ProgramHow to Pull Stock Data into your Python Program

How to Pull Stock Data into your Python Program

EV Stock Daily Cumulative Returns using Python Pandas

Plot and Compare the Stock Price Trend