ABSTRACT The significance of the environmental, social, and governance (ESG) factors has risen substantially among investors in recent years. Similarly, machine learning techniques have allowed for improvements in the prediction of stock prices. Our research combines ESG factors and prediction-based returns using recurrent neural networks to create profitable-sustainable portfolios that can consistently beat the market index in return and ESG scores. Our analysis focuses on the components of the EURO STOXX 50Ⓡ Index during the year 2021 and the first half of 2022, allowing us to analyze two different market scenarios, continuous growth and bear market, respectively. This paper provides empirical evidence that combining machine learning with ESG scores and its application in portfolio optimization can achieve higher returns and higher ESG performance depending on the macroeconomic context and presents the trade-off between the Sharpe Ratio and the ESG score of the optimized portfolios for different scenarios.