# 13. Built-in input processors

## What a built-in input is

An input turns every tick into a number, and until now that number came from a callable you wrote: the four builders of `tse_input.h` take a function pointer and an opaque pointer, and the engine calls them without looking inside (the Input chapter). That remains the general case, and nothing about it changes. `tse_input_processor.h` adds the common case: nine processors the library ships, declared rather than written.

A built-in input is a **first-class input kind**, not a convenience wrapper around the callback form. The distinction has one visible consequence, and it is the reason the header exists: a robot whose inputs are built-in is **savable without registering anything**. A function pointer cannot be serialized, so a savable custom processor has to be registered under a string key, and the loading process has to register the same key before `tse_load` (the environment chapter). A built-in processor is described completely by four values — which processor, which tick field it reads, its window length, and its smoothing coefficient — and the recipe stores those four, so the library rebuilds the processor by itself at load time, in any of the three languages, with nothing registered on either side.

The second consequence is performance, and it is invisible: a built-in processor is engine code called directly, with no crossing of the language boundary per tick and no indirection through a user pointer.

## The nine processors

| Value | Processor | What it computes | Feeds |
| --- | --- | --- | --- |
| 1 | `tse_input_sma` | simple moving average of the named field | all three |
| 2 | `tse_input_ema` | exponential moving average, smoothed by `alpha` | all three |
| 3 | `tse_input_wma` | linearly weighted moving average | all three |
| 4 | `tse_input_volatility` | sample standard deviation over the window | all three |
| 5 | `tse_input_historical_volatility` | the same over logarithmic increments | all three |
| 6 | `tse_input_atr` | average true range | OHLCV only |
| 7 | `tse_input_parkinson_volatility` | the Parkinson volatility estimator | OHLCV only |
| 8 | `tse_input_rolling_median` | median over the window | all three |
| 9 | `tse_input_rolling_robust_z_score` | robust $z$ score: deviation from the rolling median, scaled by the median absolute deviation | all three |

`tse_input_undefined` (0) is the guard and is refused. `tse_input_type_to_string` names any value of the enumeration, and answers `"unknown"` for a value outside it — the one place on this surface where an out-of-range value is answered rather than refused, because naming a value is not acting on it.

Seven of the nine read the single field they are given. The remaining two do not: the average true range needs the high, the low and the previous close of the same bar, and the Parkinson estimator needs the high and the low of one bar, so they read those members directly and **ignore** the `field` argument — which is also why they are accepted by the OHLCV builder alone.

All nine are incremental: they hold a window and update it per tick rather than recomputing it, so the cost per tick does not grow with the window length.

## The three builders

There are three builders, not nine and not six: the processor is a parameter, and the builder is chosen by the **feed**, because the feed fixes the record shape the processor reads.

```c
TSE_API TseStatus tse_add_input_ohlcv_builtin
(
	TseAccountHandle   account,
	char const*        label,
	int32_t            dataLength,
	TseDuration        duration,
	TseInputType       processor,
	TseTickField       field,
	size_t             period,
	double             alpha,
	TseMarketHandle    market,
	int                coreId,
	TseContractHandle const* contracts,
	size_t             contractCount
);
```

`tse_add_input_bidask_builtin` and `tse_add_input_trade_builtin` take exactly the same arguments. Every argument before `processor` and every argument after `alpha` means what it means in `tse_add_input_ohlcv` — the label the rest of the graph refers to the input by, the cache length, the explicit duration, the adapter the input reads from, the mandatory core id and the contract subset that binding registers on the adapter. The four in the middle are the built-in description:

| Argument | Read by | Meaning |
| --- | --- | --- |
| `processor` | always | which of the nine |
| `field` | the seven single-field processors | which member of the record; its high nibble must name the builder's own feed |
| `period` | always | the window length, in observations |
| `alpha` | `tse_input_ema` alone | the smoothing coefficient |

