NewWe open-sourced 50+ Laravel packages
Custom AI apps, agents and automation — Roundly ConsultingRoundly
All packages
Trading Analytics for Laravel

JSON & API responses

The Analytics instance and every result object implement Laravel’s Arrayable and Jsonable contracts plus PHP’s JsonSerializable, so they drop straight into API responses:

// In a controller — returns the full result matrix as JSON
return $analytics;

// Or explicitly
return response()->json($analytics);

$analytics->toArray();   // nested array
$analytics->toJson();    // JSON string
json_encode($analytics); // same payload via JsonSerializable

$analytics->expectancy->toJson(); // any single result serializes the same way

Each individual result (e.g. $analytics->expectancy, $analytics->counts) and the NumericValueAsString value object serialize the same way.

Payload shape

toArray() contains only the metrics that ran, under snake_case keys; P&L sits under profit_and_loss.realized and profit_and_loss.unrealized. Amounts and ratios are strings, so no precision is lost on the way to JSON:

array_keys($analytics->toArray());
// ['counts', 'wins', 'volume', 'value', 'commission', 'profit_and_loss',
//  'profit_factor', 'cumulative_return', 'frequency', 'duration', 'streaks',
//  'expectancy', 'risk_reward_ratio', 'win_rate_by_period', 'max_drawdown',
//  'risk_adjusted_returns']

$analytics->toArray()['profit_and_loss']['realized']['net']['pnl']['global']['total'];
// [
//     'total' => '781.00',
//     'average' => '260.33',
//     'highest' => ['value' => '990.00', 'pair' => 'BTC/USD'],
//     'lowest' => ['value' => '-306.00', 'pair' => 'ETH/USD'],
// ]

$analytics->counts->toArray()['per_pair'];
// ['BTC/USD' => ['total' => '1', 'buy' => '1', 'sell' => '0'],
//  'ETH/USD' => ['total' => '2', 'buy' => '1', 'sell' => '1']]
  • In the per_pair, per_base_currency and per_quote_currency breakdowns, count-like results (counts, wins, streaks) leave out entries whose total is zero, while aggregates (volume, value, commission, duration, P&L, cumulative return) list every key a trade fed — a pair whose P&L nets to exactly 0 included — and leave out keys merely read through forPair() and friends.
  • wins.win_ratio lists every key with a closed trade, a 0.00 ratio included.
  • frequency serializes each value as a full array — value, scale, prefix, suffix and formatted — so the unit travels with the number.
  • Formatted strings include a prefix or suffix when one is set.

Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

More ways to support, including crypto

By donating, you agree to our donation terms.

Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.