← warin.me · Data Science and Engineeringภาษาไทย

FINANCIAL DATA ENGINEERING · FEATURE LAB

An indicator is a measurement rule—not a prediction and not a trading decision.

This lab turns prices, volume, quotes, order books and benchmark series into versioned model features. The focus is not memorizing indicators, but defining time, adjustment, window, availability and cost so the same feature can be reproduced honestly.

01

Define

State price field, clock, window and adjustment.

02

Compute

Build rolling and cross-sectional features safely.

03

Validate

Detect leakage, stale quotes and corporate-action breaks.

04

Store

Version reusable observations for research and models.

01 · FROM MARKET EVENT TO FEATURE

The calculation begins only after the observation is made trustworthy.

Ticker is not a permanent identity, close may be adjusted or raw, timestamps may describe event or arrival, and one market may not represent the whole order book.

RAW MARKET DATAtrade · quote · OHLCV · corporate action
NORMALIZEinstrument ID · timezone · calendar · currency
ADJUST & ALIGNsplit/dividend policy · event/available time
FEATUREvalue · as_of · window · version
Entityinstrument_id + venue
Observationfeature_timestamp
Availabilityavailable_at ≤ decision_time
Versionformula + parameters + adjustment

02 · FIFTEEN CORE FINANCIAL FEATURES

Measure direction, variability, activity, liquidity and relative risk separately.

Features overlap. RSI, momentum and MACD may carry related information; spread and Amihud illiquidity describe different liquidity aspects. Correlation and ablation tests should decide what remains—not indicator popularity.

RETURN & MOMENTUM

01

Log return

rₜ = ln(Pₜ / Pₜ₋₁)

One-period continuously compounded return; use a declared adjusted-price policy.

Window 1 · O(T)
02

Rate of Change / Momentum

ROC_w = Pₜ/Pₜ₋w − 1

Price displacement over w observations; observation count differs from calendar days.

Window w · O(T)
03

RSI

100 − 100/(1 + avg_gain/avg_loss)

Bounded transformation of smoothed gains and losses. Initialization and smoothing convention must be versioned.

Typical w=14 · incremental O(T)
04

MACD

EMA_fast − EMA_slow

Difference between two exponential trends; signal and histogram require another smoothing definition.

Typical 12/26/9 · O(T)

TREND & POSITION

05

SMA/EMA distance

Pₜ / MA_w − 1

Scale-free distance from a rolling trend, preferable to raw price differences across instruments.

w=20/50/200 · O(T)
06

Bollinger z-position

(Pₜ − SMA_w) / SD_w

Position relative to a rolling mean and dispersion; bandwidth can be stored separately.

w=20 · O(T)
07

Drawdown

Pₜ / max(P₁…Pₜ) − 1

Decline from the running peak under a stated adjusted-price and start-date policy.

Expanding · O(T)

VOLATILITY & RANGE

08

Realized volatility

SD(rₜ₋w+1…rₜ) × annualization

Historical return dispersion. Annualization requires a declared frequency/calendar assumption.

w=20/60 · O(T)
09

ATR / NATR

TR=max(H−L, |H−C₋₁|, |L−C₋₁|)

Range-based variability including gaps; normalize by price when comparing instruments.

w=14 · O(T)

VOLUME & LIQUIDITY

10

Volume z-score

(Vₜ − mean_w(V)) / sd_w(V)

Unusual activity relative to the instrument’s own history; handle zero and regime shifts.

w=20 · O(T)
11

Turnover

volume / adjusted_free_float

Trading activity relative to tradable shares; denominator changes with corporate actions.

Daily/rolling · O(T)
12

Relative bid–ask spread

(ask−bid) / mid

Top-of-book quoted liquidity at one venue and time; filter locked, crossed or stale quotes.

Quote-level · O(Q)
13

Order-book imbalance

(Σbid_size−Σask_size)/(Σbid_size+Σask_size)

Displayed depth imbalance for declared levels and venue; orders can be cancelled.

Depth L · O(L) per snapshot
14

Amihud illiquidity

mean(|return| / traded_value)

Daily price response per currency unit traded; guard against tiny or zero denominator.

Window w · O(T)

CROSS-ASSET RISK

15

Rolling beta

Cov(rᵢ,rₘ) / Var(rₘ)

Historical sensitivity to a declared benchmark, frequency and synchronized calendar.

w=60/252 · O(T)

03 · SEE THE INDICATORS AS A SYSTEM

The same price path produces several views of evidence.

