# Buying at the Best Price with Maximum Price and Balance Constraints

Buying at the best price with a cap on maximum buy price and current counter token balance

```json
{
"gap": "0.005",
"execute_price": "min(orderbook().bid[0].price+symbol().price_precision,max_bid_price)*(1+gap*(order_pos+1))",
"max_bid_price": "orderbook().ask[0].price-symbol().price_precision",
"execute_volume": "10/execute_price+abs(order_pos)*10/execute_price",
"buy_orders_count": "5 if balance('counter').total > 5000 else 0",
"sell_orders_count": "0"
}
```

The grid places buy orders at the best available price. An additional parameter, gap, is used in calculating the order prices. In this example, gap is set to 0.5%.

<mark style="color:red;">`1+gap*(`</mark>[<mark style="color:blue;">`order_pos`</mark>](https://docs.origami.tech/function/pre-defined-parameters/order-pos)<mark style="color:red;">`-1)`</mark>

<mark style="color:red;">`max_bid_price`</mark>parameter in this case is defined as:

<mark style="color:red;">`orderbook().ask[0].price-symbol().price_precision`</mark>

which sets the maximum buy price to one price\_precision below the best ask price. Alternatively, a specific numerical value can be used to set the limit.

The [<mark style="color:blue;">`execute_volume`</mark>](https://docs.origami.tech/function/required-parameters/execute-volume) parameter determines the base token amount, equivalent to 20 counter tokens for the first order, with an increment of 10 counter tokens for each subsequent order. The formula used is:&#x20;

<mark style="color:red;">`10/`</mark>[<mark style="color:blue;">`execute_price`</mark>](https://docs.origami.tech/function/required-parameters/execute-price)<mark style="color:red;">`+`</mark>[<mark style="color:blue;">`abs`</mark>](https://docs.origami.tech/function/functions/abs)<mark style="color:red;">`(`</mark>[<mark style="color:blue;">`order_pos`</mark>](https://docs.origami.tech/function/pre-defined-parameters/order-pos)<mark style="color:red;">`)*10/`</mark>[<mark style="color:blue;">`execute_price`</mark>](https://docs.origami.tech/function/required-parameters/execute-price)

Orders will continue to be placed until the balance of the <mark style="color:red;">`counter`</mark> token drops below 5000.

If needed, this limitation can be replaced with the aggregated balance across multiple exchanges [<mark style="color:blue;">`group_balance`</mark>](https://docs.origami.tech/function/functions/group-balance)<mark style="color:red;">`('counter').current.total`</mark>

<mark style="color:red;">`5 if`</mark> [<mark style="color:blue;">`group_balance`</mark>](https://docs.origami.tech/function/functions/group-balance)<mark style="color:red;">`('counter').current.total > 5000 else 0`</mark>

&#x20;or with the balance of the base token, defined as: [<mark style="color:blue;">`balance`</mark>](https://docs.origami.tech/function/functions/balance)<mark style="color:red;">`('base').total`</mark>

<mark style="color:red;">`5 if`</mark> [<mark style="color:blue;">`balance`</mark>](https://docs.origami.tech/function/functions/balance)<mark style="color:red;">`('base').total < 5000 else 0`</mark>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.origami.tech/user-guide/grids-examples/buying-at-the-best-price-with-maximum-price-and-balance-constraints.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
