getStocks(); $stockCount = count($stocks); $step = 0.01; $maxIterations = ($stockCount - $step) / $step; for ($i = 0; $i <= $maxIterations; $i++) { // 3 possible values: +$step -$step and 0 for ($t = 0; $t <= 17; $t++) { $depots[$t] = clone $depot; /** * build variants with depots */ /** @var Stock $stock */ foreach ($depots[$t]->getStocks() as $stock) { $stock->setAmount(); } /** * get current yield/volatility */ $currentYield = $this->getDepotExpectedYield($depot); $currentVola = $this->getDepotStandardDeviation($depot); /** * get the depot with best yield/volatility ratio */ foreach($depots as $key => $d){ $yield = $this->getDepotExpectedYield($d); $vola = $this->getDepotStandardDeviation($d); if($vola <= $currentVola && $yield >= $currentYield){ // better match found. change $depot to new one. $depot = $d; } } } } }