Exemple #1
0
 public function testSell()
 {
     $this->lot->setQuantity(2);
     $this->assertFalse($this->lot->sell(3));
     $this->lot->setQuantity(10);
     $this->assertTrue($this->lot->sell(5));
     $this->assertEquals(5, $this->lot->getQuantity());
     $this->assertEquals(1500, $this->lot->getAmount());
 }
 /**
  * Sell lot
  *
  * @param Lot $lot
  * @param Security $security
  * @param Account $account
  * @param $amount
  * @param bool $dryRun
  * @return float
  * @throws \Exception
  */
 public function sellLot(Lot $lot, Security $security, Account $account, $amount, $dryRun = false)
 {
     if ($lot->getAmount() < $amount) {
         $sellQuantity = $lot->getQuantity();
         $sellAmount = $lot->getAmount();
     } else {
         $sellQuantity = ceil($amount / $lot->calcPrice());
         $sellAmount = $sellQuantity * $lot->calcPrice();
     }
     if (!$security->isCanBeSold($sellQuantity, $sellAmount)) {
         $exception = new \Exception(sprintf('Selling security error: cannot sell security with id: %s, qty: %s, amount: %s. %s|%s', $security->getId(), $sellQuantity, $sellAmount, $security->getQty(), $security->getAmount()));
         $this->logger->logError($exception);
         throw $exception;
     }
     if (!$this->checkTransaction($account, $amount, $security, 'sell')) {
         $exception = new \Exception("Cannot sell: {$security->getId()} . Transaction check fails. (Amount:{$amount})");
         $this->logger->logError($exception);
         throw $exception;
     }
     if (!$dryRun) {
         $security->sell($sellQuantity, $sellAmount);
         $queueItem = new QueueItem();
         $queueItem->setRebalancerActionId($this->getRebalancerAction()->getId());
         $queueItem->setSecurity($security);
         $queueItem->setAccount($account);
         $queueItem->setLot($lot);
         $queueItem->setQuantity($sellQuantity);
         $queueItem->setAmount($sellAmount);
         $queueItem->setStatus(QueueItem::STATUS_SELL);
         $this->getRebalancerQueue()->addItem($queueItem);
         $lot->sell($sellQuantity);
     }
     return $sellAmount;
 }