In general, this trading strategy is entirely legal. However, you will have trouble with the law if you decide to act on information about stocks that you have obtained through non-public channels, i.e., confidential information about a company whose stock is being traded. This constitutes insider trading, which is illegal.