All reference chapters

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

ValueProcessorWhat it computesFeeds
1tse_input_smasimple moving average of the named fieldall three
2tse_input_emaexponential moving average, smoothed by alphaall three
3tse_input_wmalinearly weighted moving averageall three
4tse_input_volatilitysample standard deviation over the windowall three
5tse_input_historical_volatilitythe same over logarithmic incrementsall three
6tse_input_atraverage true rangeOHLCV only
7tse_input_parkinson_volatilitythe Parkinson volatility estimatorOHLCV only
8tse_input_rolling_medianmedian over the windowall three
9tse_input_rolling_robust_z_scorerobust $z$ score: deviation from the rolling median, scaled by the median absolute deviationall 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.

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:

ArgumentRead byMeaning
processoralwayswhich of the nine
fieldthe seven single-field processorswhich member of the record; its high nibble must name the builder's own feed
periodalwaysthe window length, in observations
alphatse_input_ema alonethe 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.

RefusedText
tse_input_undefined as the processorthe built-in input processor is undefined
a TseInputType outside the enumerationan unknown TseInputType value
tse_field_undefined as the fieldthe tick field is undefined
a field whose high nibble names another feedthe tick field names a market-data kind other than the one this input reads
a field whose high nibble names no feed at allthe tick field names no market-data kind
an OHLCV field value above the fivethe tick field is not an OHLCV field
a bid/ask field value above the fivethe tick field is not a bid-ask field
any field but the price on the trade builderthe trade feed carries one value, its price; no other field can be taken from it
tse_input_atr on a feed other than OHLCVthe 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 OHLCVthe Parkinson volatility needs the high and the low of one bar, which only the OHLCV feed carries
an empty contract listat 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:

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:

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.

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.

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

Version 6.0.0.0. Also as Markdown: this chapter, the whole reference in one file, and the llms.txt map.