> 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/bots/lua-bot.md). Agents can also search and read this documentation through its [MCP server](https://docs.origami.tech/~gitbook/mcp).

# Lua bot

Lua bots — closed beta of scripted strategies. Invite code, creating a bot, strategies from templates or the AI assistant, VM limits, error policy, and how Stop and Delete treat open orders.

A Lua bot runs one or more **strategies** — Lua scripts that subscribe to market data and place orders through the bot API. Each bot is a separate process bound to one account and one market type (Spot or Futures). Strategies can be written by hand, started from a template, or generated and edited with the built-in AI assistant.

{% hint style="info" %}
Lua bots and the AI assistant are in **closed beta**. The **Lua Bots** tab shows a locked panel until you redeem an invite code: **Enter invite code → Unlock**.
{% endhint %}

![Lua Bots tab: Describe it, Templates and Blank file cards](https://602593284-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDjboTEEPFNxFhKJ6msZi%2Fuploads%2FTk5yfAkqEVk3gqx2mrxc%2Fbots-lua-bot-01.png?alt=media)

## Create a bot

1. On the **Lua Bots** tab click **New bot** (or pick **Describe it**, **Start from a template** or **Blank file** in the gallery — all end in the same dialog).
2. **New Lua bot**:
   * **Name** — up to 32 characters.
   * **Market** — **Spot** or **Futures**. Futures-only templates lock this to Futures.
   * **Account** — the account the bot trades on.
   * **Pair for the first strategy**.
3. Click **Create and open editor**. The bot is created with default VM limits and error policy, and the first strategy opens in the editor.

The account and market cannot be changed later — they define the bot's connection. **Bot Name**, **Default VM limits** and **Default error policy** are edited in **Bot settings**.

## Bot page

The bot page shows the account, the market, *N of M strategies enabled*, and:

* **Start bot** / **Stop bot**
* **Bot settings**
* **Strategies** — a card per strategy with its own enabled switch and a menu: edit, **Reset state**, **Delete strategy**
* **Add strategy**
* **Delete this bot**

A bot with no enabled strategies runs nothing.

## Strategy editor

The editor has tabs on the left — **Script .lua**, **Configuration**, **Variables**, **Help** — and panels on the right — **Assistant** and **Debugger**. The header holds the strategy name, the enabled switch, **Validate**, **Debug** (existing strategies) and **Create** / **Save**.

### Templates

**Start from a template** opens **Strategy templates**:

| Template            | What it does                                                                | Market        |
| ------------------- | --------------------------------------------------------------------------- | ------------- |
| **Blank script**    | Scaffold with the main loop, a ticker read and one subscription             | Spot, Futures |
| **Bounded grid**    | Buys below and sells above the price inside a range, re-arms levels on fill | Spot, Futures |
| **Two-sided maker** | Quotes both sides and re-quotes on a timer                                  | Spot, Futures |
| **DCA on drawdown** | Fixed quote-size buys on each step of drawdown                              | Spot, Futures |
| **RSI entries**     | RSI(14) on 5-minute candles: buy oversold, sell overbought                  | Spot, Futures |
| **TWAP exit**       | Unwinds a futures position in Reduce-Only slices                            | Futures       |
| **Trailing stop**   | Trails the high-water mark of a long and exits on a drop                    | Futures       |

### AI assistant

The **Assistant** panel is a chat about the current script. Describe what you want (*Ask about this script…*) — the gallery's **Describe it** / **Build it** starts the chat with your prompt. Code the assistant proposes has an **Apply** button that inserts it into the editor. **New chat** clears the context; **Stop** interrupts a long answer.

A strategy generated this way is created **disabled**: read it, **Validate**, and enable it yourself.

### Configuration

| Field                                                                 | Notes                                                                                                                                                                                                              |
| --------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Pair                                                                  | The strategy's main instrument.                                                                                                                                                                                    |
| **Margin mode**, **Leverage** / **Long leverage**, **Short leverage** | Futures only. Short leverage equals long if blank. Applied to the pair on the account when saved.                                                                                                                  |
| Extra instruments                                                     | **Alias**, **Account**, **Futures**, pair — other markets the script reads or trades.                                                                                                                              |
| Subscriptions                                                         | **Topic**, **Symbol**, **Interval** (candles), **Balances**, **Exchange**, **Account id** — data streams the script receives.                                                                                      |
| **Initial state (JSON, optional)**                                    | Object the script starts with, for example `{"step": 1}`. Must be valid JSON.                                                                                                                                      |
| **VM limits**                                                         | Blank inherits the bot defaults: **VM memory limit (bytes)** (default 32 MiB), **Max instructions between yields**, **Max wall-clock between yields (sec)**, **Max serialized state (bytes)**, **Max state keys**. |
| **Error policy**                                                      | See below; **Use the bot's default error policy** to inherit.                                                                                                                                                      |
| **Verbose execution trace (debug)**                                   | Detailed log for the **Debugger** panel.                                                                                                                                                                           |

### Error policy — *On fatal error*

| Option                            | Behaviour                                                                                                                                                                             |
| --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Stop, keep open orders**        | The strategy stops and leaves its orders on the exchange. No automatic restart until you edit or re-enable it.                                                                        |
| **Restart with backoff**          | Restarts with exponential backoff up to **Max restarts**; then stops. Resource-limit errors are not retried. Tick **Cancel open orders on each restart** to start each attempt clean. |
| **Cancel orders and disable bot** | Cancels the strategy's orders and disables it; re-enable manually.                                                                                                                    |

### Help

The **Help** tab loads the bot API reference — the same markdown the assistant is trained on. The link *Full API reference as raw markdown (feed it to an LLM)* opens it in a new tab. Functions include `bot.ticker`, `bot.place_order`, `bot.sleep` and the subscription callbacks; the reference in the tab is authoritative.

## Start, stop, reset, delete

| Action                       | Effect on orders                                                                                                                              | Where                        |
| ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------- |
| **Start bot** / **Stop bot** | Stopping stops the bot process; **open orders stay on the exchange**. To cancel them, disable each strategy first, or cancel on the exchange. | Bot page, row menu           |
| Strategy switch off          | Disables one strategy. With **Cancel orders and disable bot** policy its orders are cancelled; otherwise they stay.                           | Strategy card, editor header |
| **Reset state**              | Clears the strategy's Lua state and restarts it from **Initial state**.                                                                       | Strategy menu                |
| **Delete strategy**          | Removes the strategy. Its Lua state and open orders are not cancelled automatically — disable it first for a clean stop.                      | Strategy menu                |
| **Delete this bot**          | Stops the process and deletes all strategies. Open orders stay on the exchange. Cannot be undone.                                             | Bot page, row menu           |

{% hint style="warning" %}
Neither **Stop bot** nor **Delete Lua bot** cancels orders. Check [Active Orders](https://docs.origami.tech/terminal/active-orders-and-history.md) after stopping a Lua bot.
{% endhint %}

## Related

* [Bots overview](https://docs.origami.tech/bots/overview.md)
* [Bot lifecycle](https://docs.origami.tech/bots/lifecycle.md) — grid bots behave differently on Stop
