---
description: Alerts in @titancharts/react, covering the price axis bell and the showPriceAxisAlertButton setting, the price-axis:alert event, the chart menu's price-card bell and onAddAlert in React, and drawing alerts with the Alert button on a drawing (showDrawingAlertButton), the drawing:alert event with the level, zone, sloped line or levels to watch, Edit alert and Remove alert with drawing:alert-remove and onRemoveAlert, requestDrawingAlert and getDrawingAlertTarget, and setDrawingAlerts and playDrawingAlertEffect for showing your alert state.
---

# Alerts

A user asks for an alert at a price, or on a drawing. The chart tells your application what to watch: a price on the axis, or the level, zone, sloped line or set of levels a drawing stands for, worked out from the drawing. Your application opens its own alert flow, then hands the alerts it holds back to the chart, which shows them on the drawings. Your application creates, stores and checks the alerts; the chart does none of that.

Use it when your product has its own alert service and users should start an alert from a line, a zone or a price. Everything on this page is imported from `@titancharts/react`.

## Turning the buttons on

Both buttons are off by default.

```tsx
<TitanChart
  settings={{ showDrawingAlertButton: true, showPriceAxisAlertButton: true }}
  onReady={(chart) => {
    chart.on('drawing:alert', (event) => openDrawingAlertForm(event));
    chart.on('price-axis:alert', (event) => openPriceAlertForm(event));
  }}
/>
```

## The price axis bell

