public function testLoadFromArray() { $this->assertEquals(2, $this->security->getId()); $this->assertEquals('Vanguard Total Stock Market ETF', $this->security->getName()); $this->assertEquals('VTI', $this->security->getSymbol()); $this->assertEquals(13, $this->security->getSubclass()->getId()); $this->assertEquals(15, $this->security->getAssetClass()->getId()); $this->assertTrue($this->security->getIsPreferredBuy()); $this->assertEquals(20.5, $this->security->getPrice()); $this->assertEquals(10, $this->security->getQty()); $this->assertEquals(205.3, $this->security->getAmount()); }
/** * @param Portfolio $portfolio * @param Security $security * @return SecurityTransaction */ public function findOneByPortfolioAndSecurity(Portfolio $portfolio, Security $security) { $sql = "\n SELECT st.* FROM {$this->table} st\n LEFT JOIN " . self::TABLE_SECURITY_ASSIGNMENT . " sa ON st.security_assignment_id = sa.id\n INNER JOIN ce_models cem ON (cem.parent_id = sa.model_id AND cem.id = :model_id)\n WHERE sa.security_id = :security_id\n "; $params = array('security_id' => $security->getId(), 'model_id' => $portfolio->getId()); $result = $this->db->queryOne($sql, $params); if (!$result) { return null; } return $this->bindObject($result); }
/** * 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 getPositionsByPortfolio(Portfolio $portfolio, Account $account = null, Security $security = null) { $sql = "SELECT pos.client_system_account_id, sa.security_id as security_id, sm.id as muni_id\n FROM " . self::TABLE_SUBCLASS . " subc\n INNER JOIN " . self::TABLE_CE_MODEL_ENTITY . " ceme ON ceme.subclass_id = subc.id\n INNER JOIN " . self::TABLE_SECURITY_ASSIGNMENT . " sa ON sa.id = ceme.security_assignment_id\n INNER JOIN " . self::TABLE_POSITION . " pos ON pos.security_id = sa.security_id\n LEFT JOIN " . self::TABLE_SECURITY_ASSIGNMENT . " sam ON sam.id = ceme.muni_substitution_id\n LEFT JOIN " . self::TABLE_SECURITY . " sm ON sam.security_id = sm.id\n WHERE ceme.model_id = :portfolioId\n "; $parameters = array('portfolioId' => $portfolio->getId()); if (null !== $account) { $sql .= " AND pos.client_system_account_id = :accountId"; $parameters['accountId'] = $account->getId(); } if (null !== $security) { $sql .= " AND (pos.security_id = :securityId OR sm.id = :securityId)"; $parameters['securityId'] = $security->getId(); } $sql .= " GROUP BY pos.client_system_account_id, pos.security_id"; return $this->db->query($sql, $parameters); }