finance c++ programming course [part 1] – installation

C++ finance course for beginners – part 1 – hello world

REQUIREMENTS:
– microsoft visual studio express 2012 (it’s free,older versions are similar. there are slight differences between microsoft and linux c++)
– some programming experience in another language

Course objectives:

-price various derivatives using c++ code
-use basic quantlib and boost functions useful in financial engineering

C++ ,Boost and Quantlib install

1)install visual studio 2012 express :

All Downloads | Visual Studio

install visual studio 2012 c++

install visual studio 2012 c++

2) install boost

Hello world finance c++

Hello world finance c++


– download latest version of boost from http://www.boost.org

c++ boost download

c++ boost download

– unpack it into drive C:
[so its unpacked into c:\boost_1_53_0 ]

– go to the dir c:\boost_1_53_0 and run bootstrap.bat
– run b2.exe
it will take few hours to build!

build boost library

build boost library

– go to dir c:\boost_1_53_0\stage
move whole dir “lib” to c:\boost_1_53_0\ so all built libraries are inside c:\boost_1_53_0\lib

3) install quantlib

download quantlib
http://sourceforge.net/projects/quantlib/files/

quantlib download

quantlib download

unpack it to C:\

– open visual studio express for desktop

File->Open project -> QuantLib_vc10.sln

quantlib solution

quantlib solution

Choose “update” in the next dialog . it will convert files to visual studio 2012 format

in the solution explorer window select QuantLib
press Shift-F4 (or Menu->View->Property pages)

change “include directories” variable to include path to boost
change “library directories” variable to include path to boost\lib

set boost directories

set boost directories

now delete all projects except QuantLib from the solution (click on every project in solution explorer and press “del”)

press F7 to build quantlib [or menu->build->build solution]

Hello world program

Menu -> New Project

choose Visual C++ -> Win32 Console Application

new win32 console app

new win32 console app

press OK->NEXT (do NOT press FINISH yet)

check “Empty Project”
uncheck “Security…” box

new empty project

new empty project

click Finish

right Click on ConsoleApplication1 anc change include and library directory to include QuantLib and boost directories:

hello world c++ finance

hello world c++ finance

right click on “Source Files” -> Add ->Add New Item

add new c++ file

add new c++ file

in the window copy paste following code:

#include <ql/quantlib.hpp>
#include <iostream>

using namespace QuantLib;

void main()
{
	
	Date date1(30,January,2013);
	Date date2;
	date2=date1+1*Years;

std::cout<<date2;

int dummy;std::cin>>dummy;
}
hello world quantlib

hello world quantlib

press F7 to compile

double click on error :

quantlib compile error visual studio 2012

quantlib compile error visual studio 2012

and change the following line :

#  error "unknown Microsoft compiler"

by

#  define QL_LIB_TOOLSET "vc100"

so it looks like that:

quantlib compile error

quantlib compile error

press F7 to build project and F5 to run

hello world quantlib

hello world quantlib

quick explanation of the code:

#include <ql/quantlib.hpp>
#include <iostream>

these lines just insert files named quantlib.hpp and iostream into your source file
quantlib.hpp is needed to have quantlib functions and definitions (Date object)
and iostream is used to write to output console

using namespace QuantLib;

without this line you’d had to write “QuantLib::” before every quantlib object

void main()
{
	
	Date date1(30,January,2013);
	Date date2;
	date2=date1+1*Years;

std::cout<<date2;

int dummy;std::cin>>dummy;
}

these lines define main function
in c++ when console program starts it automatically start executing the function main()

	Date date1(30,January,2013);
	Date date2;

here we define two QuantLib objects(variables) date1 and date2 and assign a value for the first variable

date2=date1+1*Years;
std::cout<<date2;

here we assign value for date2 to be 1 year from date1

and std::cout part prints the result to console

int dummy;std::cin>>dummy;

this is needed to pause the screen

debugging C++ program

to debug program left click where the red dot appears and press F5

debugging c++ program

debugging c++ program

the program will start debug and you can hover cursor over the variable to see it's value

as you can see date1 will show some long number instead of date .It's because internally quantlib uses Excel format for dates .
press F10 to execute instructions one by one of F5 again to continue program until next break point

Posted in quant finance coding course