> 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/placing-orders-with-a-spread-based-on-historical-data.md).

# Placing Orders with a Spread Based on Historical Data

Orders are placed at the best price within a defined spread, ensuring asks are not set below, and bids not set above, historical price levels.

```json
{
"gap_ask": "0.005",
"gap_bid": "0.005",
"price_ask": "max(orderbook().ask[0].price-symbol().price_precision,(two_weeks_mean_high+two_weeks_mean_close)/2,mean_high_15m,min_ask_price)",
"price_bid": "min(orderbook().bid[0].price+symbol().price_precision,(two_weeks_mean_low+two_weeks_mean_close)/2,mean_low_15m,max_bid_price)",
"candles_1d": "candles('d1', 'base-counter',count=14)",
"spread_min": "10*symbol().price_precision",
"candles_15m": "candles('m15','base-counter')",
"mean_low_15m": "mean([price.low for price in candles_15m])",
"execute_price": "(((price_bid+price_ask)/2-spread_min/2)*(1+gap_bid*(order_pos+1)) if side=='buy' else ((price_bid+price_ask)/2+spread_min/2)*(1+gap_ask*(order_pos-1))) if (price_ask-price_bid)<spread_min else (price_bid*(1+gap_bid*(order_pos+1)) if side=='buy' else price_ask*(1+gap_ask*(order_pos-1)))",
"max_bid_price": "mean_low_15m",
"mean_high_15m": "mean([price.high for price in candles_15m])",
"min_ask_price": "mean_high_15m",
"execute_volume": "(6/execute_price if execute_price>two_weeks_mean_low else 10 / execute_price * abs(order_pos)) if side=='buy' else (6/execute_price if execute_price<two_weeks_mean_high else 10 / execute_price * abs(order_pos))",
"buy_orders_count": "5",
"sell_orders_count": "5",
"two_weeks_mean_low": "mean([price.low for price in candles_1d])",
"two_weeks_mean_high": "mean([price.high for price in candles_1d])",
"two_weeks_mean_close": "mean([price.close for price in candles_1d])"
}
```

In this example, the following additional data is used:

* 100 fifteen-minute candlesticks: [<mark style="color:blue;">`candles`</mark>](/function/functions/candles.md)<mark style="color:red;">`('m15','`</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;">`')`</mark>.
* 14 daily candlesticks: [<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=14)`</mark>.

#### Ask Price Calculation

The ask price is determined as the maximum of:

* The best ask price minus [<mark style="color:blue;">`symbol`</mark>](/function/functions/symbol.md)<mark style="color:red;">`().price_precision`</mark>.
* The average high price of 100 fifteen-minute candlesticks (<mark style="color:red;">`mean_high_15m`</mark>).
* The minimum ask price (<mark style="color:red;">`min_ask_price`</mark>). In example it equal to <mark style="color:red;">`mean_high_15m`</mark>. If required, it can be replaced with a specific number or any other value.
* The midpoint between the average closing price and the average high price of the last two weeks' daily candlesticks.

[<mark style="color:blue;">`max`</mark>](/function/functions/max.md)<mark style="color:red;">`(`</mark> [<mark style="color:blue;">`orderbook`</mark>](/function/functions/orderbook.md)<mark style="color:red;">`().ask[0].price-`</mark>[<mark style="color:blue;">`symbol`</mark>](/function/functions/symbol.md)<mark style="color:red;">`().price_precision,(two_weeks_mean_high+two_weeks_mean_close)/2,mean_high_15m,min_ask_price)`</mark>

#### Bid Price Calculation

The bid price is determined as the minimum of:

* The best bid price plus [<mark style="color:blue;">`symbol`</mark>](/function/functions/symbol.md)<mark style="color:red;">`().price_precision`</mark>
* The average low price of 100 fifteen-minute candlesticks (<mark style="color:red;">`mean_low_15m`</mark>)
* The maximum bid price (<mark style="color:red;">`max_bid_price`</mark>). In example it equal to <mark style="color:red;">`mean_low_15m`</mark>. If required, it can be replaced with a specific number or any other value.
* The midpoint between the average closing price and the average low price of the last two weeks' daily candlesticks.

[<mark style="color:blue;">`min`</mark>](/function/functions/min.md)<mark style="color:red;">`(`</mark> [<mark style="color:blue;">`orderbook`</mark>](/function/functions/orderbook.md)<mark style="color:red;">`().bid[0].price+`</mark>[<mark style="color:blue;">`symbol`</mark>](/function/functions/symbol.md)<mark style="color:red;">`().price_precision,(two_weeks_mean_low+two_weeks_mean_close)/2,mean_low_15m,max_bid_price)`</mark>

#### Spread and Price Adjustment

If the spread between the calculated price\_ask and price\_bid is smaller than the parameter spread\_min (set to 10 [<mark style="color:blue;">`symbol`</mark>](/function/functions/symbol.md)<mark style="color:red;">`().price_precision`</mark>), orders are adjusted to meet the minimum spread.

Otherwise:

Asks are placed at price\_ask with a 0.5% increment (<mark style="color:red;">`gap_ask`</mark>). Bids are placed at price\_bid with a 0.5% increment (<mark style="color:red;">`gap_bid`</mark>).

#### Order Volume Calculation

Order volume is set without increments and equals the equivalent of 6 counter tokens if the order price within the range of the average low and average high prices of two weeks' daily candlesticks.

For orders outside this range, the volume increases as the equivalent of 10 counter tokens multiplied by the order number.

<mark style="color:red;">`(6/`</mark>[<mark style="color:blue;">`execute_price`</mark>](/function/required-parameters/execute-price.md) <mark style="color:red;">`if`</mark> [<mark style="color:blue;">`execute_price`</mark>](/function/required-parameters/execute-price.md)<mark style="color:red;">`>two_weeks_mean_low else 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;">`)) if`</mark> [<mark style="color:blue;">`side`</mark>](/function/pre-defined-parameters.md#side-string)<mark style="color:red;">`=='`</mark>[<mark style="color:blue;">`buy`</mark>](/function/pre-defined-parameters.md)<mark style="color:red;">`' else (6/`</mark>[<mark style="color:blue;">`execute_price`</mark>](/function/required-parameters/execute-price.md) <mark style="color:red;">`if`</mark> [<mark style="color:blue;">`execute_price`</mark>](/function/required-parameters/execute-price.md)<mark style="color:red;">`<two_weeks_mean_high else 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;">`))`</mark>
