All packages
Trading Analytics for Laravel
Per-trade P&L and ROI
Each Trade computes its own figures — the same ones the engine aggregates. Every number comes back as a NumericValueAsString; the comments show its string form:
$trade = Trade::make(
baseCurrency: 'BTC',
quoteCurrency: 'USD',
openPrice: '40000',
closePrice: '42000',
size: '0.5',
direction: 'buy',
openTime: '2024-03-01 09:00',
commission: '10',
closeTime: '2024-03-01 15:00',
);
$trade->pair(); // 'BTC/USD'
$trade->isRealized(); // true — it has a close time
$trade->isOpen(); // false
$trade->profitAndLoss(); // '1000.0000000000' — gross
$trade->profitAndLoss(subtractCommissions: true); // '990.0000000000' — net
$trade->roi(); // '5.00' — percent, 2 decimals
$trade->roi(subtractCommissions: true); // '4.95'
$trade->roi(asPercentage: false); // '0.0500000000' — a fraction- P&L of a buy is (close price − open price) × size; of a sell, (open price − close price) × size.
- subtractCommissions: true subtracts the trade’s commission, when it has one.
- ROI divides the P&L by the position value (open price × size). By default it is a percentage rounded to 2 decimals; asPercentage: false returns the raw fraction.
As an array
toArray() includes the status flags and both P&L and ROI variants:
$trade->toArray();
// [
// 'base_currency' => 'BTC',
// 'quote_currency' => 'USD',
// 'open_price' => '40000.0000000000',
// 'close_price' => '42000.0000000000',
// 'size' => '0.5000000000',
// 'direction' => 'buy',
// 'open_time' => '2024-03-01 09:00:00',
// 'close_time' => '2024-03-01 15:00:00',
// 'commission' => '10.0000000000',
// 'is_realized' => true,
// 'is_open' => false,
// 'pnl' => ['gross' => '1000.0000000000', 'net' => '990.0000000000'],
// 'roi' => ['gross' => '5.00', 'net' => '4.95'],
// ]Trade also implements Jsonable and JsonSerializable, so toJson() and json_encode() give the same payload.
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 cryptoBy 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.