Documentation Index Fetch the complete documentation index at: https://mintlify.com/jessebyarugaba/Unofficial-Uganda-Securities-Exhange-API/llms.txt
Use this file to discover all available pages before exploring further.
This guide demonstrates how to retrieve historical stock data and exchange rate information using the Uganda Securities Exchange API library.
Getting historical stock data
The getPortfolioCompanyData() method retrieves historical OHLC (Open, High, Low, Close) data for a specific company, including volume and adjusted close prices.
Basic market data retrieval
Initialize the library
<? php
include 'PortfolioManager.php' ;
$portfolioManager = new PortfolioManager ();
Fetch market data
Pass the company ticker symbol to get historical data: $companyData = $portfolioManager -> getPortfolioCompanyData ( 'BOBU' );
print_r ( $companyData );
Code examples
Basic stock data
Latest share price
Analyzing price trends
Volume analysis
<? php
include 'PortfolioManager.php' ;
$portfolioManager = new PortfolioManager ();
// Get historical data for Bank of Baroda Uganda
$companyData = $portfolioManager -> getPortfolioCompanyData ( 'BOBU' );
print_r ( $companyData );
?>
Response structure
The getPortfolioCompanyData() method returns a JSON object with this structure:
{
"stock" : "BOBU" ,
"data" : [
{
"date" : "2024-01-15 00:00:00" ,
"timestamp" : 1705276800000 ,
"open" : 1500 ,
"high" : 1550 ,
"low" : 1480 ,
"close" : 1520 ,
"adjusted_close" : 1520 ,
"volume" : 125000
},
{
"date" : "2024-01-16 00:00:00" ,
"timestamp" : 1705363200000 ,
"open" : 1520 ,
"high" : 1580 ,
"low" : 1515 ,
"close" : 1560 ,
"adjusted_close" : 1560 ,
"volume" : 180000
}
],
"latestSharePrice" : 1560
}
Data fields
Field Description stockCompany ticker symbol dataArray of historical data points latestSharePriceMost recent closing price dateHuman-readable date and time timestampUnix timestamp in milliseconds openOpening price for the period highHighest price during the period lowLowest price during the period closeClosing price for the period adjusted_closeAdjusted closing price (accounts for splits, dividends) volumeNumber of shares traded
The data array is sorted chronologically, with the most recent data point at the end. Use end($data['data']) to access the latest data point.
Exchange rate data
The getExchangeRateDetails() method retrieves current UGX exchange rate information from African Financials.
Getting exchange rates
<? php
include 'PortfolioManager.php' ;
$portfolioManager = new PortfolioManager ();
// Get current exchange rate details
$exchangeRates = $portfolioManager -> getExchangeRateDetails ();
print_r ( $exchangeRates );
?>
Expected response
{
"values" : {
"VALUE/US$" : "0.000271" ,
"TODAY'S CHANGE" : "+0.000002 (+0.74%)" ,
"1 YEAR CHANGE" : "-0.000015 (-5.25%)" ,
"52 WEEK RANGE" : "0.000265 - 0.000288"
},
"disclaimer" : "Exchange rates are updated daily..." ,
"source" : "https://africanfinancials.com/currency/ug-ugx/" ,
"execution_time" : 1.2345 ,
"time in seconds" : "1.2345 seconds"
}
Working with exchange rates
<? php
include 'PortfolioManager.php' ;
$portfolioManager = new PortfolioManager ();
// Get exchange rate data
$ratesJson = $portfolioManager -> getExchangeRateDetails ();
$rates = json_decode ( $ratesJson , true );
// Extract the UGX to USD rate
$ugxToUsd = floatval ( $rates [ 'values' ][ 'VALUE/US$' ]);
// Convert UGX amount to USD
$amountUgx = 1000000 ; // 1 million UGX
$amountUsd = $amountUgx * $ugxToUsd ;
echo "UGX " . number_format ( $amountUgx ) . " = USD " . number_format ( $amountUsd , 2 ) . " \n " ;
echo "Today's Change: " . $rates [ 'values' ][ "TODAY'S CHANGE" ] . " \n " ;
echo "52 Week Range: " . $rates [ 'values' ][ '52 WEEK RANGE' ] . " \n " ;
?>
Practical applications
<? php
include 'PortfolioManager.php' ;
$portfolioManager = new PortfolioManager ();
function getStockPerformance ( $ticker ) {
global $portfolioManager ;
$dataJson = $portfolioManager -> getPortfolioCompanyData ( $ticker );
$stockData = json_decode ( $dataJson , true );
$data = $stockData [ 'data' ];
$firstPrice = $data [ 0 ][ 'close' ];
$latestPrice = $stockData [ 'latestSharePrice' ];
$priceChange = $latestPrice - $firstPrice ;
$percentChange = ( $priceChange / $firstPrice ) * 100 ;
return [
'ticker' => $ticker ,
'currentPrice' => $latestPrice ,
'priceChange' => $priceChange ,
'percentChange' => round ( $percentChange , 2 ),
'dataPoints' => count ( $data )
];
}
// Analyze multiple stocks
$stocks = [ 'BOBU' , 'UCL' , 'UMEME' ];
foreach ( $stocks as $ticker ) {
$performance = getStockPerformance ( $ticker );
echo " \n " . $performance [ 'ticker' ] . ": \n " ;
echo " Current Price: UGX " . number_format ( $performance [ 'currentPrice' ]) . " \n " ;
echo " Change: UGX " . number_format ( $performance [ 'priceChange' ]) . " (" . $performance [ 'percentChange' ] . "%) \n " ;
echo " Historical Data Points: " . $performance [ 'dataPoints' ] . " \n " ;
}
?>
Portfolio valuation with currency conversion
<? php
include 'PortfolioManager.php' ;
$portfolioManager = new PortfolioManager ();
// Define portfolio holdings
$portfolio = [
[ 'ticker' => 'BOBU' , 'shares' => 1000 ],
[ 'ticker' => 'UCL' , 'shares' => 500 ],
[ 'ticker' => 'UMEME' , 'shares' => 750 ]
];
// Get exchange rate
$ratesJson = $portfolioManager -> getExchangeRateDetails ();
$rates = json_decode ( $ratesJson , true );
$ugxToUsd = floatval ( $rates [ 'values' ][ 'VALUE/US$' ]);
// Calculate portfolio value
$totalValueUgx = 0 ;
foreach ( $portfolio as & $holding ) {
$dataJson = $portfolioManager -> getPortfolioCompanyData ( $holding [ 'ticker' ]);
$stockData = json_decode ( $dataJson , true );
$currentPrice = $stockData [ 'latestSharePrice' ];
$holdingValue = $currentPrice * $holding [ 'shares' ];
$holding [ 'currentPrice' ] = $currentPrice ;
$holding [ 'value' ] = $holdingValue ;
$totalValueUgx += $holdingValue ;
}
$totalValueUsd = $totalValueUgx * $ugxToUsd ;
// Display results
echo "Portfolio Valuation \n " ;
echo "=================== \n\n " ;
foreach ( $portfolio as $holding ) {
echo $holding [ 'ticker' ] . ": " ;
echo $holding [ 'shares' ] . " shares @ UGX " . number_format ( $holding [ 'currentPrice' ]) . " = " ;
echo "UGX " . number_format ( $holding [ 'value' ]) . " \n " ;
}
echo " \n Total Value: UGX " . number_format ( $totalValueUgx ) . " \n " ;
echo "Total Value: USD " . number_format ( $totalValueUsd , 2 ) . " \n " ;
echo " \n Exchange Rate: 1 USD = " . number_format ( 1 / $ugxToUsd , 2 ) . " UGX \n " ;
?>
Exchange rate data is fetched from African Financials and includes execution time metrics. The API performs web scraping, so response times may vary.
CSV export for analysis
<? php
include 'PortfolioManager.php' ;
$portfolioManager = new PortfolioManager ();
// Get stock data
$dataJson = $portfolioManager -> getPortfolioCompanyData ( 'BOBU' );
$stockData = json_decode ( $dataJson , true );
// Open CSV file for writing
$filename = 'BOBU_historical_data.csv' ;
$file = fopen ( $filename , 'w' );
// Write header
fputcsv ( $file , [ 'Date' , 'Open' , 'High' , 'Low' , 'Close' , 'Adjusted Close' , 'Volume' ]);
// Write data rows
foreach ( $stockData [ 'data' ] as $dataPoint ) {
fputcsv ( $file , [
$dataPoint [ 'date' ],
$dataPoint [ 'open' ],
$dataPoint [ 'high' ],
$dataPoint [ 'low' ],
$dataPoint [ 'close' ],
$dataPoint [ 'adjusted_close' ],
$dataPoint [ 'volume' ]
]);
}
fclose ( $file );
echo "Data exported to $filename \n " ;
echo "Total records: " . count ( $stockData [ 'data' ]) . " \n " ;
?>
The library performs web scraping to retrieve data. For production applications:
Cache responses to reduce API calls
Implement rate limiting to avoid overloading the USE servers
Handle timeouts and network errors gracefully
The getExchangeRateDetails() method includes execution time metrics for monitoring
Next steps
Basic usage Review the basics of using the library
Company lookup Learn how to retrieve company details