Why are u calculating outside the while loop the CCI and modt of the other valus? Usually u want to calculate most of that falus for each asset, not once for all...
I guess that the logic is wrong, not the coding