Professional Documents
Culture Documents
List of topics 1. Introduction to Visual C++ 6.0 development environment a. Static vs. Dynamic linked libraries Advantages of DLLs b. Creation of these libraries c. Usage of these libraries d. Example: Monte-Carlo method to price a plain vanilla option 2. Interfacing Matlab a. C/C++ b. Java c. Perl
Introduction to Visual C++ 6.0 Microsoft Visual C++ is part of Microsofts software development environment: Visual Studio. It is an integrated development tool used to create and build complex C++ systems with an editor, compiler and a debugger among other things. One can think of it as a development tool similar to NetBeans for Java. In this quick introduction we will look at how to create a simple console project using Visual Studio. This should help the reader to get started to create more interesting project types such as a static library and a dynamic-linked library described in the next section. Here, we will try to price a vanilla call option using a simple Monte-Carlo simulation method. Step 1: Open Microsoft Visual C++ 6.0 and go to File->New. You will see:
You will see a list of project types, of which we would be primarily interested in Win32 Console Application, Win32 Dynamic Link Library and Win32 Static Library. Console Application is the traditional command line program that we all know. Step 2: Select Win32 Console Application, Select a location directory and give a name to the project, for example SimpleMonteCarlo. Click Ok and then Finish on your next screen. You will see a screen similar to:
Step 3: Go to File->New again and select C++ Source File and give a name, say MonteCarloCall and click Ok. Step 4: Switch to FileView tab on your left pane and you will notice that a new C++ file is created. Step 5: Enter the below C++ code and save the file. #include <iostream> #include <cmath> #include <cstdlib> using namespace std; double getNormal() { double value = 0; for(int i=0; i< 10; i++) { value += rand()/static_cast<double>(RAND_MAX); } value -= 5.0; return value; }
double monteCarlo(double strike, double expiry, double current, double r, double sigma, long paths) { double discountedStock = current*exp(r*expiry - 0.5*(sigma*sigma*expiry)); double simulatedStock; double totalPayoff = 0; for(int i=0; i<paths; i++) { double rv = getNormal(); simulatedStock = discountedStock*exp(sqrt(sigma*sigma*expiry)*rv); double payoff = simulatedStock - strike; payoff = payoff > 0 ? payoff : 0.00; totalPayoff += payoff; } double mean = totalPayoff/paths; return (mean*exp(-r*expiry)); } int main() { double price = monteCarlo(70, 0.6, 60, 0.04, 0.30, 100); cout << "Price : " << price << "\n"; return 0; } Step 6: Build the project by right-clicking on the project name on the left pane and selecting build. Step 7: If no compilation errors, execute the program by pressing Ctrl+F5 How to create a simple Dynamic Linked Library (DLL) using VC++? While there are different ways to create dlls, we discuss a method called Implicit linking here. Step 1: New->Project->DLL Step 2: In the cpp file : // #define DLLEXPORT __declspec(dllexport) // Step 3: Declare the methods/functions that you want to export from your dll as DLLEXPORT Ex: DLLEXPORT int multiply(int val1, int val2) { } Step 4: Build the dll. You will see two files: a .dll and a .lib file in your debug directory We are done building a dll now. Let us make an application that uses this dll
Step 1: New->Project->Console Application Step 2: Have a new header file and write the following: // #define DLLIMPORT __declspec(dllimport) // DLLIMPORT int multiple(int, int); Step 3: Go to Project->Settings->Link Tab and include the name of the .lib file of the dll. Step 4: Place the .lib file under the root project directory and the .dll file under the debug directory Step 5: Use the function multiply() just like the way you use any function within you console applications C++ file. Advantages of Dynamic Link Libraries (DLLs) When you statically link a program, the resulting executable file must contain all the data and code that the program may ever need. Consequently, it can become ridiculously large. In addition, changing or extending a program's behavior entails recompilation and relinking. Customers must therefore close the application before they install a new version of the executable whenever you supply an update. Dynamic linking offers several advantages over static linking: code sharing, automatic updates, and security. With dynamic linking, programs can share identical code instead of owning individual copies of the same library. However, there are disadvantages with DLLs often associated with the fact that DLLs are shared by different applications.
//engClose(ep); } Create dlls out of Matlab function Lets have a function called getIV(price, strike, rate, time, value) to calculate the implied volatility of a stock from its option prices using the Black-Scholes formula. function im=getIV(price, strike, rate, time, value) im = blsimpv(100, 95, 0.075, 0.25, 10, 0.5); We can create C++ code/dll out of this function by using this command from Matlab: mcc -W cpplib:bs getIV.m
Matlab, just put all the compiled classes into a directory say: C:\java\classes and include this location into Matlab classpath using javaaddpath command: Javaaddpath C:\java\classes Making packages or individual classes available to Matlab is similar. Once the classpaths are correctly, you are set to use Java classes from within Matlab code: Arry = java.util.ArrayList(); // Create an ArrayList Arry.add(10); // Add an element 10.0 Arry.size(); // gets the size of the arraylist Option = my.package.BinomialOption(strike, maturity, rate, vol, current); Price = Option.getPrice(); ImpliedVol = my.package.BlackScholes(price, strike, maturity, rate);
undef $ml; Using Win32::DDE use Win32::DDE::Client; $Client = new Win32::DDE::Client ('Matlab', 'Engine'); die "Unable to start Matlab if $Client->Error; $Client->Execute ('r=rand(4)') || die "failed"; $Client->Execute('im = blsimpv(100, 95, 0.075, 0.25, 10, 0.5)') || die failed; defined ($resp = $Client->Request ('im')) || die " failed"; print "Implied Volatility: $resp\n"; $Client->Disconnect;