How to Use Cursors in SQL for a Portfolio Reporting VBA

Transcription

How to Use Cursors in SQL for a Portfolio Reporting VBA
How to Use Cursors in SQL for a Portfolio Reporting VBA Tool
Portfolio Reporting in VBA: Using SQL Cursors
This document continues with the example of a Portfolio Reporting
VBA tool, which began with the outline of use cases.
In the previous document, we outlined a stored procedure for
calculating the mean, skew and kurtosis of portfolio returns in SQL for
the Portfolio Reporting VBA tool. Here, we look at the more complex
stored procedure with cursors and a loop required to calculate riskreturn measures for each asset. SQL is designed to return complete
tables of data, not rows, so the cursor functionality required to obtain
row by row calculations is really an extension of SQL.
The alternative method for calculating individual asset returns would
have involved creating an ADO Recordset, and transferring each
asset’s returns into an array. This is a slow procedure as it would have
to be repeated for each asset. The better way is to create another
stored procedure in SQL that uses cursors and calls another stored
procedure similar to that for calculating Portfolio return measures.
Stored Procedure Containing Cursor
CREATE PROCEDURE Retrieve_Asset_Returns
USE Assets
DECLARE @Asset_ID VARCHAR(20)
DECLARE cursor_GetAssetReturns CURSOR FOR
SELECT AssetID FROM Asset_Return
OPEN cursor_GetAssetReturns
FETCH NEXT FROM cursor_GetAssetReturns INTO @Asset_ID
WHILE @@FETCH_STATUS = 0
1
Nyasha Madavo, VBA Developer.net
BEGIN
EXEC @CalculateAssetMoments @Asset_ID
FETCH NEXT FROM cursor_GetAssetReturns INTO @Asset_ID
END
CLOSE cursor_GetAssetReturns
DEALLOCATE cursor_GetAssetReturns
RETURN @Asset_ID, @Asset_Value, @Mean_Return,
@Var_Return, @Skew_Return, @Kurt_Return
Stored Procedure Calculating Asset Risk
CREATE PROCEDURE CalculateAssetMoments @AssetID,
@FromDate, @ToDate AS
---First section: declaring variables for risk-return measures
DECLARE @Returns_Count int
DECLARE @Mean_Return float
DECLARE @Var_Return
float
DECLARE @Skew_Return float
DECLARE @Kurt_Return
float
DECLARE @Coeff
float
DECLARE @Std_Dev_Returns float
--2nd section: Retrieving risk-return measures for each assetID
SELECT @Returns_Count = COUNT(Daily_Returns) FROM ASSETS
WHERE Date (BETWEEN @FromDate AND @ToDate) AND AssetID
= @Asset_ID
Portfolio Reporting VBA App: Using Cursors in SQL
How to Use Cursors in SQL for a Portfolio Reporting VBA Tool
SELECT @Mean_Return = SELECT
SUM(Daily_Returns)/@Returns_Count FROM ASSETS WHERE Date
BETWEEN @FromDate AND @ToDate AND AssetID = @Asset_ID
How VBA Developer.net Can Save You Time and
Money
SELECT @Var_Return = SELECT SUM((Daily_Returns @Mean_Return)^2)/(@Returns_Count – 1) FROM ASSETS WHERE
Date BETWEEN @FromDate AND @ToDate AND AssetID =
@Asset_ID
You can get complete Excel apps from VBA Developer.net containing
the code in this document, customisation, VBA development of any
Excel, Access and Outlook apps, as well as C# and C++ add-ins and
technical documentation.
SET @Std_Dev_Price = sqrt(@Var_Returns)
Visit VBA Developer.net
SELECT @Skew_Return = SELECT SUM((Daily_Returns @Mean_Return)^3/@Std_dev_Returns *@Returns_Count /
(@Returns_Count -1) /(@Returns_Count -2) WHERE Date
BETWEEN @FromDate AND @ToDate AND AssetID = @Asset_ID
Examples of VBA documents from VBA
Developer.net
SET @Coeff = 1/(@Returns_Count – 2)/(@Returns_Count – 3)
SET @Kurt_Return = @Returns_Count * (@Returns_Count
+1)*@Coeff*(Returns_Count – 1)*SUM((Daily_Returns @Mean_Return)/@Std_Dev_Return) – 3 *(@Returns_Count -1) *
@Coeff
SELECT @Asset_Value = SELECT AssetHoldings FROM
Asset_Return WHERE AssetID = @Asset_ID
-- 4th section: returning the values from the procedure
RETURN @Asset_ID, @Asset_Value, @Mean_Return,
@Var_Return, @Skew_Return, @Kurt_Return
2
Nyasha Madavo, VBA Developer.net
How to build a Black Scholes VBA Option Pricer
How to build a Black Scholes C# Option Pricer
How to build a Black Scholes VBA Option Pricer for FX
Options
How to build a Black Scholes VBA Option Pricer for Equity
Options
How to build a Black Scholes VBA Option Pricer using Monte
Carlo Simulation
How To Create a Semantic Object Model For a Portfolio
Reporting VBA Tool
Portfolio Reporting VBA App: Using Cursors in SQL
How to Use Cursors in SQL for a Portfolio Reporting VBA Tool
How to build a Black Scholes VBA Option Pricer for Equity
Barrier Options
How to build a Black Scholes VBA Option Pricer for Exotic
Asian Options
How to build a Black Scholes VBA Option Pricer for Exotic
Lookback Options
How to build an Equity Option Pricer using the Binomial Tree
in Excel VBA
How to code a Choleskey Decomposition in VBA (Numerical
Methods for Excel)
How To Normalise a Database For VBA Apps
How To Create a Database using SQL Scripts for a Portfolio
Reporting VBA App
How to Write Stored Procedures in SQL/Access/VBA for a
Portfolio Reporting VBA App
How to Use Cursors in SQL for a Portfolio Reporting VBA
Tool
How to Move Data from Access to Excel with SQL for a
Portfolio Reporting VBA App
3 ways to sort in VBA
Portfolio Reporting VBA Tool: Inserting Data into SQL/Access
Databases from Excel
How to Code a Multivariate Value at Risk (VaR) VBA Monte
Carlo Simulation
Portfolio Reporting VBA Tool: Connecting Excel with SQL &
Access Databases
How To Code the Newton-Raphson Method in Excel VBA
How To Design Classes For an Option Pricer in VBA: UML
Concepts
How to Model Volatility Smiles, Volatility Term Structure and
the Volatility Surface in Excel VBA
How To Design Classes for Object Orientated VBA
Programming
How To Write Use Cases for a Portfolio Reporting VBA Tool
How To Write a User Interface Model For a Portfolio Reporting
VBA Tool
3
Nyasha Madavo, VBA Developer.net
How to build a Black Scholes VBA Option Pricer for Binary
Options
Portfolio Reporting VBA App: Using Cursors in SQL
How to Use Cursors in SQL for a Portfolio Reporting VBA Tool
4
Nyasha Madavo, VBA Developer.net
Portfolio Reporting VBA App: Using Cursors in SQL