The following library implements useful functionality for wrinting questions for
financial derivatives. It includes forward prices, forward contracts and the Black-Scholes-Merton
model for European call and put options.
The question 606302 tests this macro library. If you make changes to the library please modify
the test question to incorporate your changes and run to make sure that nothing is broken.
This function returns the payout of a portfolio. The portfolio can contain call options, put options,
forward contracts and zero-coupon bonds. The parameters are:
- asset is the terminal asset value for all positions. It may be a single number or an array of numbers.
if it is an array of numbers then the function returns an array of payouts.
- types is an array where each element must contain 'call', 'put', 'forward' or 'bond'.
Capitalization does not matter. If an entry does not contain one of these then that position is
ignored.
- possizes is an array of position sizes. It can be positive or negative. If the type is a zero-coupon bond
then the position size is the amount paid at maturity.
- strikes is an array of strikes. These are the strikes for calls, puts and forward contracts. It is
ignored if the type is a zero coupon bond.
This function checks that the array of points represented by xValues and yValues are matched by the given
lines. The function returns a number between 0 and 1 determined by how many points are matched. It is
meant to be used in a conditional style question where the student enters a chart to match a payout diagram. If a y-value
is equal to zero, it does not have to be on any line. The parameters are:
- xValues is an array of x-values for the payout to be checked.
- yValues is an array of payout values, one for each x-value.
- lines is an array of lines. A line is represented by four values x1, y1, x2 and y2 representing the
end points of the line. This can be returned using gettwopointdata from the student chart. Most likely, you
will want to call $w, $h = getsnapwidthheight() to pass into the gettwopointdata call.
- xTol is a tolerance on the x-values to match to the line. It defaults to 1e-4 which should generally work.
- yTol is a tolerance on the y-values to match to the line. It defaults to 1e-4 which should generally work.
This function calculates the fair forward price for an asset which has a yield. It is
appropriate for assets such as stock indices or FX rates. The parameters are:
- spot is the spot price of the asset.
- rf is the continuously compounded risk-free interest rate.
- yield is the yield on the asset as a continuously compounded rate.
- today is today's date.
- matdate is the maturity date of the forward.
- fmt is the format of the date. It defaults to 'F j, Y'
This function calculates the fair forward price for a stock which pays a single dividend.
The parameters are:
- spot is the spot price of the stock.
- rf is the continuously compounded risk-free interest rate.
- div is the dividend amount.
- today is today's date.
- divdate is the dividend date for the dividend. If it is before the maturity of the forward then it
is included in the calculation. If it is after, then it is ignored.
- matdate is the maturity date of the forward.
- fmt is the format of the date. It defaults to 'F j, Y'
This function finds the present value of a forward contract. The parameters are:
- count is the number of units of the asset to be delivered in the contract.
A positive number will return the present value of being long the contract (buying the asset)
and a negative number will return the present value of being short the contract (selling the asset).
- forward is the fair forward price of the asset at the maturity of the contract.
- strike is the strike of the forward contract. This is the price at which one is
buying or selling the asset.
- rf is the continuously compounded risk-free interest rate.
- today is today's date.
- matdate is the maturity date of the forward contract.
- fmt is the format of the date. It defaults to 'F j, Y'
This function calculates the value, delta, gamma or vega of a European call or put
option using the Black-Scholes-Merton formulas. The parameters are:
- type must be 'call' or 'put'.
- strike is the strike of the option.
- mat is the maturity date of the option.
- today is today's date.
- spot is the spot price of the asset.
- rf is the continuously compounded risk-free interest rate.
- vol is the volatility of the asset.
- ans is the answer type. It must be 'value', 'delta', 'gamma' or 'vega'.
- fmt is the format of the date. It defaults to 'F j, Y'
This function finds the nth IMM date after the start date. IMM dates are the
third Wednesday of March, June, September and December. The parameters are:
- start is the starting date. If it is an IMM date then n=1 will return the next one.
- n is the number of IMM dates after start date to return. It must be positive.
- fmt is the format of the date. It defaults to 'F j, Y'
This function finds the nth equity futures date after the start date. Equity
futures dates are the third Friday of March, June, September and December. The parameters are:
- start is the starting date. If it is an equity futures date then n=1 will return the next one.
- n is the number of equity futures dates after start date to return. It must be positive.
- fmt is the format of the date. It defaults to 'F j, Y'
This function converts a rate from one type to another. The rate types must be '360', '365' or 'cc'.
The '360' and '365' refer to ACT/360 and ACT/365 basis. 'cc' refers to continuously compounded rates.
- days is the number of days for the rate.
- rate is the rate to be converted.
- starttype is the type of the rate. It must be '360', '365' or 'cc'.
- endtype is the type of rate to be converted to. It must be '360', '365' or 'cc'.
This function computes the fair forward rate between date1 and date2 using the continuously compounded rates rate1
and rate2. It returns the rate in the basis which must be '360' or '365' for ACT/360 or ACT/365.
- today is today's date.
- date1 the start date of the forward rate.
- rate1 is the continuously compounded rate from today to date1.
- date2 the end date of the forward rate.
- rate2 is the continuously compounded rate from today to date2.
- basis is the basis of the rate to be calculated. It must be '360' or '365' for
ACT/360 or ACT/365.
- fmt is the format of the date. It defaults to 'F j, Y'
This function computes the present value of an forward rate agreement (FRA).
- borrowlend is whether the present value is from the perspective of
the borrower 'borrow' or lender 'lend'.
- principal is the amount being borrowed or lent. It must be positive.
- date1 the start date of the forward rate agreement.
- date2 the end date of the forward rate agreement.
- strike is the agreed rate for the borrowing or lending.
- basis is the basis of the agreed rate. It must be '360' or '365' for
ACT/360 or ACT/365.
- today is today's date.
- rate1 is the continuously compounded rate from today to date1.
- rate2 is the continuously compounded rate from today to date2.
- fmt is the format of the date. It defaults to 'F j, Y'