def __rsub__(self, other: Union['Price', float, int]): """Subtracts two PriceType objects together. Arguments: other: Union[Price, float, int] The object that is the left 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 = other.rate - self.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(other - self.rate, self.pair) elif not isinstance(other, Number): raise InvalidNonNumericQuantity(other) return price
def __iadd__(self, other: Union['Price', float, int]): """Iteratively add to a Price object. Arguments: other: Union[Price, float, int] The object that is the left argument of the operation. Raises: IncompatibleTradingPairException: If (other : Price) and the pairs do not equal each other. InvalidNonNumericQuantity: If not (other : Union[Price, Number]). """ if isinstance(other, Price): if self.pair != other.pair: raise IncompatibleTradingPairOperation(self.pair, other.pair) self.rate += other.rate elif isinstance(other, float) or isinstance(other, int): self.rate += other elif not isinstance(other, Number): raise InvalidNonNumericQuantity(other) return self
def __truediv__(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, Price): if self.pair.base == other.pair.base and self.pair.quote != other.pair.quote: return Price(self.rate / other.rate, other.pair.quote / self.pair.quote) elif self.pair.base != other.pair.base and self.pair.quote == other.pair.quote: return Price(self.rate / other.rate, self.pair.base / other.pair.base) elif self.pair.base != other.pair.base and self.pair.quote != other.pair.quote: raise IncompatibleTradingPairOperation(self.pair, other.pair) return self.rate / other.rate elif isinstance(other, float) or isinstance(other, int): price = Price(self.rate / other, self.pair) elif isinstance(other, Quantity): raise IncompatiblePriceQuantityOperation(self, other) elif not isinstance(other, Number): raise InvalidNonNumericQuantity(other) return price
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
def __rmul__(self, other): if not isinstance(other, Number): raise IncompatibleTradingPairOperation(other, self) return Price(other, self)