The tendency of investors to choose investments with maximum return and minimal risk causes the need for diversification in a portfolio to form an optimal portfolio. A lot of research on stock portfolio optimization has been conducted extensively, but not many have tried to apply machine learning concepts such as clustering analysis to accelerate the establishment of a model that can have a positive effect on the time and cost efficiency of portfolio management. However, clustering is only limited to determining the optimal stock candidate, so it is necessary to add another optimization model to calculate the portfolio weight. Based on these problems, this study carried out portfolio optimization using Mean-Variance (MV) and Mean Absolute Deviation (MAD) model based on K-Medoids Clustering by Dynamic Time Warping approach using Monte Carlo-Expected Tail Loss for risk analysis. Based on the analysis results, the MAD portfolio is more optimal than the MV portfolio by the MAD portfolio consists of five stocks, namely BMRI shares with a weight of 0.06243, UNTR shares of 0.08658, BBRI shares of 0.10285, BBCA of 0.53623, and KLBF shares of 0.21191 are the best optimal portfolios. The optimal portfolio of the MAD model has a rate of return of 87.836% in May 2017 - December 2022 with a portfolio performance of 0.03704, while the resulting risk level based on Carlo-Expected Tail Loss is 2.2416%.