Пример #1
0
    def test_quantize(self):
        x = Decimal('100.0001')
        precision = Decimal('0.01')
        self.assertEqual(quantize(x, precision, down=True), Decimal('100'))
        self.assertEqual(quantize(x, precision, down=False), Decimal('100.01'))

        precision = Decimal('0.01000000')
        self.assertEqual(quantize(x, precision, down=True), Decimal('100'))
        self.assertEqual(quantize(x, precision, down=False), Decimal('100.01'))
Пример #2
0
    def _validate_order(self, order, price_estimates=None):
        resources = self.get_resources()
        symbol = ''.join(order.product.split('_'))
        filt = self.filters[symbol]

        if order._quantity < filt['min_order_size']:
            return
        if order._quantity > filt['max_order_size']:
            order._quantity = filt['max_order_size']
        order._quantity = quantize(order._quantity, filt['order_step'])

        if order._price is not None:
            if order._price < filt['min_price']:
                return
            if order._price > filt['max_price']:
                return
            if order._price % filt['price_step'] != 0:
                order._price = quantize(order._price,
                                        filt['price_step'],
                                        down=order._action.name == 'BUY')

        if order._price is None:
            value = (price_estimates[filt['commodity']] /
                     price_estimates[filt['base']]) * order._quantity
        else:
            value = order._price * order._quantity
        if value < filt['min_notional']:
            return

        if order._action.name == 'SELL':
            if resources[filt['commodity']] < order._quantity:
                order._quantity = resources[filt['commodity']]
                order = self._validate_order(order, price_estimates)
        else:
            if order._price is None:
                price_commodity = price_estimates[filt['commodity']]
                price_base = price_estimates[filt['base']]
                price = price_commodity / price_base
            else:
                price = order._price
            if resources[filt['base']] < order._quantity * price:
                order._quantity = resources[filt['base']] / (price *
                                                             Decimal('1.0001'))
                # any number
                order = self._validate_order(order, price_estimates)
        return order
Пример #3
0
    def _validate_order(self, order, price_estimates=None):
        resources = self.get_resources()
        symbol = order.product.replace('_', '-')
        filt = self.filters[symbol]
        base, commodity = filt['base'], filt['commodity']
        # quantity
        if order._quantity < filt['min_order_size']:
            return

        if order._quantity > filt['max_order_size']:
            order._quantity = filt['max_order_size']

        if order._quantity % filt['order_step'] > 0:
            order._quantity = quantize(order._quantity, filt['order_step'])

        # price
        if order._price is not None:
            if order._price % filt['price_step'] > 0:
                order._price = quantize(order._price, filt['price_step'],
                                        down=(order._action.name == 'BUY'))

        # resources
        if order._action.name == 'SELL':
            if order._quantity > resources[commodity]:
                order._quantity = resources[commodity]
                order = self._validate_order(order, price_estimates)
        else:
            epsilon = Decimal('1.0001')
            if order._price is not None:
                price = order._price
                if price * order._quantity > resources[base]:
                    order._quantity = resources[base] / (
                        price * epsilon)
                    order = self._validate_order(order, price_estimates)
            else:
                price = price_estimates[commodity] / price_estimates[base]
                fee = self.get_taker_fee(order.product) + 1
                if price * order._quantity * fee > resources[base]:
                    order._quantity = resources[base] / (price * fee * epsilon)
                    order = self._validate_order(order, price_estimates)
        return order