/** * {inheritDoc} */ public function supports(MarketDepthPriceInterface $requestedDepth) { // need to match BID -> ASK and ASK -> BID to be satisfied if ($requestedDepth->getType() === $this->type) { $errorMessage = "Cannot check if market depth price of type '%s' supports market depth price of type '%s' (only %s orders can satisfy %s orders)"; throw new InvalidArgumentException(sprintf($errorMessage, $requestedDepth->getType(), $this->type, CurrencyExchangeOperationInterface::TYPE_BID, CurrencyExchangeOperationInterface::TYPE_ASK)); } // must be equal or inverse if (!$this->equals($requestedDepth) && !$this->isInverse($requestedDepth)) { $errorMessage = "Cannot check if market depth price is supported: fromCurrency and toCurrency must match."; throw new InvalidArgumentException($errorMessage); } $thisAmountCurrency = $this->depth->getCurrency(); $requestedAmountCurrency = $requestedDepth->getDepth()->getCurrency(); $requestedAmount = $requestedDepth->getDepth(); if (!$thisAmountCurrency->equals($requestedAmountCurrency)) { $requestedAmount = $requestedDepth->convert($requestedAmount); } if (CurrencyExchangeOperationInterface::TYPE_ASK === $this->type) { // request is BID (to buy), so they must outbid or match our asking price if ($requestedDepth->getMultiplier() >= $this->multiplier && $requestedAmount->isLessOrEqual($this->depth)) { return true; } return false; } // CurrencyExchangeOperationInterface::TYPE_BID === $this->type // request is ASK (to sell), so their selling price must be less or matching our buy price if ($requestedDepth->getMultiplier() <= $this->multiplier && $this->depth->isLessOrEqual($requestedAmount)) { return true; } return false; }
/** * {inheritDoc} */ public function getSupported(MarketDepthPriceInterface $marketDepthPrice) { // need to look up opposite (e.g. match BIDS to ASKS and visa versus) $typeKey = CurrencyExchangeOperationInterface::TYPE_ASK === $marketDepthPrice->getType() ? 'bids' : 'asks'; return array_filter($this->{$typeKey}, function ($marketDepthPriceElement) use($marketDepthPrice) { /** * @var $marketDepthPriceElement \Matmar10\Money\CurrencyExchange\Entity\MarketDepthPriceInterface */ return $marketDepthPriceElement->supports($marketDepthPrice); }); }