* `settings.showPriceAxisAlertButton` adds a bell to the price axis that follows the pointer, on the price pane and on the lower panes. A click emits [`price-axis:alert`](#price-axis-alert-payload) with the value under the bell.
* The chart reads it only when it is created. It has no runtime or saved value. To change it, create a new chart; in React, remount `<TitanChart>` with a new `key`.
* The bell hides while a drawing tool is armed and while a drawing is being drawn, is selected or is dragged.

### Price axis alert payload

`price-axis:alert` fires when the price axis bell is clicked. The payload is an `IPriceAxisAlertEvent`:

| Field | Type | Description |
|---|---|---|
| `price` | `number` | The pane's value under the bell: a price on the price pane, the indicator's value on a lower pane. |
| `paneId` | `string` | `'main'`, or the lower pane's indicator instance ID. |
| `symbol` | `string` | The chart's symbol. |
| `lastPrice` | `number \| undefined` | The latest close; `0` with no data. On the price pane only. |
| `direction` | `'above' \| 'below' \| undefined` | `'above'` when `price` is at or above `lastPrice`. On the price pane only. |
| `market` | `Market` | The chart's market. |
| `interval` | `Interval` | The chart's interval. |
| `anchorRect` | `{ top: number; left: number; width: number; height: number }` | The bell's box in viewport CSS pixels, to anchor a popover. |

On a lower pane the event carries that pane's `paneId` and the indicator's value under the bell, with no `lastPrice` or `direction`.

### Showing a price alert

An alert set from the price axis has no drawing to carry it, so draw it as a [reference line](references.md) that your application owns. Give the line the alert's ID, its price and a pill. Make it `draggable` so the user can move the alert, and open your own popover on a click to edit or remove it.

```typescript
chart.on('price-axis:alert', async ({ price, paneId, anchorRect }) => {
  if (paneId !== 'main') return;
  const alert = await openPriceAlertForm({ price, anchorRect }); // your dialog and your backend
  if (alert) {
    chart.references.add({ id: alert.id, price: alert.price, text: 'Alert', lineStyle: 'dashed', draggable: true });
  }
});

chart.on('reference:drag_end', async ({ id, price, originalPrice }) => {
  const saved = await saveAlertPrice(id, price); // your backend
  chart.references.update(id, { price: saved ? price : originalPrice });
});

chart.on('reference:click', ({ id, clientX, clientY }) => openAlertMenu(id, clientX, clientY));
```

Reference lines are yours to keep: they are not saved with the chart, and a market change clears them. When the chart loads or the market changes, add the lines for the alerts your application holds on that market, for example with `chart.references.restore(...)`. See [Interactive Mode](references.md#interactive-mode) and [Lifecycle](references.md#lifecycle).

## React: onAddAlert

`chartFeatures.onAddAlert` handles every place a user asks for an alert with one callback: the bell in the chart menu's price card, the price axis bell on the price pane, and every `drawing:alert` (the drawing Alert button and its **Edit alert**, the chart menu's **Add alert on {name}…**, and `Alt + A`). With it, the chart listens to `drawing:alert` and `price-axis:alert` for you, so the drawing Alert button shows with no other code. The price card's bell needs only the callback; the price axis bell and the drawing Alert button still need their settings.

```tsx
import { TitanChart, type IAddAlertContext } from '@titancharts/react';

function onAddAlert(ctx: IAddAlertContext) {
  if (ctx.source === 'drawing' && ctx.drawingAlert) {
    // The drawing and every price it stands for; `alert` is set to edit an alert it carries.
    openDrawingAlertForm(ctx.drawingAlert);
    return;
  }
  // The chart menu's price-card bell or the price axis bell.
  openPriceAlertForm({ symbol: ctx.symbol, price: ctx.price, direction: ctx.direction });
}

export function Chart() {
  return (
    <TitanChart
      settings={{ showDrawingAlertButton: true, showPriceAxisAlertButton: true }}
      chartFeatures={{ onAddAlert }}
    />
  );
}
```

* `ctx.source` says where the user asked: `'contextMenu'`, `'priceAxis'` or `'drawing'`.
* For a drawing, `price` is the watched price nearest `lastPrice`: a level's price, a zone's nearer edge, the nearest of a set of levels, or a sloped line's price at the latest bar. `direction` is `'above'` when `price` is at or above `lastPrice`. `drawingAlert` carries the whole `drawing:alert` payload.
* A bell on a lower pane reads an indicator's value, not a price, so it does not call `onAddAlert`. The chart menu's price card shows its bell on the price pane only, and on a lower pane offers Copy price alone. Listen to `price-axis:alert` for the price axis bell on a lower pane.
* Without `onAddAlert`, or with `chartFeatures={false}`, the chart does not listen for you.

See [Chart Features](chart-features.md#react-only-callback-items) for the `IAddAlertContext` fields.

## Plain API: events

Without React's callbacks, subscribe to the events on the `chart` handle. The drawing Alert button shows as soon as the chart has a `drawing:alert` listener and the setting is on, and a `drawing:alert-remove` listener gives it the **Edit alert** and **Remove alert** menu.

```ts
chart.updateMainChartSettings({ showDrawingAlertButton: true });

chart.on('drawing:alert', ({ id, target, market, lastPrice, anchorRect, alert }) => {
  openAlertPopover({ drawingId: id, target, symbol: market.symbol, lastPrice, anchorRect, editing: alert });
});

chart.on('drawing:alert-remove', ({ id, alert }) => {
  deleteAlert(id, alert); // then send your new set with chart.setDrawingAlerts(...)
});

// Needs settings.showPriceAxisAlertButton when the chart is created.
chart.on('price-axis:alert', ({ price, paneId, direction, anchorRect }) => {
  if (paneId === 'main') {
    openPriceAlertPopover({ price, direction, anchorRect });
  } else {
    openIndicatorAlertPopover({ instanceId: paneId, value: price, anchorRect });
  }
});
```

## Drawing alerts

A selected drawing asks for an alert on the prices it stands for, and shows the alerts your application holds on it.

### The Alert button

* `settings.showDrawingAlertButton` puts an **Alert** button, a bell, on the toolbar of a selected drawing that can carry an alert (see [Alert Button](tool-widget.md#alert-button)). Its tooltip reads **Add alert on {tool}**. This setting applies when the chart is created and is never saved.
* `chart.updateMainChartSettings({ showDrawingAlertButton })` turns it on or off at runtime. A value set this way is saved with the chart in `getState().mainChartSettings`, and `restoreState()` brings it back. Until a value is saved, the chart follows your `settings`. `resetMainChartSettings()` clears the saved value.
* The button shows only while the chart has a `drawing:alert` listener. A saved chart with the button on shows no button in an application without an alert flow. In React, [`onAddAlert`](#react-onaddalert) counts as a listener.
* A click emits [`drawing:alert`](#drawing-alert-payload). On a drawing that carries your alert, the click edits or removes it; see [Editing and removing an alert](#editing-and-removing-an-alert).
* Where the button shows, the user can ask the same way without it: **Add alert on {name}…** in the drawing's [chart menu](chart-features.md#right-clicking-a-drawing), and `Alt + A` on the selected drawing, rebindable as the `addAlert` command in [Keyboard Shortcuts](keyboard-shortcuts.md). Each emits `drawing:alert` as a click does.

### Drawings that carry an alert

A drawing can carry an alert when it is on the price pane and its tool is one of these:

| Drawing | `target.kind` | What it watches |
|---|---|---|
| Horizontal Line, Horizontal Ray | `level` | Its price. |
| Trend Line, Ray, Extended Line, Info Line, Trend Angle, when flat | `level` | The price both ends share. |
| The same lines, when sloped | `slopedLevel` | The line, read at the latest bar. |
| Rectangle | `zone` | Its top and bottom prices; its left and right edges are ignored. |
| Parallel channel (`parallelLine`), when both lines are flat | `zone` | The prices of its two lines. |
| Parallel channel, when sloped | `slopedZone` | Both lines, read at the latest bar, while the parallel line spans the first line's times. |
| Fibonacci Retracement | `levels` | Each enabled level, at the price the drawing draws it. |
| Long Position, Short Position | `levels` | Its entry, target and stop. |

A Ray whose second point is before its first, so that it points back in time, carries no alert. A sloped line or channel needs bars on the chart. With no bars, its prices cannot be read, and it carries no alert. The drawing Alert button is not offered on a multi-spline chart.

### Drawing alert payload

`drawing:alert` fires when the user asks for an alert on a drawing: a click on its Alert button or on the button's **Edit alert**, **Add alert on {name}…** in the chart menu, or `Alt + A` on the selected drawing. It also fires when you call `requestDrawingAlert()`. The payload is an `IDrawingAlertEvent`:

| Field | Type | Description |
|---|---|---|
| `id` | `string` | The drawing's ID, as `getDrawings()` and the `drawing:*` events name it. |
| `tool` | `ToolbarTool` | The drawing's tool. |
| `drawing` | `IDrawingInfo` | The drawing, as `drawing:settings` carries it. |
| `target` | `IDrawingAlertTarget` | The prices to watch (below). |
| `market` | `Market` | The chart's market. |
| `interval` | `Interval` | The chart's interval. |
| `lastPrice` | `number` | The latest close; `0` with no data. |
| `anchorRect` | `{ top: number; left: number; width: number; height: number } \| undefined` | The button's box, or the box passed to `requestDrawingAlert()`, in viewport CSS pixels, to anchor a popover. Absent when no box was given. |
| `alert` | `IDrawingAlertState \| undefined` | The drawing's entry from your last `setDrawingAlerts()` call, when it carries your alert: the user asks to edit that alert. Absent for a drawing with no alert. |

`target` holds the drawing's prices, worked out from its points and settings as the chart draws them:

```ts
type IDrawingAlertTarget =
  | { kind: 'level'; price: number; direction: 'above' | 'below' }
  | { kind: 'zone'; upper: number; lower: number; lastPriceInside: boolean }
  | { kind: 'slopedLevel'; price: number; lastBarTime: number; perBar: number;
      scale: 'linear' | 'log'; direction: 'above' | 'below' }
  | { kind: 'slopedZone'; upper: number; lower: number; lastBarTime: number; perBar: number;
      scale: 'linear' | 'log'; lastPriceInside: boolean }
  | { kind: 'levels'; levels: IDrawingAlertLevel[] };

interface IDrawingAlertLevel {
  id: string;        // 'fib:0.618' (the level's ratio as a number, whatever the label shows), 'entry', 'target' or 'stop'
  price: number;
  ratio?: number;    // The Fibonacci ratio; absent on a position's levels
  direction: 'above' | 'below';
}
```

* `direction` is the way price must move from the last price to reach the level: `'above'` while the level is at or above it.
* `lastPriceInside` says whether the last price is in the zone now, its edges included.
* A sloped target is the line read at the latest bar. `price` (or `upper` and `lower`) is its price at that bar, `lastBarTime` is that bar's time in ms, and `perBar` is its change per bar. With `scale: 'linear'`, the line is at `price + perBar * n` after `n` bars. On a logarithmic price axis (`scale: 'log'`), `perBar` is in log10 of the price, and the line is at `price * 10 ** (perBar * n)`.
* A level's `id` names the level, never its price, so it stays the same while the drawing moves. A Fibonacci level's id carries its ratio as a number, such as `'fib:0.618'`, even when the drawing labels the level in percent.
* Fibonacci levels come in ratio order. A level at or below zero is left out.

### Editing and removing an alert

On a drawing in your `setDrawingAlerts()` set, the Alert button shows the alert as on, and a click works on that alert:

* While the chart has a `drawing:alert-remove` listener, or React's [`onRemoveAlert`](#react-onremovealert), the click opens a menu under the button with **Edit alert** and **Remove alert**. **Edit alert** emits `drawing:alert` with `alert` set. **Remove alert** emits `drawing:alert-remove`.
* With no such listener, the click emits `drawing:alert` with `alert` set at once.

A `drawing:alert` that carries `alert` asks to edit that alert, not to add one. The chart menu's **Add alert on {name}…** and `Alt + A` carry it too.

### Remove alert payload

`drawing:alert-remove` fires when the user picks **Remove alert** in the Alert button's menu, on a drawing in your `setDrawingAlerts()` set. The payload is an `IDrawingAlertRemoveEvent`:

| Field | Type | Description |
|---|---|---|
| `id` | `string` | The drawing's ID. |
| `tool` | `ToolbarTool` | The drawing's tool. |
| `drawing` | `IDrawingInfo` | The drawing, as `drawing:settings` carries it. |
| `alert` | `IDrawingAlertState` | The drawing's entry from your last `setDrawingAlerts()` call. |
| `market` | `Market` | The chart's market. |
| `interval` | `Interval` | The chart's interval. |

The chart changes nothing itself: delete the alert in your application, then send the new set with `setDrawingAlerts()`.

### Asking from your own UI

`chart.requestDrawingAlert(id, anchorRect?)` emits `drawing:alert` for a drawing whether or not its button shows, for example from your own menu. It returns `false` and emits nothing for an unknown ID, or for a drawing that carries no alert: another tool, a drawing on a lower pane, a Ray that points back in time, or a sloped line with no bars.

```ts
const asked = chart.requestDrawingAlert(drawingId, menuRowElement.getBoundingClientRect());
```

`chart.getDrawingAlertTarget(id)` reads the `target` that `drawing:alert` would carry for a drawing now, with no event and no alert flow. Read it to work out a watched level again after the drawing moved or the interval changed, since a sloped line's `perBar` and `lastBarTime` follow the bars, then send your new set with `setDrawingAlerts()`. It returns `null` where `requestDrawingAlert()` returns `false`.

```ts
chart.on('drawing:update', ({ id }) => {
  if (!myAlerts.has(id)) return;
  const target = chart.getDrawingAlertTarget(id);
  if (target) myAlerts.retarget(id, target); // then call chart.setDrawingAlerts(...)
});
```

### React: onRemoveAlert

`chartFeatures.onRemoveAlert` takes **Remove alert** from the Alert button's menu. It receives the [`drawing:alert-remove`](#remove-alert-payload) payload, an `IDrawingAlertRemoveEvent`. With it, the chart listens to `drawing:alert-remove` for you, so the Alert button opens its **Edit alert** and **Remove alert** menu on a drawing that carries your alert.

```tsx
import { TitanChart, type IDrawingAlertRemoveEvent, type ITitanChartAPI } from '@titancharts/react';

let chart: ITitanChartAPI | null = null;

function onRemoveAlert({ id }: IDrawingAlertRemoveEvent) {
  myAlerts.delete(id);
  chart?.setDrawingAlerts(myAlerts.entries()); // the chart changes nothing itself
}

export function Chart() {
  return (
    <TitanChart
      settings={{ showDrawingAlertButton: true }}
      onReady={(api) => { chart = api; }}
      chartFeatures={{ onAddAlert, onRemoveAlert }}
    />
  );
}
```

### Showing your alert state on drawings

`chart.setDrawingAlerts(entries)` shows the alerts your application holds on the drawings that carry them. Each call sends the full set and replaces the last one; `[]` clears it.

```ts
chart.setDrawingAlerts([
  { id: 'tool_12', levels: [97250], tooltip: ['Crosses 97,250'], distanceLabel: '{pct}% away' },
  { id: 'tool_15', tooltip: ['Leaves the channel'], count: 2 }, // a sloped channel: no levels
]);
```

Each entry is an `IDrawingAlertState`:

| Field | Type | Description |
|---|---|---|
| `id` | `string` | The drawing's ID. |
| `levels` | `number[] \| undefined` | The prices the alert watches now. Leave it out for a sloped line or channel: the chart reads the line at the latest bar. |
| `tooltip` | `string[] \| undefined` | Lines shown in the bell's tooltip. |
| `distanceLabel` | `string \| undefined` | The label drawn near a watched level, with a `{pct}` placeholder, such as `'{pct}% away'`. Absent or `''`: no label. |
| `pending` | `boolean \| undefined` | Your application is saving a change to the alert, for example after the user moved the drawing: the bell is hollow and the dashes hold still. |
| `count` | `number \| undefined` | How many alerts the drawing carries, for the Alert button's tooltip: **1 alert on this drawing** or **{count} alerts on this drawing**. Default `1`. |

* Each drawing in the set gets a bell that moves with it, and a bell tag on the price axis at each watched level. It also gets the distance label when you give one. The label shows only while price is within 0.8% of the nearest watched level: it fades in from 0.8% and is fully shown from 0.5%, and `{pct}` prints the distance to two decimals, such as `0.42% away`. A level further off shows no label. The label reads the distance to the nearest level on either side, so a level price has crossed keeps its label, at the distance price has moved past it, until your set drops the alert or the level. A pending alert shows no label. A line draws heavier, and a line that stops short of the chart's right edge gets dashes that carry it on to the edge. A rectangle's border marches. Other drawings, such as an ellipse, keep their usual stroke.
* The drawing's Alert button shows the alert as on: the bell rings on a filled background, with the tooltip **1 alert on this drawing** or **{count} alerts on this drawing**, and the button carries `data-state="on"`. A click edits or removes the alert; see [Editing and removing an alert](#editing-and-removing-an-alert).
* A removed or hidden drawing loses its decoration on the chart. A drawing hidden from its toolbar's **More** menu stays selected, and its toolbar keeps the Alert button, shown as on while your set holds the drawing.
* Removing a drawing leaves your set as it is. Drop the drawing's alert on `drawing:remove` and send the set again without it. A `restoreState()` removes every drawing and adds it back with the same ID, so ignore `drawing:remove` while your own `restoreState()` call is pending. Undo brings a removed drawing back with the same ID, without the alert you dropped. To confirm with the user before a drawing that carries an alert goes, pass `onBeforeRemoveDrawings`; see [Asking before the user removes drawings](drawing-tools.md#asking-before-the-user-removes-drawings).
* Send the set again when the symbol changes. Drawings belong to the chart, not to a symbol, so a drawing's Alert button keeps showing the previous symbol's alert until you call `setDrawingAlerts()` again.
* The set is never saved with the chart: `getState()` does not carry it, so a chart opened from a saved state shows no alert until you call `setDrawingAlerts()`. A `restoreState()` on the same chart keeps the set.

### Effects

`chart.playDrawingAlertEffect(id, effect, at?)` plays a one-shot effect on a drawing that carries an alert:

| Effect | When to play it |
|---|---|
| `'arm'` | The alert was created or turned on. |
| `'cross'` | Price met the drawing. `at` (`{ time?, price? }`) places it; by default it plays at the watched level at the latest bar. |
| `'off'` | The alert was turned off. Play it with the `setDrawingAlerts()` call that drops the drawing. |
| `'saved'` | A change to the alert was saved. |

With reduced motion, the state change shows at once.

## Types

| Type | Shape | Where |
|---|---|---|
| `IDrawingAlertEvent` | `{ id, tool, drawing, target, market, interval, lastPrice, anchorRect?, alert? }` | The `drawing:alert` payload. |
| `IDrawingAlertRemoveEvent` | `{ id, tool, drawing, alert, market, interval }` | The `drawing:alert-remove` payload, and the argument of React's `onRemoveAlert`. |
| `IDrawingAlertTarget` | `level`, `zone`, `slopedLevel`, `slopedZone` or `levels` | `IDrawingAlertEvent.target`, and what `getDrawingAlertTarget()` returns. |
| `IDrawingAlertLevel` | `{ id, price, ratio?, direction }` | One level of a `levels` target. |
| `IPriceAxisAlertEvent` | `{ price, paneId, symbol, lastPrice?, direction?, market, interval, anchorRect }` | The `price-axis:alert` payload. |
| `IDrawingAlertState` | `{ id, levels?, tooltip?, distanceLabel?, pending?, count? }` | An entry of `setDrawingAlerts()`. |
| `DrawingAlertEffect` | `'arm' \| 'cross' \| 'off' \| 'saved'` | The effect `playDrawingAlertEffect()` plays. |

See [Types](../api-reference/types.md#alert-types) for the declarations, [Events](../api-reference/events.md#drawing-events) for every drawing event, and [Chart Methods](../api-reference/chart-methods.md#drawing-alert-methods) for the methods.
