PHP code example of roundly-consulting / trading-analytics-for-laravel

1. Go to this page and download the library: Download roundly-consulting/trading-analytics-for-laravel 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/ */

    

roundly-consulting / trading-analytics-for-laravel example snippets


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'),
];

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'),
);

$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