PHP code example of baconfy / indicators

1. Go to this page and download the library: Download baconfy/indicators 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/ */

    

baconfy / indicators example snippets


use Baconfy\Indicators\Data\Candle;
use Baconfy\Indicators\Indicators\Rsi;
use Brick\Math\BigDecimal;

$candles = [
    new Candle(
        openTime: new DateTimeImmutable('2026-01-01 00:00:00'),
        open: BigDecimal::of('42000.00'),
        high: BigDecimal::of('42500.00'),
        low: BigDecimal::of('41800.00'),
        close: BigDecimal::of('42300.00'),
        volume: BigDecimal::of('1250.5'),
    ),
    // ... ordered oldest to newest
];

$rsi = (new Rsi(period: 14))->compute($candles);

// $rsi[0] === null   — warm-up
// $rsi[14] instanceof BigDecimal

use Baconfy\Indicators\IndicatorManager;

$manager = new IndicatorManager;

$manager->make('rsi', ['period' => 14])->compute($candles);
$manager->make('macd')->compute($candles);   // full defaults
$manager->available();                       // list<string> of built-in names

/** @return list<BigDecimal|null> */
public function compute(array $candles): array;

/** @return array<string, list<BigDecimal|null>> */
public function compute(array $candles): array;

new Ema(period: 0);
// InvalidParameterException: Baconfy\Indicators\Indicators\Ema 

$manager->register('my-indicator', MyIndicator::class);

// or through the constructor, merged over the built-in defaults
$manager = new IndicatorManager(['my-indicator' => MyIndicator::class]);

public function __construct(private readonly IndicatorManager $indicators) {}