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

# Placing orders with a spread based on historical data

Grid recipe: place bids and asks at the best price within a spread, keeping them away from historical price levels.

Learn how to create Origami grid strategies that adjust spreads dynamically based on historical volatility and price performance.

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: `candles('m15','base-counter')`.
* 14 daily candlesticks: `candles('d1','base-counter',count=14)`.

#### Ask Price Calculation

The ask price is determined as the maximum of:

* The best ask price minus `symbol().price_precision`.
* The average high price of 100 fifteen-minute candlesticks (`mean_high_15m`).
* The minimum ask price (`min_ask_price`). In example it equal to `mean_high_15m`. 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.

`max(` `orderbook().ask[0].price-symbol().price_precision,(two_weeks_mean_high+two_weeks_mean_close)/2,mean_high_15m,min_ask_price)`

#### Bid Price Calculation

The bid price is determined as the minimum of:

* The best bid price plus `symbol().price_precision`
* The average low price of 100 fifteen-minute candlesticks (`mean_low_15m`)
* The maximum bid price (`max_bid_price`). In example it equal to `mean_low_15m`. 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.

`min(` `orderbook().bid[0].price+symbol().price_precision,(two_weeks_mean_low+two_weeks_mean_close)/2,mean_low_15m,max_bid_price)`

#### 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 `symbol().price_precision`), orders are adjusted to meet the minimum spread.

Otherwise:

Asks are placed at price\_ask with a 0.5% increment (`gap_ask`). Bids are placed at price\_bid with a 0.5% increment (`gap_bid`).

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

`(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))`
