# MyTrader User-Defined Indicators (UDI) Documentation

> ⚠️ **Required reading for every indicator: pages 001–005, in full, before writing any code.** This applies whatever the indicator does — including one that only creates drawings, event markers or bar highlights, or only shows HTML. Together these pages cover the class, the object `onInit()` must return (`plots[]`, `settingsFields[]`), `onCalculate()`, the lifecycle, and what happens when the market, timeframe or settings change. Every later page assumes them and does not repeat them.
>
> The page for your specific task comes *after* these, not instead of them. The [Quick Lookups](#quick-lookups) table below is for questions that come up once you have read them.

---

## Core Concepts & Setup — required reading

- **[001-introduction.md](https://api.fxblue.com/mytrader/udi/markdown/001-introduction.md)** — **Required.** Lifecycle (when each function is called), common pitfalls, what UDIs can do, how to add them to charts.
- **[002-myindicator-class.md](https://api.fxblue.com/mytrader/udi/markdown/002-myindicator-class.md)** — **Required.** Class structure and the UserDefinedIndicator base class.

## Writing Indicators — required reading

- **[003-initialization-oninit.md](https://api.fxblue.com/mytrader/udi/markdown/003-initialization-oninit.md)** — **Required.** `onInit()` function: the return object every indicator needs, defining plots (including none), settings fields, context data.
- **[004-calculating-data-oncalculate.md](https://api.fxblue.com/mytrader/udi/markdown/004-calculating-data-oncalculate.md)** — **Required.** `onCalculate()` function, input data, efficiency patterns, technical analysis (FXB.ta library).
- **[005-chart-changes.md](https://api.fxblue.com/mytrader/udi/markdown/005-chart-changes.md)** — **Required.** `onContextChange()` and `onParameterChange()` handlers.

## Visual Output & Interaction

- **[006-drawings-markers-highlights.md](https://api.fxblue.com/mytrader/udi/markdown/006-drawings-markers-highlights.md)** — Creating drawings (rectangles, lines, channels etc.), event markers, bar highlights. Includes all drawing types and properties.
- **[007-notifications.md](https://api.fxblue.com/mytrader/udi/markdown/007-notifications.md)** — Toast notifications, background color changes.
- **[008-html-in-chart.md](https://api.fxblue.com/mytrader/udi/markdown/008-html-in-chart.md)** — Embedding HTML iframes, bidirectional messaging between HTML and UDI.

## Trading & Account Integration

- **[009-deal-ticket-and-trade.md](https://api.fxblue.com/mytrader/udi/markdown/009-deal-ticket-and-trade.md)** — Recommended trades, opening the deal ticket, order definitions.
- **[010-account-order-data.md](https://api.fxblue.com/mytrader/udi/markdown/010-account-order-data.md)** — Subscribing for account metrics and order data without UDIX access.

## Advanced Topics

- **[011-other-topics.md](https://api.fxblue.com/mytrader/udi/markdown/011-other-topics.md)** — External data collection (XMLHttpRequest, CORS), date/timezone conversion, multi-timeframe indicators, remote C#/Python indicators.
- **[012-legacy-code.md](https://api.fxblue.com/mytrader/udi/markdown/012-legacy-code.md)** — Legacy UDI format (UDI namespace vs. MyIndicator class), migration guide.

## Reference

- **[A10-examples-reference.md](https://api.fxblue.com/mytrader/udi/markdown/A10-examples-reference.md)** — Complete list of 21 example UDI files with descriptions and links. Browse by pattern: basics, histograms, plot variants, drawings, event markers, bar highlights, HTML, multi-timeframe, capstone.

---

## Indicator Calculations: FXB.ta and FXB.CandleStore

⚠️ **These are not documented in this guide.** They are shared with the MyTrader scripting framework and documented there, in **[§5 Working with candle data](https://api.fxblue.com/mytrader/scripting/markdown/242-5-working-with-candle-data.md)**. That section states explicitly that its features can be used from any UDI, without framework access and without turning it into a UDIX, and that it can be treated as part of the UDI developer reference.

Read it. Almost every real indicator needs it, and §5 links onwards to everything below it:

- **[§5.2 FXB.ta](https://api.fxblue.com/mytrader/scripting/markdown/251-5.2-fxb-ta-technical-analysis-library.md)** — moving averages, ATR, RSI, MACD, Bollinger bands and ~70 others. The [full list with parameters](https://api.fxblue.com/mytrader/scripting/markdown/263-5.2.8-list-of-technical-analysis-calculations.md) is §5.2.8; moving averages are listed separately at §5.2.7.
- **[§5.1 FXB.CandleStore](https://api.fxblue.com/mytrader/scripting/markdown/243-5.1-the-fxb-candlestore-object.md)** — candle storage, and aggregation to higher timeframes.

---

## For LLMs Building UDIs

⚠️ **Before writing code:**
- **Read 001–005 first**, whatever you are building — see the box at the top of this page. Do this before fetching the page for your specific task, and before §5. If you have not read one of them, say so rather than treating something as undocumented.
- The class **must** be named `MyIndicator`. A descriptive name is allowed only if the file ends with an alias: `class MyIndicator extends SimpleMovingAverage {}`. Without it, nothing runs and nothing is reported — the chart just stays empty.
- Modern format uses `class MyIndicator extends UserDefinedIndicator { ... }` with `this.createDrawing()` — **not** legacy `UDI.onInit = function() { ... }` format.
- Always fetch the freshly-documented API before guessing—the framework is large enough that "this seems like it should work" is unreliable.
- **Do not invent functions or properties.** If it's not in the docs, it doesn't exist — but "the docs" are two trees, not one. **`FXB.ta` and `FXB.CandleStore` are not documented in this guide**; see [Indicator Calculations](#indicator-calculations-fxbta-and-fxbcandlestore) above. Their absence from this guide is not evidence they don't exist, and is not a reason to hand-roll an EMA or an ATR.
- **Fetch the calculation's page before using it**, even if you were told about the library earlier in this session. Constructor parameters and output accessors vary between calculations — MACD has `GetHistogramValue()`, fractal bands put their second output on `GetSignalValue()`. A remembered signature is a guessed signature.
- `onInit()` must always return `plots[]` and `settingsFields[]`, even when empty. An indicator that only draws returns `plots: []` — see [003](https://api.fxblue.com/mytrader/udi/markdown/003-initialization-oninit.md#plots-array).
- Drawings cannot be created from `onInit()` — defer to first `onCalculate()` call. An indicator that only draws still implements `onCalculate()` for this reason.
- Consider `omitFromParameters: true` on every settings field, and on `yesno` (boolean) fields in particular. Otherwise the chart shows every value after the indicator's name, and users see summaries like `(10, 20, 5, true, true, false, true)`. Keep only the values that tell one copy of the indicator from another — see [Keeping the parameter summary readable](https://api.fxblue.com/mytrader/udi/markdown/003-initialization-oninit.md#keeping-the-parameter-summary-readable).
- `select` field values arrive as strings, not numbers — use `parseInt()`/`parseFloat()`.
- Dates are milliseconds in **chart time**, not UTC — convert with `convertChartDateToUTC()` / `convertUTCToChartDate()`.
- `line` plots are joined across null values, so a line plot can't show separate segments — use a `point` plot (optionally with an icon `lineStyle`) or drawings for those.

---

## Quick Lookups

| Task | File |
|------|------|
| Naming your class | [002-myindicator-class.md](https://api.fxblue.com/mytrader/udi/markdown/002-myindicator-class.md) → Required: `MyIndicator`, or an alias to it |
| Trying to plot values on the chart | [003-initialization-oninit.md](https://api.fxblue.com/mytrader/udi/markdown/003-initialization-oninit.md) → Plots array |
| Indicator with no plotted values (drawings, markers or highlights only) | [003-initialization-oninit.md](https://api.fxblue.com/mytrader/udi/markdown/003-initialization-oninit.md) → `plots[]` can be empty; then [006](https://api.fxblue.com/mytrader/udi/markdown/006-drawings-markers-highlights.md) → Where these fit in the lifecycle |
| Need a parameter the user can configure | [003-initialization-oninit.md](https://api.fxblue.com/mytrader/udi/markdown/003-initialization-oninit.md) → Settings fields |
| Keeping the parameter summary after the indicator's name short | [003-initialization-oninit.md](https://api.fxblue.com/mytrader/udi/markdown/003-initialization-oninit.md) → Keeping the parameter summary readable |
| Calculating indicators from candle data | [004-calculating-data-oncalculate.md](https://api.fxblue.com/mytrader/udi/markdown/004-calculating-data-oncalculate.md) |
| Using moving averages, ATR, RSI etc. in a UDI | [004-calculating-data-oncalculate.md](https://api.fxblue.com/mytrader/udi/markdown/004-calculating-data-oncalculate.md) → Technical analysis (FXB.ta) |
| What calculations exist, and their parameters | **Not in this guide** → [§5.2.8](https://api.fxblue.com/mytrader/scripting/markdown/263-5.2.8-list-of-technical-analysis-calculations.md) |
| The `FXB.ta` / `FXB.CandleStore` APIs | **Not in this guide** → [§5 Working with candle data](https://api.fxblue.com/mytrader/scripting/markdown/242-5-working-with-candle-data.md) |
| Drawing lines, rectangles on the chart | [006-drawings-markers-highlights.md](https://api.fxblue.com/mytrader/udi/markdown/006-drawings-markers-highlights.md) |
| Responding to user clicks on markers | [006-drawings-markers-highlights.md](https://api.fxblue.com/mytrader/udi/markdown/006-drawings-markers-highlights.md) → Event markers |
| Putting a button or form in the chart | [008-html-in-chart.md](https://api.fxblue.com/mytrader/udi/markdown/008-html-in-chart.md) |
| Suggesting a trade or opening deal ticket | [009-deal-ticket-and-trade.md](https://api.fxblue.com/mytrader/udi/markdown/009-deal-ticket-and-trade.md) |
| Reading account balance, open positions | [010-account-order-data.md](https://api.fxblue.com/mytrader/udi/markdown/010-account-order-data.md) |
| Fetching external data (calendar, signals) | [011-other-topics.md](https://api.fxblue.com/mytrader/udi/markdown/011-other-topics.md) → External data |
| Multi-timeframe without time-travelling | [011-other-topics.md](https://api.fxblue.com/mytrader/udi/markdown/011-other-topics.md) → Multi-timeframe indicators |
| Converting between UTC and chart time | [011-other-topics.md](https://api.fxblue.com/mytrader/udi/markdown/011-other-topics.md) → Date conversion |

---

**Framework access:** Without UDIX (framework access), UDIs can plot, draw, and suggest trades. With UDIX, they can place trades, cancel orders, and more. See [001-introduction.md](https://api.fxblue.com/mytrader/udi/markdown/001-introduction.md) § 1.1 for the distinction.

**Examples:** 21 example files walk through basics, histograms, drawings, HTML, multi-timeframe, and more. See [A10-examples-reference.md](https://api.fxblue.com/mytrader/udi/markdown/A10-examples-reference.md) for the full list.
