Why are you doing all the calculations manually? I imagine doing this using the trader PHP extension which has a lot of indexes already there, look here http://belsky.info/archives/35-Trader-PHP-extension,-use-it.html . The data could be surely ...