public function testAddItem()
 {
     $dataSell = array('job_id' => 1, 'account' => array('id' => 1), 'security' => array('id' => 1), 'status' => QueueItem::STATUS_SELL, 'lot' => array('id' => 1), 'quantity' => 10, 'amount' => 1000);
     /** @var QueueItem $queueItem */
     $queueItem = new QueueItem();
     $queueItem->loadFromArray($dataSell);
     $this->rebalancerQueue->addItem($queueItem);
     $this->assertCount(1, $this->rebalancerQueue);
     $this->assertEquals(10, $this->rebalancerQueue->first()->getQuantity());
     $this->assertEquals(1000, $this->rebalancerQueue->first()->getAmount());
     $this->rebalancerQueue->addItem($queueItem);
     $this->assertCount(1, $this->rebalancerQueue);
     $this->assertEquals(20, $this->rebalancerQueue->first()->getQuantity());
     $this->assertEquals(2000, $this->rebalancerQueue->first()->getAmount());
     $dataSell2 = array('job_id' => 1, 'account' => array('id' => 1), 'security' => array('id' => 1), 'status' => QueueItem::STATUS_SELL, 'lot' => array('id' => 3), 'quantity' => 15, 'amount' => 150);
     /** @var QueueItem $queueItem */
     $queueItem = new QueueItem();
     $queueItem->loadFromArray($dataSell2);
     $this->rebalancerQueue->addItem($queueItem);
     $this->assertCount(2, $this->rebalancerQueue);
     $this->assertEquals(15, $this->rebalancerQueue->last()->getQuantity());
     $this->assertEquals(150, $this->rebalancerQueue->last()->getAmount());
 }
Beispiel #2
0
 public function testIsStatusBuy()
 {
     $this->assertTrue($this->queueItem->isStatusBuy());
     $this->queueItem->setStatus(QueueItem::STATUS_SELL);
     $this->assertFalse($this->queueItem->isStatusBuy());
 }
 public function testDelete()
 {
     $securityRepository = new SecurityRepository();
     $security = $securityRepository->findOneBySymbol('VTI');
     $accountRepository = new AccountRepository();
     $account = $accountRepository->findOneByAccountNumber('916985328');
     $subclassRepo = new SubclassRepository();
     $subclass = $subclassRepo->findFirst();
     $riaRepo = new RiaRepository();
     $ria = $riaRepo->findOneBy(array('email' => '*****@*****.**'));
     $jobRepo = new JobRepository();
     $job = $jobRepo->findOneBy(array('user_id' => $ria->getId()));
     $rebalancerActionRepo = new RebalancerActionRepository();
     $rebalancerActions = $rebalancerActionRepo->bindForJob($job);
     $rebalancerAction = $rebalancerActions->first();
     $rebalancerQueue = new QueueItem();
     $rebalancerQueue->setSecurity($security);
     $rebalancerQueue->setAccount($account);
     $rebalancerQueue->setAmount(100.1);
     $rebalancerQueue->setRebalancerActionId($rebalancerAction->getId());
     $rebalancerQueue->setQuantity(10);
     $rebalancerQueue->setStatus(QueueItem::STATUS_BUY);
     $rebalancerQueue->setSubclass($subclass);
     $this->repository->save($rebalancerQueue);
     $last = $this->repository->findById($this->repository->getLastInsertId());
     $this->repository->delete($last);
     $deletedLast = $this->repository->findById($last->getId());
     $this->assertNull($deletedLast);
 }
 /**
  * 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;
 }
 public function delete(QueueItem $item)
 {
     $sql = "DELETE FROM " . self::TABLE_REBALANCER_QUEUE . " WHERE id = :id";
     $this->db->query($sql, array('id' => $item->getId()));
 }