ADJUSTED PRICEpricerolling meanMOMENTUMreturn · ROC · RSI · MACDTRENDMA distance · z-positionVOLATILITYSD · ATR · drawdownLIQUIDITYspread · turnover · AmihudRELATIVE RISKbeta · correlation
Figure 1. Indicators are different transformations of related observations. More indicators do not automatically create more independent information.

04 · PYTHON LAB: BUILD VERSIONED DAILY FEATURES

Shift the features before joining them to a decision made at the next observation.

This compact example uses pandas-style operations. Production code needs instrument grouping, calendar handling, stable sorting and tests for missing and duplicate observations.

import numpy as np
import pandas as pd

def build_features(df: pd.DataFrame) -> pd.DataFrame:
    x = df.sort_values(["instrument_id", "trading_date"]).copy()
    g = x.groupby("instrument_id", group_keys=False)

    x["log_return_1d"] = g["adjusted_close"].transform(
        lambda s: np.log(s / s.shift(1)))
    x["momentum_20"] = g["adjusted_close"].transform(
        lambda s: s / s.shift(20) - 1)
    x["sma_distance_20"] = x["adjusted_close"] / g["adjusted_close"].transform(
        lambda s: s.rolling(20, min_periods=20).mean()) - 1
    x["volatility_20"] = g["log_return_1d"].transform(
        lambda s: s.rolling(20, min_periods=20).std())
    x["volume_z_20"] = g["volume"].transform(
        lambda s: (s - s.rolling(20).mean()) / s.rolling(20).std())
    mid = (x["best_bid"] + x["best_ask"]) / 2
    x["relative_spread"] = (x["best_ask"] - x["best_bid"]) / mid

    feature_cols = ["log_return_1d", "momentum_20", "sma_distance_20",
                    "volatility_20", "volume_z_20", "relative_spread"]
    # Example contract: a decision at date t uses features available through t-1.
    x[feature_cols] = g[feature_cols].shift(1)
    x["feature_version"] = "daily_market_v1"
    return x
Shift is necessary—but not sufficient

You must also verify when the source became available. A closing price may be usable after market close; a revised fundamental report must use publication and revision timestamps; corporate-action-adjusted history may be recalculated later.

05 · COMPLEXITY AND MATERIALIZATION

Most rolling indicators are linear when implemented incrementally; repeating them across experiments is still expensive.

For A instruments, T observations and E experiments, rebuilding a linear feature set costs roughly O(EAT). A versioned feature build moves it toward O(AT) computation plus feature reads. Cross-sectional ranking can add sorting cost O(TA log A).

WITHOUT STORECompute ≈ E × A × T × F

20 experiments × 2,000 stocks × 2,500 days × 15 features

WITH VERSIONED FEATURESCompute ≈ A × T × F

Build once, validate once, reuse across experiments.

OFFLINE FINANCIAL FEATURE KEYinstrument_id + venue + feature_timestamp + feature_set_version
+ price_adjustment_version + calendar_version + source_snapshot

06 · LEAKAGE AND BIAS CHECKLIST

A clean formula can still create dishonest evidence.

01

Look-ahead

Using close, volume or report data before it was observable at the decision time.

02

Survivorship bias

Training only on instruments that remain listed today.

03

Corporate-action leakage

Using retrospectively adjusted prices without recording adjustment version and purpose.

04

Cross-sectional leakage

Normalizing one stock using peers that were unavailable or outside the historical universe.

05

Stale quote

Treating an old bid/ask as contemporaneous liquidity.

06

Timezone/calendar mismatch

Joining markets with different sessions by date label rather than comparable event time.

07

Multiple testing

Trying many windows and retaining only those that looked successful.

08

Licensing and redistribution

Storing or sharing derived features beyond the permitted market-data terms.

07 · STUDENT CHALLENGES

Treat each indicator as a versioned hypothesis.

01

Implement log return, momentum, rolling volatility, relative spread and Amihud illiquidity from one synthetic dataset.

02

Compare raw close and adjusted close around a simulated 2-for-1 split; explain which features break.

03

Show that naive rolling computation and incremental computation produce parity within tolerance.

04

Create a point-in-time universe that includes delisted instruments and compare with today’s survivor-only universe.

05

Measure correlations among RSI, momentum, MACD and SMA distance; remove redundant features by an explicit rule.

06

Design a feature-store contract including market-data source, venue, calendar, adjustment and availability time.

THE CENTRAL IDEA

Financial feature engineering is the discipline of reconstructing what the market data could honestly tell us at that time.