roundly-consulting / trading-analytics-for-laravel
Calculate trading performance analytics — P&L, returns, streaks, profit factor — for Laravel.
Package info
github.com/roundly-consulting/trading-analytics-for-laravel
pkg:composer/roundly-consulting/trading-analytics-for-laravel
Fund package maintenance!
Requires
- php: ^8.4
- ext-bcmath: *
- illuminate/contracts: ^12.0|^13.0
- illuminate/database: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
- roundly-consulting/enums-for-laravel: ^1.0
- roundly-consulting/package-toolkit-for-laravel: ^1.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.18
- nunomaduro/collision: ^8.8|^9.0
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- roundly-consulting/testing-for-laravel: ^1.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Trading Analytics for Laravel
Trading performance analytics for Laravel — P&L, returns, drawdown, expectancy, streaks, Sharpe and
Sortino ratios and more, globally and per pair, base and quote currency. Feed it trades, rows or an
ordered query; every amount is computed with bcmath in a single pass, so there is no float drift
and memory stays flat however long the history is.
Installation
Requires PHP 8.4 with ext-bcmath, and Laravel 12 or 13.
composer require roundly-consulting/trading-analytics-for-laravel
Usage
Build trades from plain values (or read your own rows with Trade::fromRow()):
use RoundlyConsulting\TradingAnalytics\DataTransferObjects\Trade; use RoundlyConsulting\TradingAnalytics\Enums\Direction; $trades = [ Trade::make(baseCurrency: 'BTC', quoteCurrency: 'USD', openPrice: '45000.00', closePrice: '45500.00', size: '0.1', direction: Direction::BUY, openTime: '2024-01-15 12:30', commission: '15.00', closeTime: '2024-01-15 14:30'), Trade::make(baseCurrency: 'ETH', quoteCurrency: 'USD', openPrice: '2500.00', closePrice: '2450.00', size: '2', direction: Direction::BUY, openTime: '2024-01-16 09:00', closeTime: '2024-01-16 11:00'), ];
Run the engine over them:
use Illuminate\Support\Facades\DB; use RoundlyConsulting\TradingAnalytics\Facades\TradingAnalytics; $analytics = TradingAnalytics::for($trades)->scale(2)->calculate(); // …or stream a query ordered by close time, one page of rows at a time: $history = TradingAnalytics::calculate( DB::table('trades')->where('user_id', $user->id)->orderBy('close_time')->orderBy('id'), );
Read the figures — exact bcmath strings, ready to return as JSON:
$analytics->realizedProfitAndLoss->net->global->total->total->toRawString(); // "-65.00" $analytics->wins->winRatio->global->total->toRawString(); // "0.50" $analytics->volume->forPair('BTC/USD')->buy->total->toRawString(); // "0.10" $analytics->maxDrawdown->percentage; $analytics->riskAdjustedReturns->sharpeRatio; return $analytics; // the full result matrix as JSON
Documentation
The full documentation — configuration, every feature and its API, and testing — lives on our website: roundly-consulting.com/open-source/docs/trading-analytics-for-laravel
Release notes are in CHANGELOG.md. To contribute, see the contributing guide.
Support our work
This package is free and open source, built and maintained by Roundly Consulting. If it saves you time, please consider supporting our open-source work — a one-time donation, a monthly pledge on Patreon or a crypto donation helps fund maintenance, new features and new packages.
License
The MIT License (MIT). Please see LICENSE.md for more information.