Refactor the trading algorithm