For example, the payoff for European call or put options are functions of the
terminal price of the underlying and the exercise price of the option

The payoff is programmed as a function of two pieces of data, the terminal price and the exercise price.

// file payoff_black_scholes.cc #include <algo.h> double payoff_european_call(double& price, double& X){ return max(0.0,price-X); }; double payoff_european_put(double& price, double& X){ return max(0.0,X-price); };

As another example, let us look at options depending on the max or min of the price sequence. Here we have to pass the whole price history

// file payoff_max_min #include <vector.h> #include <algo.h> double payoff_max(vector<double>& prices) { double m = *max_element(prices.begin(),prices.end()); return m-prices.back(); // max is always larger or equal. }; double payoff_min(vector<double>& prices) { double m = *min_element(prices.begin(),prices.end()); return prices.back()-m; // always positive or zero };

One thing to note in this definition is the use of the function
`max_element`. It returns the maximum of set of variables passed to
it. It is part of the more recent additions to the `C++` language, the
algorithms in the Standard Template Library.

Another example: Options on the average.

// file payoff_average.cc #include <math.h> #include <algo.h> #include <vector.h> double payoff_arithmetric_average(vector<double>& prices) { double sum=accumulate(prices.begin(), prices.end(),0.0); double avg = sum/prices.size(); return max(0.0,avg-prices.back()); }; double payoff_geometric_average(vector<double>& prices) { double logsum=log(prices[0]); for (unsigned i=1;i<prices.size();++i){ logsum+=log(prices[i]); }; double avg = exp(logsum/prices.size()); return max(0.0,avg-prices.back()); };