Objective: based on a systematic approach using machine learning , this research aims to propose a model of selection and allocation of assets that allows for building profitable and safe portfolios, even in times of insecurity and low predictability. Methodology: we used the machine learning algorithm called random forest to associate the independent variables with a dependent one and learn the probability of positive returns in the month following the data collection. According to the probabilities, the stocks were allocated into long , short, or non-allocated portfolios . Finally, we allocated a share of gold, which is a protection asset much used in times of crisis and uncertainty. Results and contributions: the study reached its goal and demonstrated being possible to build profitable and safe investment portfolios, even in times of greater uncertainty and volatility, as in 2020 due to the Covid-19 pandemic. We found that the model is effective in moments of crisis and also of greater predictability, as in the period from 2016 to 2019 when the stock exchange has an uptrend. Relevance: the relevance of this study points to an unprecedented historical context in Brazil, where uncertainties regarding both the local and world economy have demanded advanced studies of prediction to minimize risks and contribute to results for investors. In addition, we highlight that following a short period of low Selic (2019 to 2021), the Central Bank increased the rate again, raising the interest more in profitable and safer assets than the investment in stocks. Impact on the area: the study has a positive impact on the finance area since studies in this field promote greater stability to investors and thus better capital flow for companies, which, in turn, contribute to society and the growth of the country.