To calculate the total return for equities over the last 3 weeks using Bloomberg Query Language (BQL), you need to consider both price appreciation and any dividends paid during that period. Here's a BQL formula to achieve this:
// Define the securities and the date range
let securities = ["Equity1 Ticker EQ", "Equity2 Ticker EQ"]; // Replace with your equity tickers
let endDate = businessDateAdd(today(), -1); // The most recent business day
let startDate = businessDateAdd(endDate, -15); // 3 weeks (15 business days) prior to the end date
// Calculate price return
let priceReturn =
bql.history(
bql.securities(securities),
{
fields: ["PX_LAST"],
startDate: startDate,
endDate: endDate
}
).then(result =>
result.data.map(securityData => ({
security: securityData.security.id.value,
priceReturn: (securityData.fields.PX_LAST.values[securityData.fields.PX_LAST.values.length -1]
/ securityData.fields.PX_LAST.values[0]
- 1) * 100
}))
);
// Calculate dividend return
let dividendReturn =
bql.history(
bql.securities(securities),
{
fields: ["DVD_GROSS_ADJUSTED"],
startDate: startDate,
endDate: endDate
}
).then(result =>
result.data.map(securityData => ({
security: securityData.security.id.value,
dividendReturn: securityData.fields.DVD_GROSS_ADJUSTED.values.reduce((acc, val) => acc + val, 0)
}))
);
// Combine price return and dividend return to get total return
bql.combine([priceReturn, dividendReturn]).then(([priceResults, dividendResults]) => {
return priceResults.map(priceResult => {
const dividendResult = dividendResults.find(dr => dr.security === priceResult.security);
return {
security: priceResult.security,
totalReturn: priceResult.priceReturn + dividendResult.dividendReturn
};
});
});
Explanation:
-
Define Securities and Date Range:
securities: An array of equity tickers.
endDate: The most recent business day, calculated using businessDateAdd(today(), -1).
startDate: The date 3 weeks (15 business days) before the endDate, calculated using businessDateAdd(endDate, -15).
-
Calculate Price Return:
bql.history is used to get historical PX_LAST (last price) data for the specified securities and date range.
- The price return is calculated as (\frac{\text{Ending Price}}{\text{Starting Price}} - 1), and then multiplied by 100 to convert it to a percentage
-
Calculate Dividend Return:
bql.history is used to get historical DVD_GROSS_ADJUSTED (gross adjusted dividends) data for the specified securities and date range.
- The dividend return is the sum of all dividends paid during the period.
-
Combine Price Return and Dividend Return:
bql.combine is used to merge the results of the price return and dividend return calculations.
- The total return for each security is the sum of the price return and the dividend return.
Notes:
- Tickers: Replace
"Equity1 Ticker EQ" and "Equity2 Ticker EQ" with the actual tickers of the equities you want to analyse.
- Field Adjustments: Ensure that the fields
PX_LAST and DVD_GROSS_ADJUSTED are correct and available for your securities. If not you may need to use alternative fields.
- Business Days: The date calculation assumes you want business days. If you want calendar days, you should use
dateAdd instead of businessDateAdd.
This script will return the total return for each equity in the list over the last 3 weeks, considering both price changes and dividends.