Exemple #1
0
 public function testSetLotId()
 {
     $lot = $this->getMock('Model\\WealthbotRebalancer\\Lot', null);
     $lot->setId(14);
     $this->queueItem->setLot($lot);
     $this->assertEquals(14, $this->queueItem->getLot()->getId());
 }
 /**
  * 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;
 }