The success of the mission assigned to a Wireless Sensor Network (WSN) depends heavily on the cooperation between the nodes of this network. Indeed, given the vulnerability of wireless sensor networks to attack, some entities may engage in malicious behavior aimed at undermining the proper functioning of the network. As a result, the selection of reliable nodes for task execution becomes a necessity for the network. To improve the cooperation and security of wireless sensor networks, the use of Trust Management Systems (TMS) is increasingly recommended due to their low resource consumption. The various existing trust management systems differ in their methods of estimating trust value. The existing ones are very rigid and not very accurate. In this paper, we propose a robust and accurate method (RATES) to compute direct and indirect trust between the network nodes. In RATES model, to compute the direct trust, we improve the Bayesian formula by applying the chaining of trust values, a local reward, a local penalty and a flexible global penalty based on the variation of successful interactions, failures and misbehaviors frequency. RATES thus manages to obtain a direct trust value that is accurate and representative of the node behavior in the network. In addition, we introduce the establishment of a simple confidence interval to filter out biased recommendations sent by malicious nodes to disrupt the estimation of a node's indirect trust. Mathematical theoretical analysis and evaluation of the simulation results show the best performance of our approach for detecting on-off attacks, bad-mouthing attacks and persistent attacks compared to the other existing approaches.