> ## Documentation Index
> Fetch the complete documentation index at: https://docs.defiloops.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Writing a card in chat

> What to say, what the agent shows back, and the lines a card is made of. Every example is a card you can write today.

**In short:** say what you want in your own words. The agent asks only for what it cannot guess, such as the amount or the chain. It shows you the card and a plain line of what it can do. When you agree, it saves the card and backtests it in the same conversation.

## What to say

Name the token, the rule and how much. These all work as requests.

* "Buy $10 of cbBTC whenever its RSI drops below 30. Use $40 from my wallet, at most once a week."
* "Every Monday at 09:00 UTC, swap 1,000 USDC for WETH, 10 times."
* "Put a stop loss on my WETH at \$2,800."
* "Buy cbBTC when funding turns cheap, but only while I hold none."
* "Take 10 USDC from my wallet and buy cbBTC now."

The agent turns each into a card and shows it. Check three things before you agree.

1. **The money.** Is the budget the amount you meant, and does it come from the right place?
2. **The rule.** Does the condition say what you meant?
3. **The limits.** Is there a cap on how often it acts and how much each run may move?

Say "change something" if any of them is wrong. A change is a new version of the card.

## The lines of a card

| line | says | example |
| - | - | - |
| `PLAN` | the name | `PLAN Dip buyer` |
| `ON` | the network | `ON Base` |
| `CHECK` | how often it looks | `CHECK every 1 day` |
| `BUDGET` | the money set aside for it | `BUDGET 40 USDC from my wallet` |
| `WHEN`, `AND` | the condition | `WHEN rsi(cbBTC) is below 30` |
| `EVERY` | a clock, in UTC | `EVERY Monday at 09:00 UTC` |
| `DO`, `THEN` | what it does, in order | `DO swap 4 USDC for cbBTC, at most 0.5% below market` |
| `ALSO WHEN` | a second rule | `ALSO WHEN this strategy holds cbBTC` |
| `LIMITS` | the caps, joined by `·` | `LIMITS at most once a week · stop after 10 runs` |

A card with no `WHEN`, `EVERY` or `HOLD` runs once, when you press Start.

## Examples

### Buy on an oversold signal

```
PLAN      Dip buyer
ON        Base
CHECK     every 1 day
BUDGET    40 USDC from my wallet
WHEN      rsi(cbBTC) is below 30
DO        swap 4 USDC for cbBTC, at most 0.5% below market
LIMITS    at most once a week · stop after 10 runs · never more than $4 per run
```

Once a day the card reads the RSI of cbBTC. The `rsi` measure is a small script the agent writes for you. If the RSI is below 30, it swaps 4 USDC for cbBTC. It buys at most once a week, at most 10 times, and never more than \$4 in one run. A firing that the budget cannot cover is skipped, never traded in part. Fees and gas count against the budget.

### Buy, then sell on the opposite signal

```
PLAN      Buy cbBTC on RSI dips, sell on RSI above 70
ON        Base
CHECK     every 1 day
BUDGET    10 USDC
WHEN      rsi(cbBTC) is below 30
DO        swap 25% of my USDC for cbBTC, at most 0.5% below market
ALSO WHEN this strategy holds cbBTC
AND       rsi(cbBTC) is above 70
DO        swap 100% of my cbBTC for USDC, at most 0.5% below market
LIMITS    at most once a week
```

`this strategy holds cbBTC` reads what this card holds, never the rest of your wallet. A card tests a level and a position, not a crossing.

### A set day of the week

```
PLAN      Buy WETH on Mondays, a week apart
ON        Base
CHECK     every 1 day
BUDGET    1000 USDC
WHEN      on Monday
AND       the WETH price is below $4,000
DO        swap 100 USDC for WETH, at most 0.5% below market
LIMITS    at most once a week · stop after 10 runs
```

Days of the week are in UTC.

### A fixed schedule

```
PLAN      Invest 10,000 USDC into WETH over 10 weeks
ON        Base
BUDGET    10000 USDC
EVERY     Monday at 09:00 UTC
DO        swap 1000 USDC for WETH, at most 0.5% below market
LIMITS    stop after 10 runs
```

