PHP code example of robertgdev / ta-lib-hybrid

1. Go to this page and download the library: Download robertgdev/ta-lib-hybrid library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

robertgdev / ta-lib-hybrid example snippets


use RobertGDev\TaLibHybrid\TaLibHybrid;

$close = [44.02, 44.10, 44.16, 44.22, 44.10, 44.00, 44.12, 44.18, 44.28, 44.32];

// Single-output indicator — returns flat array, null-padded
$sma = TaLibHybrid::sma($close, 5);
// [null, null, null, null, 44.12, 44.116, 44.12, 44.124, 44.136, 44.18]

// Multi-output indicator — returns associative array of aligned arrays
$bbands = TaLibHybrid::bbands($close, 5, 2.0, 2.0, 0);
// ['upper' => [...], 'middle' => [...], 'lower' => [...]]

$macd = TaLibHybrid::macd($close, 12, 26, 9);
// ['macd' => [...], 'signal' => [...], 'hist' => [...]]

$sma = ta_sma($close, 14);
$atr = ta_atr($high, $low, $close, 14);
$rsi = ta_rsi($close, 14);
$macd = ta_macd($close, 12, 26, 9);

use RobertGDev\TaLibHybrid\TaLibHybrid;
use RobertGDev\TaLibHybrid\Backend\FallbackBackend;

// Force pure-PHP backend (e.g. for testing)
TaLibHybrid::setBackend(new FallbackBackend());

// Check which backend is active
if (TaLibHybrid::isExtensionAvailable()) {
    echo 'Using C extension v' . TaLibHybrid::version();
} else {
    echo 'Using pure-PHP fallback';
}

// Reset to auto-detection
TaLibHybrid::setBackend(null);

use RobertGDev\TaLibHybrid\Backend\ExtensionBackend;
use RobertGDev\TaLibHybrid\Backend\FallbackBackend;

$ext = new ExtensionBackend();
$fb  = new FallbackBackend();

// Both implement the same interface
$smaExt = $ext->sma($close, 14);
$smaFb  = $fb->sma($close, 14);

// SMA(3) on [1, 2, 3, 4, 5]
$result = TaLibHybrid::sma([1, 2, 3, 4, 5], 3);
// [null, null, 2.0, 3.0, 4.0]
//  idx0   idx1  idx2  idx3  idx4

$stoch = TaLibHybrid::stoch($high, $low, $close, 5, 3, 0, 3, 0);
// ['slowk' => [null, null, ...], 'slowd' => [null, null, ...]]

$aroon = TaLibHybrid::aroon($high, $low, 14);
// ['down' => [...], 'up' => [...]]

$ht_sine = TaLibHybrid::ht_sine($close);
// ['sine' => [...], 'leadsine' => [...]]

┌─────────────────────────────────────────────────┐
│  TaLibHybrid (static facade)                    │
│  TaLibHybrid::sma($values, 14)                 │
├─────────────────────────────────────────────────┤
│  Functions.php (procedural polyfill)            │
│  ta_sma($values, 14)                           │
├─────────────────────────────────────────────────┤
│  BackendInterface                               │
│  ├── ExtensionBackend  → ext-ta-lib C calls    │
│  └── FallbackBackend   → pure PHP (Core/)      │
├─────────────────────────────────────────────────┤
│  Constants.php  (TA_* constants polyfill)       │
│  Enum/          (MovingAverageType, ReturnCode…)│
│  Exception/     (TaLibException hierarchy)      │
└─────────────────────────────────────────────────┘

src/
├── TaLibHybrid.php           # Static facade (167 methods)
├── Constants.php              # TA_* constant polyfill (per-constant guards)
├── Functions.php              # ta_* procedural polyfill (161 functions)
├── Backend/
│   ├── BackendInterface.php   # Contract: 4 utility + 161 indicator methods
│   ├── ExtensionBackend.php   # Pass-through to ext-ta-lib
│   └── FallbackBackend.php    # Pure PHP with null-padded output formatting
├── Enum/
│   ├── MovingAverageType.php  # 9 cases (SMA, EMA, WMA, …)
│   ├── FuncUnstId.php         # 25 cases (ADX, RSI, EMA, …)
│   ├── ReturnCode.php         # 6 cases (Success, BadParam, …)
│   ├── CandleSettingType.php  # 12 cases
│   ├── RangeType.php          # 3 cases
│   └── Compatibility.php      # 2 cases
├── Exception/
│   ├── TaLibException.php
│   ├── TaLibInputException.php
│   └── TaLibCalculationException.php
└── Fallback/
    ├── Classes/
    │   ├── CandleSetting.php
    │   └── MoneyFlow.php
    └── Core/                  # Ported calculation engines
        ├── Core.php
        ├── Lookback.php
        ├── OverlapStudies.php
        ├── VolatilityIndicators.php
        ├── MomentumIndicators.php
        ├── CycleIndicators.php
        ├── VolumeIndicators.php
        ├── PatternRecognition.php
        ├── StatisticFunctions.php
        ├── PriceTransform.php
        ├── MathTransform.php
        └── MathOperators.php