tevens Institute of Technology Assignment 1 FE 522 – C++ Programming in Finance Due Date: December 2, 2018
For every problem below, create a different project folder. You should then put all these folders inside a .zip file with your name before submitting everything in Canvas. Remember to delete all build and .vs folders before doing so (do not delete the .vscode folders). This .zip file should be accompanied by a .pdf file containing a report (one single report for the whole assignment). I do not provide test cases for any of the problems, and these must be provided by you. When providing a solution to a problem, you must be able to present test cases alongside it, otherwise it will not be accepted as a valid solution.
If a problem requires input and/or output files, please provide the whole file content (if not large) in the body of the report. If the file is too large to be pleasantly presented in a report (larger than a page), please provide a sample. You should include these files in folders named “input” and “output”, respectively, in the root folder of each project. In order for your code to work on any machine (not only yours), use relative paths to these files in your source code:
• for input files, use: “../../input/filename.txt”
• for output files, use: “../../output/filename.txt”
Problem 1 (2 points). Design and implement an AmericanOption class. It should hold information such as option type (call or put), spot price (of the underlying asset), strike price, interest rate, volatility (of the underlying asset) and time to maturity. Don’t accept illegal values. Implement a getPrice() function which gives the price of the option using a binomial tree defined as a matrix (vector< vector
Hint: https://stackoverflow.com/questions/17663186/initializing-a-two-dimensional-stdvector
Problem 2 (2 points). Design and implement an Option class as the base class for both EuropeanOption and AmericanOption classes. It should contain all the member variables that are common between both option types, as well as a virtual function getPrice(). Moreover, it should contain four (non-virtual) functions to compute the numerical Greeks (getDelta() for spot price, getRho() for interest rate, getVega() for volatility, and getTheta() for time to maturity) of a given option. These functions should:
• Compute the price of the option by calling the virtual function getPrice();
• Apply a small bump to the required pricing factor (the size of the bump should be a parameter of the function);
• Compute the “bumped price” of the option by calling the virtual function getPrice();
1
https://stackoverflow.com/questions/17663186/initializing-a-two-dimensional-stdvector
• Change the modified pricing factor back to its original value;
• Return the numeric approximation of the given Greek.
After all the above is done, implement an external function (to the class) which takes a Option& as parameter and compute all four Greeks in a row. Compare the results you obtain for American and European (call and put) options.
Problem 3. (2 points). Modify the Link class from sections 17.9 and 17.10 of the book to hold a value of an Option. Write a printAll() function that lists options with their attributes one per line. Add a member function addOrdered() that place its new element in correct (type, time to maturity, strike price)-order. Using Links with values of both EuropeanOption and AmericanOption, make a list of both call and put options to test your implementation.
Problem 4 (2 points). Modify the Link class from sections 17.9 and 17.10 of the book to be a template with the type of value as the template argument. Then redo Problem 3 with Link
Problem 5. (2 points) In C, a string (often called a C string or a C-style string in C++ literature) is a zero-terminated array of characters. For example, p and q are equivalent:
char∗ p = ” asd f ” ; char∗ q = new char ( 5 ) ; q [ 0 ] = ’ a ’ ; q [ 1 ] = ’ s ’ ; q [ 2 ] = ’d ’ ; q [ 3 ] = ’ f ’ ; q [ 4 ] = 0 ;
In C, we cannot have member functions, we cannot overload functions, and we cannot define an operator (such as ==) for a struct. It follows that we need a set of (nonmember) functions to manipulate C-style strings:
(a) Write a function, char* strdup(const char*), that copies a C-style string into memory it allocates on the free store.
(b) Write a function, char* findx(const char* s, const char* x), that finds the first occurrence of the C-style string x in s.
(c) Write a function, int strcmp(const char* s1, const char* s2), that compares C-style strings. Let it return a negative number if s1 is lexicographically before s2, zero if s1 equals s2, and a positive number if s1 is lexicographically after s2.
Do not use any standard library functions. Do not use subscripting; use the dereference operator * instead.
Bonus (2 points). Design and implement a BarrierOption class that also inherits from Option. This time, however, the getPrice() function should compute the option price using Monte Carlo. Compute the Greeks for this kind of option.
2