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

# Outputs for alerts: events and data

> Declare outputs that are not drawn but can be used in alerts: named events with @plot.event and data values with @plot.data.

Some values an indicator computes are meant for alerts, not for the chart. Indie has two kinds of outputs for them.
Neither is drawn, and both are listed as alert sources:

* An *event output* ([`@plot.event`](#event-outputs)) reports that something happened on a bar: a cross, a breakout,
  a volume spike. Users create an alert on the event by name, for example **MACD cross: Bullish cross**, instead of
  building a value condition on a plotted line.
* A *data output* ([`@plot.data`](#data-outputs)) carries a number, such as a stop level or the ATR, that users can
  set value conditions on or put into the alert message, without adding a line to the chart.

Like any other plot, each of them takes one slot in the tuple that `Main` returns.

## Event outputs

Declare an event output with the [`@plot.event`](https://takeprofit.com/docs/indie/Library-reference/package-indie-plot) decorator.

### Syntax

```py theme={null}
@plot.event(id, title=None, options=None)
```

* `id`: a unique identifier of the output, as for other plots.
* `title`: the name users see in the alert dialog. If omitted, the `id` is used.
* `options`: a list of the events this output can report, such as `['Bullish cross', 'Bearish cross']`. Leave it out
  for a single event.

What `Main` returns for the output depends on `options`:

| Declaration | `Main` returns | Meaning |
| - | - | - |
| With `options` | `str` or `None` (`Optional[str]`) | The name of the option that fired on this bar, or `None` if nothing happened |
| Without `options` | `bool` | `True` if the event fired on this bar |

### An output with several options

The indicator below plots MACD and its signal line and reports crosses of the two as one event output with two
options.

```py theme={null}
# indie:lang_version = 5
from indie import indicator, plot, Optional
from indie.algorithms import Macd

@indicator('MACD Cross Events')
@plot.event('cross', 'MACD cross', options=['Bullish cross', 'Bearish cross'])
@plot.line('macd', 'MACD')
@plot.line('signal', 'Signal')
def Main(self):
    macd, signal, hist = Macd.new(self.close, 12, 26, 9)
    event: Optional[str] = None
    if macd[1] <= signal[1] and macd[0] > signal[0]:
        event = 'Bullish cross'
    elif macd[1] >= signal[1] and macd[0] < signal[0]:
        event = 'Bearish cross'
    return event, macd[0], signal[0]
```

`event` is `None` on some paths and a string on others, so it needs the `Optional[str]` annotation. A conditional
expression needs none, so the same output can be written in one line:

```py theme={null}
event = 'Bullish cross' if bull else ('Bearish cross' if bear else None)
```

The chart shows only the two lines. In the alert dialog, the **MACD cross** output offers **Bullish cross**,
**Bearish cross** and **Any**.

### A single event

Without `options`, the output is one event and `Main` returns a `bool`:

```py theme={null}
# indie:lang_version = 5
from indie import indicator, param, plot
from indie.algorithms import Sma

@indicator('Volume Spikes')
@param.int('length', default=20, min=1, title='Average length')
@param.float('factor', default=2.0, min=1.0, title='Spike factor')
@plot.event('spike', 'Volume spike')
@plot.columns('volume', 'Volume')
def Main(self, length, factor):
    avg = Sma.new(self.volume, length)
    spike = self.volume[0] > avg[0] * factor
    return spike, self.volume[0]
```

An indicator can declare as many event outputs as it needs, next to its regular plots. Group related events into one
output with options (for example, the bullish and bearish variants of the same signal), and use separate outputs for
unrelated events.

### Creating an alert on an event

1. Add the indicator to the chart and open the alert dialog (see [Alerts overview](https://takeprofit.com/docs/guide/alerts/Alerts-overview.md)).
2. Choose the indicator as the source, then pick the event output by its title, for example **MACD cross**.
3. For an output with options, pick one option, for example **Bullish cross**, or **Any** to be notified about every
   option. A single event has nothing more to choose.

For event outputs the dialog hides the criteria and the target value. Frequency defaults to **Once Per Bar Close**,
so the alert fires once, on the bar where the event happened. You can change it under the additional settings.

The default alert name is *indicator title: option*, for example **MACD Cross Events: Bullish cross**, or
*indicator title: event title* when **Any** is picked. Keep it, or give the alert your own name: the alerts list shows
the event alert as the value condition it is stored as (see below), so the name is what tells event alerts apart.

### How an event output is encoded

The output is stored as a numeric series, like any other plot:

| Output | Value on a bar with the event | Value on other bars |
| - | - | - |
| With `options` | The 1-based position of the option in `options` (`1` for the first, `2` for the second, …) | `0` |
| Without `options` | `1` | `0` |

The alert dialog builds a regular value condition from it: **Inside Channel** from `k - 0.5` to `k + 0.5` for option
`k`, and **Greater Than** `0.5` for **Any** or a single event. You only need these numbers when you read the output
directly, for example through the [Platform API](https://takeprofit.com/docs/platform-api/overview), or set a value alert on it yourself.
Because the encoding follows the order of `options`, adding an option to the end of the list keeps existing alerts
valid, while reordering or removing options changes what they match.

### Limitations of event outputs

* **Not drawn.** An event output never appears on the chart and has no entry on the Style tab or in the Outputs list
  of the indicator settings. To also show the event, add a [marker](https://takeprofit.com/docs/indie/Plotting-and-drawing/Data-plotting-lines-columns-etc.md#markers)
  or a [label](https://takeprofit.com/docs/indie/Plotting-and-drawing/Drawings-lines-labels.md).
* **`Main` returns the option name or `None`.** A string literal that is not one of the `options` is a compilation
  error. A string computed at run time that matches no option stops the indicator with an error that names the output
  and the value.
* **One option per bar.** An output reports at most one option on a bar. If two events can happen on the same bar
  and users need alerts on both, declare them as separate outputs.
* **Options rules.** `options` must be written as a list literal right in the decorator, not as a module-level
  constant. It holds 2 to 32 distinct, non-empty string literals, each at most 64 characters, with no leading or
  trailing spaces. A list with one item is a compilation error: declare a single event instead.

## Data outputs

Declare a data output with the [`@plot.data`](https://takeprofit.com/docs/indie/Library-reference/package-indie-plot) decorator:

```py theme={null}
@plot.data(id, title=None)
```

* `id`: a unique identifier of the output. It is also the name used in alert message placeholders.
* `title`: the name users see in the alert dialog. If omitted, the `id` is used.

`Main` returns a bare number for it (`float` or `int`), the same value a line would take. Return `nan` on bars where
there is no value.

The indicator below draws an EMA and exposes a trailing stop and the ATR as data outputs:

```py theme={null}
# indie:lang_version = 5
from math import nan
from indie import indicator, param, plot, color
from indie.algorithms import Ema, Atr

@indicator('EMA with ATR Stop', overlay_main_pane=True)
@param.int('length', default=50, min=1, title='EMA length')
@param.float('mult', default=2.0, min=0.1, title='ATR multiplier')
@plot.line('ema', 'EMA', color=color.AQUA)
@plot.data('stop', 'Long stop')
@plot.data('atr', 'ATR')
def Main(self, length, mult):
    ema = Ema.new(self.close, length)
    atr = Atr.new(14)
    stop = self.close[0] - mult * atr[0] if self.close[0] > ema[0] else nan
    return ema[0], stop, atr[0]
```

The chart shows only the EMA. In the alert dialog, **Long stop** and **ATR** are listed next to **EMA** and take the
usual value criteria. Their values can also go into the alert message with
the `{{cond<N>.main.<id>}}` placeholder, for example `Stop: {{cond0.main.stop}}, ATR: {{cond0.main.atr}}` (see
[Variables in alert messages](https://takeprofit.com/docs/guide/alerts/How-use-variable-InAlert.md)).

> [!TIP]
>
> `@plot.data` replaces the old workaround of a `@plot.line` with `pane`, `status_line` and `price_label` all turned off
> in its `display_options`. Such a line still exists on the chart and in the Style tab; a data output does not.

### Limitations of data outputs

* **Not drawn.** A data output creates no chart series and has no entry on the Style tab. It shows no value in the
  status line or on the price scale, and does not affect the chart's scale.
* **Numbers only.** `Main` returns a bare `float` or `int`. Returning a `plot.Line(...)` object, or passing `color=`
  or `display_options=` to `@plot.data`, is a compilation error.
* **No fills.** `@plot.fill` cannot reference a data output or an event output.


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