> 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/selling-at-the-best-price-considering-historical-data.md).

# Selling at the Best Price Considering Historical Data

[Grid 2: Selling at the Best Price Considering Historical Data](/user-guide/grids-examples.md#grid-2-prodazha-po-luchshei-cene-s-uchetom-istoricheskikh-dannykh)

Selling at the best price, not lower than `min_ask_price` and the midpoint between the average closing price and the average highs of daily candles over the past month.

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

The grid places sell orders at the best price. An additional parameter, <mark style="color:red;">**`gap`**</mark>, is used to calculate the order prices. In this example, it is set to **1%**.

<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 additional parameters, daily candles for the last 30 days are retrieved.

[`candles`](/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 these, additional parameters are calculated: the average closing price and the average daily high price over the last 30 days.

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

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

<figure><img src="/files/qeezec3bt3kYXh6oHyvP" alt=""><figcaption></figcaption></figure>

Next, the order price is calculated as the maximum of the current best price, the minimum ask price set in the <mark style="color:red;">`min_ask_price`</mark> parameter, and the midpoint between the average closing price and the average high price over the last 30 days.

In this example, <mark style="color:red;">`min_ask_price`</mark> is set to the maximum of the average closing price and the current best bid price, plus 2 price\_precision.

Orders will be placed until the balance of the **Counter** token exceeds 5000.\
If needed, this can be replaced with a combined 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 based on the token balance <mark style="color:red;">`base`</mark> [<mark style="color:blue;">`balance`</mark>](/function/structures/balance.md)<mark style="color:red;">`('`</mark>[<mark style="color:blue;">`base`</mark>](/function/shortcuts/base.md)<mark style="color:red;">`').total`</mark>

<mark style="color:red;">`5 if`</mark> [<mark style="color:blue;">`balance`</mark>](/function/structures/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>
