Forecasting stock prices is an extremely challenging job considering the high volatility and the number of variables that influence it (political, economical, social, etc.). Predicting the closing price provides useful information and helps the investor to make the right decision. The use of deep learning and more precisely the recurrent neural networks RNNs in stock market forecasting is an increasingly common practice in the literature. The Long Short Term Memory LSTM and Gated Recurrent Unit GRU architectures are among the most widely used types of RNN networks, given their suitability for sequential data. In this paper, we propose a trading strategy designed for the Moroccan stock market, based on two deep learning model: Long Short Term Memory LSTM and Gated Recurrent Unit GRU to predict respectively close price for short and mid term horizon. Decision rules for buying and selling stocks are implemented based on the forecasting given by the two models, then over four three-years periods, we simulate transactions using these decision rules with different parameters for each stock. We only hold stocks that ensure a return greater than a benchmark return over the four periods. Random search is then used to choose one of the available parameters and the performance of the portfolio built from the selected stocks will be tested over a further period. The repetition of this process with a variation of benchmark return makes it possible to select the best possible combination of stock each with the parameters optimized for the decision rules. The proposed strategy produces very promising results and outperform the performance of indices used as benchmarks in the local market.