# Selling at the Best Price Considering Historical Data

[Grid 2: Selling at the Best Price Considering Historical Data](https://docs.origami.tech/user-guide/grids-examples/..#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>](https://docs.origami.tech/function/pre-defined-parameters/order-pos)<mark style="color:red;">`-1)`</mark>

As additional parameters, daily candles for the last 30 days are retrieved.

[`candles`](https://docs.origami.tech/function/functions/candles)<mark style="color:red;">`('d1', '`</mark>[<mark style="color:blue;">`base`</mark>](https://docs.origami.tech/function/shortcuts/base)<mark style="color:red;">`-`</mark>[<mark style="color:blue;">`counter`</mark>](https://docs.origami.tech/function/shortcuts/counter)<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>](https://docs.origami.tech/function/functions/mean)<mark style="color:red;">`([price.`</mark>[<mark style="color:blue;">`close`</mark>](https://docs.origami.tech/function/structures/candle) <mark style="color:red;">`for price in candles_1d])`</mark>

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

<figure><img src="https://3075831504-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh6BkURa7HNU017yGuhBt%2Fuploads%2FRmTEbUbYzJJyt5zxvqGi%2Fimage.png?alt=media&#x26;token=f433a94c-0758-4a1b-8a53-458378f7b828" 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>](https://docs.origami.tech/function/functions/group-balance)<mark style="color:red;">`('`</mark>[<mark style="color:blue;">`counter`</mark>](https://docs.origami.tech/function/shortcuts/counter)<mark style="color:red;">`').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;">`('`</mark>[<mark style="color:blue;">`counter`</mark>](https://docs.origami.tech/function/shortcuts/counter)<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>](https://docs.origami.tech/function/structures/balance)<mark style="color:red;">`('`</mark>[<mark style="color:blue;">`base`</mark>](https://docs.origami.tech/function/shortcuts/base)<mark style="color:red;">`').total`</mark>

<mark style="color:red;">`5 if`</mark> [<mark style="color:blue;">`balance`</mark>](https://docs.origami.tech/function/structures/balance)<mark style="color:red;">`('`</mark>[<mark style="color:blue;">`base`</mark>](https://docs.origami.tech/function/shortcuts/base)<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/selling-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.
