Logic to buy and sell stocks