예제 #1
0
 public function testSetPrice()
 {
     $this->security->setPrice(200.7);
     $this->assertEquals(200.7, $this->security->getPrice());
 }
예제 #2
0
 /**
  * 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;
 }