This paper aimed to develop a useful Machine Learning (ML) model for detecting companies with lasting competitive advantages (companies’ moats) according to their financial ratios in order to improve the performance of investment portfolios. First, we computed the financial ratios of companies belonging to the S&P 500. Subsequently, we assessed the stocks’ moats according to an evaluation defined between 0 and 5 for each financial ratio. The sum of all the ratios provided a score between 0 and 100 to classify the companies as wide, narrow or null moats. Finally, several ML models were applied for classification to obtain an efficient, faster and less expensive method to select companies with lasting competitive advantages. The main findings are: (1) the model with the highest precision is the Random Forest; and (2) the most important financial ratios for detecting competitive advantages are a long-term debt-to-net income, Depreciation and Amortization (D&A)-to-gross profit, interest expense-to-Earnings Before Interest and Taxes (EBIT), and Earnings Per Share (EPS) trend. This research provides a new combination of ML tools and information that can improve the performance of investment portfolios; to the authors’ knowledge, this has not been done before. The algorithm developed in this paper has a limitation in the calculation of the stocks’ moats since it does not consider its cost, price-to-earnings ratio (PE), or valuation. Due to this limitation, this algorithm does not represent a strategy for short-term or intraday trading.