Neither `period` nor `alpha` is validated at the boundary: both are handed to the processor as given. The executed feed takes no built-in processor at all — it has no builder here — and the book feed keeps its own two builders in `tse_book.h`.

## What is refused

Every refusal is a `tse_error` with text from `tse_last_error(account)`, and each one names the fact rather than the argument position.

| Refused | Text |
| --- | --- |
| `tse_input_undefined` as the processor | the built-in input processor is undefined |
| a `TseInputType` outside the enumeration | an unknown TseInputType value |
| `tse_field_undefined` as the field | the tick field is undefined |
| a field whose high nibble names another feed | the tick field names a market-data kind other than the one this input reads |
| a field whose high nibble names no feed at all | the tick field names no market-data kind |
| an OHLCV field value above the five | the tick field is not an OHLCV field |
| a bid/ask field value above the five | the tick field is not a bid-ask field |
| any field but the price on the trade builder | the trade feed carries one value, its price; no other field can be taken from it |
| `tse_input_atr` on a feed other than OHLCV | the average true range needs the high, the low and the previous close, which only the OHLCV feed carries |
| `tse_input_parkinson_volatility` on a feed other than OHLCV | the Parkinson volatility needs the high and the low of one bar, which only the OHLCV feed carries |
| an empty contract list | at least one contract must be specified; empty contract list is not allowed |

The ordinary input refusals apply unchanged: a null account, label, adapter or symbol pointer, a label longer than the capacity the appendix states, a symbol no contract was declared under, a duplicate input label, and an adapter whose market-data type does not match the builder.

## In use

A simple moving average over the close of two contracts, declared rather than written, and a robot that saves without a registered key:

```c
tse_add_input_ohlcv_builtin
(
	account, "SMA3", 16, tse_duration_minutes,
	tse_input_sma, tse_field_ohlcv_close, 3u, 0.0,
	market, -1, symbols, 2u
);
```

The smoothing coefficient is passed as zero above because the simple moving average does not read it; the exponential one does:

```c
tse_add_input_ohlcv_builtin
(
	account, "EMA9", 16, tse_duration_minutes,
	tse_input_ema, tse_field_ohlcv_close, 9u, 0.2,
	market, -1, symbols, 2u
);
```

## C++ and Python

### C++

The three builders are methods of `tse::Account` — `addInputOhlcvBuiltIn`, `addInputBidAskBuiltIn`, `addInputTradeBuiltIn` — and the enumeration is `tse::InputType` with the members `sma`, `ema`, `wma`, `volatility`, `historicalVolatility`, `atr`, `parkinsonVolatility`, `rollingMedian`, `rollingRobustZScore`. The field is a `tse::TickField`, as everywhere in the wrapper. Note one difference from C in the argument order: the C++ methods take the core id **last**, after the contract symbols, following the convention of the other `addInput*` methods. Refusals throw `tse::Error` carrying the same text.

```cpp
account.addInputOhlcvBuiltIn
(
	"SMA3", 16, tse::Duration::minutes,
	tse::InputType::sma, tse::TickField::ohlcvClose, 3u, 0.0,
	market, {"WTI", "BRENT"}, -1
);
```

### Python

The same three methods are `Account.add_input_ohlcv_builtin`, `add_input_bidask_builtin` and `add_input_trade_builtin`, with the core id last as in C++, and the enumeration is `tse.InputType` with the members `SMA`, `EMA`, `WMA`, `VOLATILITY`, `HISTORICAL_VOLATILITY`, `ATR`, `PARKINSON_VOLATILITY`, `ROLLING_MEDIAN`, `ROLLING_ROBUST_Z_SCORE`. Refusals raise `tse.TseError`.

```python
account.add_input_ohlcv_builtin(
    "SMA3", 16, tse.Duration.Minutes,
    tse.InputType.SMA, tse.TickField.OHLCV_CLOSE, 3, 0.0,
    market, ["WTI", "BRENT"], -1
)
```
