Professional Documents
Culture Documents
Guy Yollin
Instructor, Applied Mathematics University of Washington
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 1 / 45
Outline
Introduction to QueryTables Downloading equity data from Yahoo Finance Read data from SQL databases Implementing VBA worksheet formulas VBA chart creation
2 / 45
Outline
Introduction to QueryTables Downloading equity data from Yahoo Finance Read data from SQL databases Implementing VBA worksheet formulas VBA chart creation
3 / 45
Section references
J. Green, S. Bullen, R. Bovey, M. Alexander Excel 2007 VBA Programmers Reference Wiley, 2007 Chapter 21
4 / 45
The QueryTables object is a collection of QueryTable objects Use the Add method to create a new query table and add it to the QueryTables collection Each QueryTable object represents a worksheet table built from data returned from an external data source
5 / 45
Destination Sql
6 / 45
7 / 45
True if queries for the query table are performed asynchronously (in the background) Returns or sets the command string for the specied data source The CommandType describes the value of the CommandText property Gets/sets text le column format True if the comma is the delimiter when you import a text le
Dene the connection string Dene the destination Call QueryTables.Add to create the query table object Set the command (i.e. specify the data to be retrieved) Call QueryTable.Refresh to get the data
9 / 45
Outline
Introduction to QueryTables Downloading equity data from Yahoo Finance Read data from SQL databases Implementing VBA worksheet formulas VBA chart creation
10 / 45
Section references
Simon Beginninga Financial Modeling, 3rd Edition Massachusetts Institute of Technology, 2008 Chapter 41 J. Green, S. Bullen, R. Bovey, M. Alexander Excel 2007 VBA Programmers Reference Wiley, 2007 Chapter 21
11 / 45
12 / 45
Parameter s a b c d e f g
Guy Yollin (Copyright 2012)
Description ticker symbol fromMonth - 1 fromdat (2 digits) fromYear toMonth - 1 toDay (2 digits) toYear d=day, m=month
14 / 45
16 / 45
18 / 45
Outline
Introduction to QueryTables Downloading equity data from Yahoo Finance Read data from SQL databases Implementing VBA worksheet formulas VBA chart creation
19 / 45
Section references
J. Green, S. Bullen, R. Bovey, M. Alexander Excel 2007 VBA Programmers Reference Wiley, 2007 Chapter 21
20 / 45
21 / 45
ODBC Setup
22 / 45
24 / 45
26 / 45
27 / 45
Outline
Introduction to QueryTables Downloading equity data from Yahoo Finance Read data from SQL databases Implementing VBA worksheet formulas VBA chart creation
28 / 45
Section references
John Walkenbach Excel 2010 Power Programming with VBA Sams, 2010 Chapter 8
29 / 45
d1 =
30 / 45
Black-Shoales formula
P u b l i c F u n c t i o n B l a c k S c h o l e s ( C a l l P u t F l a g As S t r i n g , _ S As Double , X As Double , T As Double , r As Double , _ v As Double ) As Double Dim d1 As Double , d2 As Double d1 = ( Log ( S / X) + ( r + v ^ 2 / 2 ) T) / ( v Sqr (T ) ) d2 = d1 v Sq r (T) I f CallPutFlag = "c" BlackScholes = S E l s e I f CallPutFlag = BlackScholes = X End I f End F u n c t i o n Then CND( d1 ) X Exp( r T) CND( d2 ) " p " Then Exp( r T) CND( d2 ) S CND( d1 )
Source: http://www.espenhaug.com/black_scholes.html
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 31 / 45
N (x ) = where
k=
1 1+ x
Black-Shoales formula
// The c u m u l a t i v e n o r m a l d i s t r i b u t i o n f u n c t i o n P u b l i c F u n c t i o n CND(X As Double ) As Double Dim L Const Const Const As a1 a3 a5 Double , K As Double = 0 . 3 1 9 3 8 1 5 3 : C o n s t a2 = 0. 35 65 6 3 78 2: = 1 . 7 8 1 4 7 7 9 3 7 : C o n s t a4 = 1. 82 12 55 97 8: = 1.330274429
L = Abs (X) K = 1 / (1 + 0.2316419 L) CND = 1 1 / Sqr ( 2 A p p l i c a t i o n . P i ( ) ) Exp( L ^2/2) _ ( a1 K + a2 K^2 + a3 K^3 + a4 K^4 + a5 K^5) I f X < 0 Then CND = 1 CND End I f End F u n c t i o n
33 / 45
34 / 45
You can use many (but not all) of Excels worksheet functions in your VBA code The WorksheetFunction object, which is contained in the Application object, holds all the worksheet functions that you can call from your VBA procedures To use a worksheet function in a VBA statement, just precede the function name with the object reference:
Application.WorksheetFunction
35 / 45
Black-Shoales formula
P u b l i c F u n c t i o n B l a c k S c h o l e s V e r 2 ( C a l l P u t F l a g As S t r i n g , _ S As Double , X As Double , T As Double , r As Double , _ v As Double ) As Double Dim d1 As Double , d2 As Double d1 = ( Log ( S / X) + ( r + v ^ 2 / 2 ) T) / ( v Sqr (T ) ) d2 = d1 v Sq r (T) I f C a l l P u t F l a g = " c " Then B l a c k S c h o l e s V e r 2 = S W o r k s h e e t F u n c t i o n . NormSDist ( d1 ) _ X Exp( r T) W o r k s h e e t F u n c t i o n . NormSDist ( d2 ) E l s e I f C a l l P u t F l a g = " p " Then B l a c k S c h o l e s V e r 2 = X Exp( r T) _ W o r k s h e e t F u n c t i o n . NormSDist( d2 ) _ S W o r k s h e e t F u n c t i o n . NormSDist( d1 ) End I f End F u n c t i o n
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 36 / 45
Outline
Introduction to QueryTables Downloading equity data from Yahoo Finance Read data from SQL databases Implementing VBA worksheet formulas VBA chart creation
37 / 45
Section references
John Walkenbach Excel 2010 Power Programming with VBA Sams, 2010 Chapter 18 Duane Birnbaum and Michael Vine Excel VBA Programming for the Absolute Beginner, 3rd Edition Thomson Course Technology, 2007 Chapter 9
38 / 45
1 2 3 4
Select A1:A17, H1:H17 Click record macro Insert clustered columns chart Right click and Format Axis
Guy Yollin (Copyright 2012)
6 7 8 9
Change Axis Labels to Low Align text to rotate 270 Delete legend Click stop recording
Data access with VBA 39 / 45
Recorded macro
Sub R e c o r d P l o t R e t u r n s ( ) A c t i v e S h e e t . S h a p e s . AddChart . S e l e c t ActiveChart . SetSourceData S o u r c e := Range ( " msft ! $A$1 : $A$17 , msft ! $H$1 : $H$17 " ) A c t i v e C h a r t . ChartType = x l C o l u m n C l u s t e r e d A c t i v e C h a r t . Axes ( x l C a t e g o r y ) . S e l e c t A c t i v e S h e e t . C h a r t O b j e c t s ( " C h a r t 10 " ) . A c t i v a t e S e l e c t i o n . T i c k L a b e l P o s i t i o n = xlLow A c t i v e S h e e t . C h a r t O b j e c t s ( " C h a r t 10 " ) . A c t i v a t e A c t i v e C h a r t . Legend . S e l e c t Selection . Delete End Sub
Hard-coded range Hard-coded chart name (will not be correct the next time) Nothing recorded regarding the text alignment
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 40 / 45
To-Do: align x-axis labels add a y-axis label change the title change chart position
Guy Yollin (Copyright 2012) Data Access with SQL, Excel & VBA Data access with VBA 41 / 45
42 / 45
43 / 45
44 / 45
http://depts.washington.edu/compfin
45 / 45