# Buying and Selling When the 15-Minute RSI Exits a Defined Range

Execute buy and sell orders when the 15-minute RSI moves outside the specified range of values.

```json
{
  "max_bid": "mean([candle.close for candle in candles_m15])",
  "min_ask": "mean([candle.close for candle in candles_m15])",
  "rsi_m15": "rsi(candles('m15', 'base-counter'), period=14, column='close')[0]",
  "candles_m15": "candles('m15', 'base-counter')",
  "execute_price": "min(orderbook().ask[0].price,max_bid) if side == 'buy' else max(orderbook().bid[0].price,min_ask)",
  "execute_volume": "10 / ticker()",
  "buy_orders_count": "1 if rsi_m15 < 30 else 0",
  "sell_orders_count": "1 if rsi_m15 > 70 else 0",
  "sleep_after_seconds": "300"
}
```

Buy Orders: Triggered when the RSI falls below 30, executed at the first ask price.&#x20;

Sell Orders: Triggered when the RSI rises above 70, executed at the first bid price.

In this example, 15-minute candlestick data is used to calculate the average closing price

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

and the RSI

[<mark style="color:blue;">`rsi`</mark>](https://docs.origami.tech/function/technical-analysis/stochastic-rsi)<mark style="color:red;">`(`</mark>[<mark style="color:blue;">`candles`</mark>](https://docs.origami.tech/function/functions/candles)<mark style="color:red;">`('m15', '`</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;">`'), period=14, column='`</mark>[<mark style="color:blue;">`close`</mark>](https://docs.origami.tech/function/structures/candle)<mark style="color:red;">`')[0]`</mark>

Sell Orders:

If the RSI exceeds 70, a sell order is placed every 300 seconds (as specified by <mark style="color:red;">`"`</mark>[<mark style="color:blue;">`sleep_after_seconds`</mark>](https://docs.origami.tech/function/optional-parameters/sleep-after-seconds)<mark style="color:red;">`": "300"`</mark>) at the best bid price, provided it is not lower than the average closing price of the last 100 fifteen-minute candlesticks.

<mark style="color:red;">`"`</mark>[<mark style="color:blue;">`sell_orders_count`</mark>](https://docs.origami.tech/function/required-parameters/sell-orders-count)<mark style="color:red;">`": "1 if rsi_m15 > 70 else 0"`</mark>

Buy Orders:

If the RSI falls below 30, a buy order is placed every 300 seconds at the best ask price, provided it is not higher than the average closing price of the last 100 fifteen-minute candlesticks.

<mark style="color:red;">`"`</mark>[<mark style="color:blue;">`buy_orders_count`</mark>](https://docs.origami.tech/function/required-parameters/buy-orders-count)<mark style="color:red;">`": "1 if rsi_m15 < 30 else 0"`</mark>
