PERCENT_PRICE_BYSIDE error

Hello there! I need help to undestand the limits in the Binance Futures.
Basically, I’m developing a software (in python) that receives a trade message on Telegram and uses that to open an order in the Binance Futures. I’m using Testnet.
So, I have a method that make a get_avg_price and get the multipliers Up/Down to ask and bid, but, the formula to compute the max and min prices looks wrong, the order isn’t open because the price is out of range. I think that has something to do with PERCENT_PRICE_BY_SIDE filter. Currently, I’m using BTCUSDT coin.

My method of the formula:

    def is_price_within_percent_price_by_side_filter(self, symbol, price, percent_price_filter):
        avg_price = float(self.binance_client.get_avg_price(symbol=symbol)['price'])
        bid_multiplier_up = float(percent_price_filter['bidMultiplierUp'])
        bid_multiplier_down = float(percent_price_filter['bidMultiplierDown'])
        ask_multiplier_up = float(percent_price_filter['askMultiplierUp'])
        ask_multiplier_down = float(percent_price_filter['askMultiplierDown'])

        min_bid_price = avg_price * bid_multiplier_down
        max_bid_price = avg_price * bid_multiplier_up
        min_ask_price = avg_price * ask_multiplier_down
        max_ask_price = avg_price * ask_multiplier_up

and my method to open an order: (there are some prints to help me lol)

    def abrir_ordem(self, par_trading, alavancagem, intervalo_precos, take_profit, stop_loss):
        symbol = par_trading.replace(" / ", "")
        quantidade = int(re.search(r'\d+', alavancagem).group())
        preco_min, preco_max = map(float, intervalo_precos.split(" a "))

        percent_price_filter = self.get_percent_price_by_side_filter(symbol)
        if not percent_price_filter:
            return

        avg_price = float(self.binance_client.get_avg_price(symbol=symbol)['price'])

        # Ajusta os preços de take profit e stop loss para garantir que estejam dentro dos limites permitidos
        take_profit_price = avg_price * (1 + take_profit / 100)
        take_profit_price = self.ajustar_precisao_e_verificar_filtro(symbol, take_profit_price)

        stop_loss_price = avg_price * (1 - stop_loss / 100)
        stop_loss_price = self.ajustar_precisao_e_verificar_filtro(symbol, stop_loss_price)

        print(f"Symbol: {symbol}")
        print(f"Avg Price: {avg_price}")
        print(f"Preco Max: {preco_max}")
        print(f"Take Profit Price: {take_profit_price}")
        print(f"Stop Loss Price: {stop_loss_price}")

        if take_profit_price is not None and stop_loss_price is not None:
            if self.is_price_within_percent_price_by_side_filter(symbol, preco_max, percent_price_filter):
                self.criar_ordem_take_profit(symbol, quantidade, take_profit_price)
                self.criar_ordem_stop_loss(symbol, quantidade, stop_loss_price)
            else:
                logger.err_or("Preço máximo fora dos limites do filtro PERCENT_PRICE_BYSIDE.")
        else:
            logger.error("Erro ao ajustar preços de take profit e stop loss.")

Try this:

def is_price_within_percent_price_by_side_filter(self, symbol, price, percent_price_filter):
    avg_price = float(self.binance_client.get_avg_price(symbol=symbol)['price'])
    bid_multiplier_up = float(percent_price_filter['bidMultiplierUp'])
    bid_multiplier_down = float(percent_price_filter['bidMultiplierDown'])
    ask_multiplier_up = float(percent_price_filter['askMultiplierUp'])
    ask_multiplier_down = float(percent_price_filter['askMultiplierDown'])

    min_bid_price = avg_price * bid_multiplier_down
    max_bid_price = avg_price * bid_multiplier_up
    min_ask_price = avg_price * ask_multiplier_down
    max_ask_price = avg_price * ask_multiplier_up

    # Check if the given price is within the bid or ask range
    is_within_bid = min_bid_price <= price <= max_bid_price
    is_within_ask = min_ask_price <= price <= max_ask_price

    return is_within_bid or is_within_ask

Also try this revised function to validate the prices of take profit and stop loss:

#,,,,

if take_profit_price is not None and stop_loss_price is not None:
    if self.is_price_within_percent_price_by_side_filter(symbol, take_profit_price, percent_price_filter) and \
       self.is_price_within_percent_price_by_side_filter(symbol, stop_loss_price, percent_price_filter):
        self.criar_ordem_take_profit(symbol, quantidade, take_profit_price)
        self.criar_ordem_stop_loss(symbol, quantidade, stop_loss_price)
    else:
        logger.error("Preço máximo ou mínimo fora dos limites do filtro PERCENT_PRICE_BY_SIDE.")
else:
    logger.error("Erro ao ajustar preços de take profit e stop loss.")

This way, we check if the take profit and stop loss prices are within the allowed range and we ensure that the ajustar_precisao_e_verificar_filtro method adjusts the prices correctly to match Binance’s price precision requirements.

Thanks! I just finish some updates using your sugestion and works very well!