# Buying at the Best Price Considering Historical Data

```json
{
"gap": "0.01",
"candles_1d": "candles('d1', 'base-counter',count=30)",
"execute_price": "min(orderbook().bid[0].price+symbol().price_precision,(month_mean_low+month_mean_close)/2,max_bid_price)*(1+gap*(order_pos+1))",
"max_bid_price": "min(month_mean_close,orderbook().ask[0].price-symbol().price_precision*2)",
"execute_volume": "10/execute_price if execute_price>month_mean_low else (10/execute_price+abs(order_pos)*10/execute_price)",
"month_mean_low": "mean([price.low for price in candles_1d])",
"buy_orders_count": "5 if balance('counter').total > 5000 else 0",
"month_mean_close": "mean([price.close for price in candles_1d])",
"sell_orders_count": "0"
}
```

Buy orders are placed at the best available price, provided it does not exceed the max\_bid\_price or the midpoint between the average closing price and the average daily low over the past month.

The grid places buy orders at the best price, with an additional parameter, gap, used to calculate the order prices. In this example, the gap is set to 1%.

Price reduction: <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>

As supplementary parameters, daily candlestick data for the past 30 days is retrieved:

[<mark style="color:blue;">`candles`</mark>](/function/functions/candles.md)<mark style="color:red;">`('d1', '`</mark>[<mark style="color:blue;">`base`</mark>](/function/shortcuts/base.md)<mark style="color:red;">`-`</mark>[<mark style="color:blue;">`counter`</mark>](/function/shortcuts/counter.md)<mark style="color:red;">`',count=30)`</mark>

Based on this data, the following additional parameters are calculated:

Average closing price over 30 days:

[<mark style="color:blue;">`mean`</mark>](/function/functions/mean.md)<mark style="color:red;">`([price.close for price in candles_1d])`</mark>

Average daily low over 30 days:

[<mark style="color:blue;">`mean`</mark>](/function/functions/mean.md)<mark style="color:red;">`([price.low for price in candles_1d])`</mark>

The order price is then determined as the maximum of:

* The current best bid.
* The maximum bid price: max\_bid\_price. In this case minimum of the current best ask price minus two price\_precision and average closing price over 30 days.
* The midpoint between the average closing price and the average daily low over the past 30 days.

Orders will be placed until the counter token balance exceeds 5000

If needed, this restriction 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;">`('`</mark>[<mark style="color:blue;">`counter`</mark>](/function/shortcuts/counter.md)<mark style="color:red;">`').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;">`('`</mark>[<mark style="color:blue;">`counter`</mark>](/function/shortcuts/counter.md)<mark style="color:red;">`').current.total < 5000 else 0`</mark>

or the base token balance

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