`EVERY week` means the calendar week, which starts Monday 00:00 UTC. `EVERY 7 days` rolls from the moment you start the card, and its first run comes one period later. Write `EVERY 7 days, starting now` for a first run at the start. A run missed while the service was down fires once on recovery, never twice.

### Do something once, now

```
PLAN      Buy cbBTC with 10 USDC from my wallet
ON        Base
DO        take 10 USDC from my wallet
THEN      swap it for cbBTC, at most 1% below market
```

There is no draft and no backtest. You confirm in chat with "Yes, run it once", and the card runs once. The confirmation covers the line the checker wrote and the exact amounts. A run-once card can also supply to a lending market, repay, borrow, bridge and open a liquidity band. See [what a card can watch and do](https://docs.defiloops.com/cards/kinds.md).

### A stop that rests as an order

```
PLAN Stop loss on my ETH
ON Base
IMMEDIATELY WHEN the ETH price is below $2,800
DO swap 100% of my WETH for USDC, at most 1% below market
LIMITS at most once a day
```

`IMMEDIATELY WHEN` places a resting order that acts between bars. A card that says `WHEN` looks only at closed bars.

### A card on data

A card reads any data feed through a measure. This measure reads the daily median funding rate across four venues.

```
//@version=6
indicator("btc_funding", timeframe = "1D")
//@dl.unit fraction
x = dl.feed("view.funding_median.btc")
plot(x, "btc_funding")
```

The card then compares the plot, by the plot's name.

```
PLAN      Buy cbBTC when funding is cheap
ON        Base
CHECK     every 1 day
BUDGET    100 USDC
WHEN      this strategy holds no cbBTC
AND       btc_funding(cbBTC) is below 0.045%
DO        swap 100% of my USDC for cbBTC, at most 0.5% below market
LIMITS    at most once a day
```

Funding rates have periods. A daily funding key is the sum of that day's venue intervals, so 0.015% per 8 hours is about 0.045% a day. The agent converts your rate to the key's period and says so.

## Rules of thumb the checker enforces

* **Name one token one way.** cbBTC on Base, WBTC on Ethereum and Arbitrum, WETH for ether, USDC for dollars.
* **Put every cap on the one `LIMITS` line**, joined by `·`. A line that starts with `stop` is refused.
* **Guard a rule that could be true on every bar.** Use `this strategy holds no cbBTC` or a frequency, or the card is refused.
* **Money you set aside is the `BUDGET`.** "Use \$40" is `BUDGET 40 USDC`. The size of each trade is the amount in `DO`.
* **A swap accepts at most 1.5% below market.** The same bound holds for an order's band. A wider one is refused when you save, approve, start or place the card. The 1.5% leaves room for the router's fee and price drift inside a 2% band. A token outside our list for the card's network is refused too.
* **Units must match.** A health factor is a plain number, a price is dollars and a probability is a percentage.
* **The clock cannot be coarser than the measure.** A weekly rule is checked daily, with a once-a-week limit.

When the checker refuses a line, it names the line and the reason. The agent shows you the reason in its own words, and offers the nearest card that works. It does not invent a workaround.

## What it cannot write yet

* A window under an hour, such as "if it falls 3% in five minutes". Measures run on 1-hour, 4-hour, daily or weekly bars. The nearest card is an hourly one, or a resting order.
* A card that only sells cannot be backtested, because every backtest starts in USDC. Write the entry rule too.
* Waiting inside a card, with `THEN WAIT UNTIL`. The checker reads it, but a card that uses it is refused today.

## Under the hood

This part is for readers who want the mechanism.

The measure language is a subset of Pine v6. A measure starts with `//@version=6`, declares one `indicator`, states one unit and ends in `plot`. The units are probability, fraction, percent, price, usd, flag and count. The subset has no `request.security`, no `strategy.entry`, no future lookback and no drawing. The data checks prove that a bar uses only that bar and earlier ones, and that the same bars give the same numbers.

The agent reuses a measure you already have instead of writing a new one, because the same measure over the same data is stored once.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.