> For the complete documentation index, see [llms.txt](https://docs.origami.tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.origami.tech/user-guide/grids-examples/buying-at-the-best-price-with-maximum-price-and-balance-constraints.md).

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

Learn how to create Origami Tech grid strategies that execute buy orders efficiently while respecting price and balance limitations.

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>](/function/pre-defined-parameters/order-pos.md)<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>](/function/required-parameters/execute-volume.md) 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>](/function/required-parameters/execute-price.md)<mark style="color:red;">`+`</mark>[<mark style="color:blue;">`abs`</mark>](/function/functions/abs.md)<mark style="color:red;">`(`</mark>[<mark style="color:blue;">`order_pos`</mark>](/function/pre-defined-parameters/order-pos.md)<mark style="color:red;">`)*10/`</mark>[<mark style="color:blue;">`execute_price`</mark>](/function/required-parameters/execute-price.md)

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>](/function/functions/group-balance.md)<mark style="color:red;">`('counter').current.total`</mark>

<mark style="color:red;">`5 if`</mark> [<mark style="color:blue;">`group_balance`</mark>](/function/functions/group-balance.md)<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>](/function/functions/balance.md)<mark style="color:red;">`('base').total`</mark>

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