Esempio n. 1
0
    def __rtruediv__(self, other: Union['Price', float, int]):
        """Divides two PriceType objects together.

        Arguments:
            other: Union[Price, float, int]
                The object that is the left argument of the
                addition operation.

        Raises:
            IncompatibleTradingPairException:
                If (other : Price) and the pairs do not equal each other.
            InvalidNonNumericQuantity:
                If not (other : Union[Price, Number]).
        """
        price = None

        if isinstance(other, float) or isinstance(other, int):
            price = Price(other / self.rate, self.pair.quote / self.pair.base)

        elif isinstance(other, Quantity):
            raise IncompatiblePriceQuantityOperation(self, other)

        elif not isinstance(other, Number):
            raise InvalidNonNumericQuantity(other)

        return price
Esempio n. 2
0
 def __mul__(self, other: Union['Quantity', float, int]) -> 'Quantity':
     if isinstance(other, Price):
         if other.pair.quote != self.instrument:
             raise IncompatiblePriceQuantityOperation(
                 other.pair.quote, self.instrument)
         return Quantity(other.pair.base, other.rate * self.size,
                         self.path_id)
     return Quantity._math_operation(self, other, operator.mul)
Esempio n. 3
0
    def __truediv__(self, other: Union['Quantity', 'Instrument', float, int]) -> Union['Quantity', 'Price']:
        if isinstance(other, Price):
            if other.pair.base != self.instrument:
                raise IncompatiblePriceQuantityOperation(other.pair.quote, self.instrument)
            return Quantity(other.pair.quote, self.size / other.rate, self.path_id)
        elif other.__class__.__name__ == "Instrument":
            return Price(self.size, self.instrument / other)

        elif isinstance(other, Quantity):
            if self.instrument != other.instrument:
                return Price(self.size / other.size, self.instrument / other.instrument)

        return Quantity._math_operation(self, other, operator.truediv)
Esempio n. 4
0
    def __mul__(self, other: Union['Price', 'Quantity', float, int]):
        """Multiplies two PriceType objects together.

        Arguments:
            other: Union[Price, float, int]
                The object that is the right argument of the
                operation.

        Raises:
            IncompatibleTradingPairException:
                If (other : Price) and the pairs do not equal each other.
            InvalidNonNumericQuantity:
                If not (other : Union[Price, Number]).
        """
        price = None

        if isinstance(other, Price):
            rate = self.rate * other.rate
            pair = self.pair or other.pair

            if self.pair != other.pair:
                raise IncompatibleTradingPairOperation(self.pair, other.pair)

            price = Price(rate, pair)

        elif isinstance(other, float) or isinstance(other, int):
            price = Price(self.rate * other, self.pair)

        elif isinstance(other, Quantity):

            if other.instrument != self.pair.quote:
                raise IncompatiblePriceQuantityOperation(
                    self.pair.quote, other.instrument)

            return Quantity(self.pair.base, self.rate * other.size,
                            other.path_id)

        elif not isinstance(other, Number):
            raise InvalidNonNumericQuantity(other)

        return price