FN6806: Object Oriented Programming II
Problems - Set 3
项目类别:金融
Question 3-1
Implement Vasicek model for interest rate simulation
𝑑𝑟(𝑡) = 𝜅 [𝜇 − 𝑟(𝑡)] 𝑑𝑡 + 𝜎𝑑𝑊(𝑡)
𝑟(𝑡 + 1) = 𝑟(𝑡) + 𝜅 [𝜇 − 𝑟(𝑡)] Δ𝑡 + 𝜎

Δ𝑡𝑧
• Use Euler-Maruyama method to generate the paths. returned value is a tuple: 1) the end
rates of all paths, 2) the sum of all rates of all paths (except the starting 𝑟0).
• You could use either vector or valarray for the result.
• The parameters are 𝑟0 = 5%, 𝜎 (𝑠𝑑) = 10%, 𝜅 (kappa) = 0.82, 𝜇 (𝑟𝑚𝑒𝑎𝑛) = 5%, 𝑇 = 0.5, 𝑑𝑡 =
1.0/365.0, 𝑝𝑎𝑡ℎ𝑠 = 20,000, 𝑠𝑡𝑒𝑝𝑠 = 𝑖𝑛𝑡(𝑇 ∗ 365).
auto vasicek(double sd, double kappa, double r_mean, double r0,
double T, int paths, int steps, mt19937 &gen) {
double dt = T / steps;
vector<double> sum_rates(paths);
vector<double> end_rates(paths);
^^.
return make_tuple(end_rates, sum_rates);
}
auto vasicek_valarray(double sd, double kappa, double r_mean,
double r0, double T, int paths, int steps, mt19937 &gen) {
double dt = T / steps;
valarray<double> sum_rates(0.0, paths);
valarray<double> end_rates(r0, paths);
^^.
return make_tuple(end_rates, sum_rates);
}
• Below is my test code and result as reference. You could adapt it to test your result.
seed_seq seed{90127};
auto mtgen = mt19937{seed};
auto [end_rates, sum_rates] =
vasicek(sd, kappa, r_mean, r0, T, 20'000, int(0.5 * 365),
mtgen);
auto end_rates_avg =
accumulate(end_rates.begin(), end_rates.end(), 0.0) /

end_rates.size();

auto sum_rates_avg =
accumulate(sum_rates.begin(), sum_rates.end(), 0.0) /
sum_rates.size();
cout ^< end_rates_avg ^< ", " ^< sum_rates_avg ^< "\n";
^/ 0.0495695, 9.05915
mtgen.seed(seed);
auto [end_rates2, sum_rates2] =
vasicek_valarray(
sd, kappa, r_mean, r0, T, 20'000, int(0.5 * 365), mtgen);
end_rates_avg =
accumulate(end_rates2.begin(), end_rates2.end(), 0.0) /
end_rates2.size();
sum_rates_avg =
accumulate(sum_rates2.begin(), sum_rates2.end(), 0.0) /
sum_rates2.size();
cout ^< end_rates_avg ^< ", " ^< sum_rates_avg ^< "\n";
^/ 0.0495608, 9.06302

Question 3-2
A simulator for event-driven backtesting.
There are two approaches in backtesting trading strategy: vectorized and event-driven.
The vectorized approach is the most common one and consists of simulating the strategy di-
rectly on historical data. The price series are loaded as vectors and we use vectorized opera-
tions for both the trading strategy and the performance metrics.
For example, we can have a buy signal whenever Close > Open and sell signal whenever Close
< Open, and porformance metric is the 1-day lagged signal times the return of the next day
(assuming buy at next day Open). It’s fast to implement such backtesting. However, if we want
to simulate for adding the number of orders when we have 2nd buy signal, we need to modify
the algorithm but it will not be easy. Vectorized method can not simulate the execution of
orders realistically. At all, it is a simplified approach towards backtesting.
The event-driven approach is more sophiscated as it simulates the strategy as if it was executed
in real-time. The price series are loaded as a stream of ticks and the strategy is executed on
each tick, and various modules can be added at both sides: the exeuction side and the strat-
egy side. For example, the exeuction could simulation the price slippage of the execution, the
strategy side could simulate for stop loss and dynamic order sizing depends on past perfor-
mance. The event-driven approach is more flexible and more realistic, but it is more difficult
to implement.
In this exercise, you will read the source code of an event-driven simulator and try to under-
stand how it works. You will need to document 5 places that exception could occur, what is the
error message, what could be the cause of the error and what could be the exception handling.
Create a file exceptions.txt and write down your answers.
This project will also be used in the Final quiz, so you should get familiar with it.
About the simulator:
• Forked and modified from Aku https://github.com/flouthoc/aku/. I have made some
changes and you can get it by forking on Replit at https://replit.com/@YeKunlun/
akufork?v=1
• The author of this repo only made a start so it’s just a partial implementation. You would
find many rough edges: incomplete and incorrect.
• In one-line explaination, it runs over a CSV file with each line as a tick. The trading
strategy acts on the tick data and perform buy/sell operations.
• All cpp files are in \src
• All hpp files are in \include
Use Replit tools
• When you press Run button, the program shasll run in the Console. However, you need
to scroll back in history for the output.
• You could use Code Search (Shortcut: Ctrl+Shift+F) to search for text in the project.
• For manual mode, usually you don’t need to, you could open the Shell tool to type make
for compilation and then type ./main to run the program. make shall auto-detect any
recently changed file and recompile the program. If you want to recompile every thing,
make clean and then make.

留学ICU™️ 留学生辅助指导品牌
在线客服 7*24 全天为您提供咨询服务
咨询电话(全球): +86 17530857517
客服QQ:2405269519
微信咨询:zz-x2580
关于我们
微信订阅号
© 2012-2021 ABC网站 站点地图:Google Sitemap | 服务条款 | 隐私政策
提示:ABC网站所开展服务及提供的文稿基于客户所提供资料,客户可用于研究目的等方面,本机构不鼓励、不提倡任何学术欺诈行为。