コード例 #1
0
ファイル: Sell.php プロジェクト: junjinZ/wealthbot
 /**
  * @param LotModel $model
  * @return int|null
  */
 public function create(LotModel $model)
 {
     if ($model->isMF()) {
         if (null == ($previewMFLot = $this->lotRepo->findOnePreviewMFLot($model))) {
             // TODO: add error
             return null;
         } else {
             $amount = $previewMFLot->getAmount();
             $amount -= $model->getAmount();
             if ($previewMFLot->getDate() == $model->getDate()) {
                 $model->setAmount($amount);
                 $model->setQuantity($amount);
                 $model->setCostBasis($amount);
                 // Save lot changes
                 $this->lotRepo->update($previewMFLot->getId(), $model);
                 // Return preview MF lot
                 $id = $previewMFLot->getId();
             } else {
                 $model->setStatus(WealthbotLot::LOT_IS_OPEN);
                 $model->setQuantity($amount);
                 $model->setCostBasis($amount);
                 // Save lot
                 $id = $this->lotRepo->save($model);
             }
         }
     } else {
         $initialLots = $this->lotRepo->findAllInitialLots($model);
         $id = $this->compareEqualQuantity($initialLots, $model);
         $id = is_null($id) ? $this->compareRtQuantity($initialLots, $model) : $id;
     }
     $model->isClosed() && $this->lotRepo->updateRebalancerDiff($id, $this->compareRebalancerQueue($model, RebalancerQueueModel::STATUS_SELL));
     return $id;
 }
コード例 #2
0
ファイル: Buy.php プロジェクト: junjinZ/wealthbot
 /**
  * @param LotModel $model
  * @return int
  */
 public function create(LotModel $model)
 {
     if ($model->isMF()) {
         if (null == ($previewMFLot = $this->lotRepo->findOnePreviewMFLot($model))) {
             $model->setQuantity($model->getAmount());
             $model->setCostBasis($model->getAmount());
             // Save lot
             $id = $this->lotRepo->save($model);
         } else {
             $amount = $previewMFLot->getAmount();
             $amount += $model->getAmount();
             if ($previewMFLot->getDate() == $model->getDate()) {
                 $model->setAmount($amount);
                 $model->setQuantity($amount);
                 $model->setCostBasis($amount);
                 // Save lot changes
                 $this->lotRepo->update($previewMFLot->getId(), $model);
                 // Return preview MF lot
                 $id = $previewMFLot->getId();
             } else {
                 $model->setStatus(WealthbotLot::LOT_IS_OPEN);
                 $model->setQuantity($amount);
                 $model->setCostBasis($amount);
                 // Save lot
                 $id = $this->lotRepo->save($model);
             }
         }
     } else {
         // Save lot
         $id = $this->lotRepo->save($model);
     }
     $id && $model->isInitial() && $this->lotRepo->updateRebalancerDiff($id, $this->compareRebalancerQueue($model, RebalancerQueueModel::STATUS_BUY));
     return $id;
 }
コード例 #3
0
ファイル: AbstractLot.php プロジェクト: junjinZ/wealthbot
 /**
  * @param LotModel $model
  * @param int $status
  * @return bool
  */
 protected function compareRebalancerQueue(LotModel $model, $status)
 {
     $amount = $model->getAmount();
     if (!empty($amount)) {
         $parameters['status'] = $status;
         $parameters['quantity'] = $model->getQuantity();
         $parameters['security_id'] = $model->getSecurityId();
         $status == RebalancerQueueModel::STATUS_SELL && ($parameters['lot_id'] = $model->getInitialLotId());
         if ($rebalancerQueue = $this->rebalancerQueueRepo->findOneBy($parameters)) {
             $percent = abs(($model->getAmount() - $rebalancerQueue->getAmount()) / $model->getAmount() * 100);
             return $percent > 10;
         }
     }
     return false;
 }
コード例 #4
0
 /**
  * Set position for lots
  *
  * @param Lot $model
  * @return bool
  */
 public function updatePositionBy(Lot $model)
 {
     return $this->fpdo->update($this->table)->set('position_id', $model->getPositionId())->where('security_id', $model->getSecurityId())->where('date = DATE(?)', $model->getDate())->where('client_system_account_id', $model->getClientSystemAccountId())->execute();
 }
コード例 #5
0
ファイル: Lot.php プロジェクト: junjinZ/wealthbot
 /**
  * @param SystemClientAccountModel $account
  * @param SecurityModel $security
  * @param array $data
  * @return mixed
  */
 public function process(SystemClientAccountModel $account, SecurityModel $security, array $data)
 {
     $model = new LotModel();
     $model->setSymbol($data['symbol']);
     $model->setTransactionCode($data['transaction_code']);
     $model->setInitialLotId(null);
     $model->setQuantity($data['qty']);
     $model->setStatus(WealthbotLot::LOT_INITIAL);
     $model->setDate($data['tx_date']);
     $model->setWasClosed(false);
     $model->setAmount($data['net_amount']);
     $model->setCostBasis($data['gross_amount']);
     $model->setRealizedGain(null);
     $model->setSecurityId($security->getId());
     $model->setClientSystemAccountId($account->getId());
     $model->setWasRebalancerDiff(false);
     $class = Factory::make($model->isBuy() ? 'Buy' : 'Sell');
     // Create new lot
     return $class->create($model);
 }