Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Heuristic:TA Lib Ta lib python Compatibility Mode Switching

From Leeroopedia



Knowledge Sources
Domains Financial_Analysis, Debugging
Last Updated 2026-02-09 22:00 GMT

Overview

The `set_compatibility()` function switches between two computation modes (0 = default, 1 = Metastock-compatible) that produce different EMA and other indicator values.

Description

TA-Lib supports a global compatibility mode that changes how certain indicators are calculated. Mode 0 (default) follows the standard TA-Lib algorithm, while Mode 1 follows Metastock's algorithm. The difference is most noticeable in EMA (Exponential Moving Average) computation, where the initial seed value calculation differs between modes. This setting is global and affects all subsequent indicator calls until changed.

Usage

Apply this heuristic when your TA-Lib indicator values differ from reference values from platforms like Metastock, or when comparing results between different versions or configurations of TA-Lib. If you need to match Metastock output exactly, switch to compatibility mode 1. Always remember to reset the mode after use if your application mixes both modes.

The Insight (Rule of Thumb)

  • Action: Call `talib.set_compatibility(0)` for default TA-Lib behavior, or `talib.set_compatibility(1)` for Metastock-compatible behavior.
  • Value: Mode 0 (default) produces `EMA([0..9], 3) = [nan, nan, 1, 2, 3, 4, 5, 6, 7, 8]`. Mode 1 produces `EMA([0..9], 3) = [nan, nan, 1.25, 2.125, 3.0625, ...]`.
  • Trade-off: Switching mode is global and affects all threads. Always restore the mode after use to avoid unexpected side effects.

Reasoning

Different financial software platforms use slightly different algorithms for the same indicator names. The most common difference is in how the EMA seed value is computed. Metastock uses a different weighting for the initial period. TA-Lib provides the compatibility switch so users can match whichever platform they are comparing against.

Code evidence from `talib/_common.pxi:102-109`:

def _ta_set_compatibility(value):
    cdef TA_RetCode ret_code
    ret_code = lib.TA_SetCompatibility(value)
    _ta_check_success('TA_SetCompatibility', ret_code)

def _ta_get_compatibility():
    return lib.TA_GetCompatibility()

Test demonstrating both modes from `tests/test_func.py:66-77`:

def test_compatibility():
    a = np.arange(10, dtype=float)
    talib.set_compatibility(0)
    r = func.EMA(a, 3)
    assert_array_equal(r, [np.nan, np.nan, 1, 2, 3, 4, 5, 6, 7, 8])
    talib.set_compatibility(1)
    r = func.EMA(a, 3)
    assert_array_equal(r, [np.nan, np.nan, 1.25, 2.125, 3.0625,
                           4.03125, 5.015625, 6.0078125,
                           7.00390625, 8.001953125])
    talib.set_compatibility(0)

Note how the test explicitly restores compatibility mode to 0 at the end, demonstrating the best practice of always resetting global state.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment