public function testIsCanBePurchased() { $this->assertFalse($this->security->isCanBePurchased(false, false)); $this->assertFalse($this->security->isCanBePurchased(false, 1)); $this->assertFalse($this->security->isCanBePurchased(1, false)); $this->assertFalse($this->security->isCanBePurchased(-1, 1)); $this->assertFalse($this->security->isCanBePurchased(1, -1)); $this->assertFalse($this->security->isCanBePurchased(-1, -1)); $this->assertTrue($this->security->isCanBePurchased(1, 1)); }
/** * Buy security * * @param Security $security * @param Account $account * @param $amount * @param bool $dryRun * @return float|int * @throws \Exception */ public function buySecurity(Security $security, Account $account, $amount, $dryRun = false) { $price = $security->getPrice(); $buyQuantity = $price > 0 ? floor($amount / $price) : 0; $buyAmount = $buyQuantity * $price; if (!$security->isCanBePurchased($buyQuantity, $buyAmount)) { $exception = new \Exception(sprintf('Buying security error: cannot buy security with id: %s, qty: %s, amount: %s.', $security->getId(), $buyQuantity, $buyAmount)); $this->logger->logError($exception); throw $exception; } if (!$this->checkTransaction($account, $amount, $security, 'buy')) { $exception = new \Exception("Cannot buy: {$security->getId()} . Transaction check fails. (Amount:{$amount})"); $this->logger->logError($exception); throw $exception; } if (!$dryRun) { $this->logger->logInfo("Buy security {$security->getId()} qty: {$buyQuantity}; amount: {$buyAmount}"); $security->buy($buyQuantity, $buyAmount); $queueItem = new QueueItem(); $queueItem->setRebalancerActionId($this->getRebalancerAction()->getId()); $queueItem->setSecurity($security); $queueItem->setAccount($account); $queueItem->setQuantity($buyQuantity); $queueItem->setAmount($buyAmount); $queueItem->setStatus(QueueItem::STATUS_BUY); $this->getRebalancerQueue()->addItem($queueItem); } return $buyAmount; }