> 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/grids/recipes/buying-at-the-best-price-with-maximum-price-and-balance-constraints.md).

# Buying at the best price with maximum price and balance constraints

Grid recipe: buy at the best available price with a cap on the maximum buy price and on your counter token balance.

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%.

`1+gap*(order_pos-1)`

`max_bid_price`parameter in this case is defined as:

`orderbook().ask[0].price-symbol().price_precision`

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 `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:

`10/execute_price+abs(order_pos)*10/execute_price`

Orders will continue to be placed until the balance of the `counter` token drops below 5000.

If needed, this limitation can be replaced with the aggregated balance across multiple exchanges `group_balance('counter').current.total`

`5 if` `group_balance('counter').current.total > 5000 else 0`

or with the balance of the base token, defined as: `balance('base').total`

`5 if` `balance('base').total < 5000 else 0`
