金融市场中信息万变,C++作为编程语言,具有编译速度快,编程灵活等特点,深受金融工程人士喜爱。
本贴运用C++语言,编制数学公式、金融函数、以及投资实战分析程序,均属原创代码,在之前的帖子分别发布过,此处做一个集合,供广大量化投资爱好者们研究和学习。
具体内容如下:
1.公差为1的等差数列求和(Arithmetic Series)
等差数列求和公式:设公差为d,
第n项:an = 1 + (n-1)d
前n项求和公式:Sn = na1 + n(n - 1)d/2;当d = 1时:Sn = (a1 + an)n/2
(程序中假设公差为1)
本帖隐藏的内容
- // 等差数列求和 -by Freddy
- #include <iostream>
- #include<cmath>
- using namespace std;
- int sum;
- int i;
- int n;
- int main(){
- cout<<"In order to calculate: '1+2+3+...+n', please input the value of n"<<endl;
- cin>>n;
- for(i=1;i<=n;i++){sum+=i;}
- cout<<endl<<"1+2+3+...+"<<n<<" = "<<sum;
- return 0;
- }
2.现金流现值计算(Present Value of Cash Flow)
未来现金流现值:每一期发生在未来的现金流的贴现金额的总和。
公式:PV = ∑ⁿ t=1 Ct/(1 + r)^t
(程序中假设未来现金流Ct为恒定值)
本帖隐藏的内容
- //现金流现值的计算 -by Freddy
- #include<iostream>
- #include <cmath>
- using namespace std;
-
- main(){
- double Ct=0.0, r= 0.0; double PV = 0.0;
- int T = 0;
- cout<<"Input the stream of payments Ct:\n";
- cin>>Ct;
- cout<<"Input the interest rate r:\n";
- cin>>r;
- cout<<"Input the year of payment T:\n";
- cin>>T;
-
-
- for(int t=1; t<=T; t++){
- PV+=Ct/pow(1.0+r,t);
- }
- cout<<"The present value is\n";
- cout<<PV;
-
- return 0;
- }
3.麦考劳林级数(Maclaurin Series)
设B(a,r) 是欧几里得空间RN中的开球,ƒ 是定义在B(a,r) 的闭包上的实值函数,并在每一点都存在所有的n+1 次偏导数。这时的泰勒公式为:
ex =1 + X/1!+ X2/2! + X3/3! + ···, -∞ < X < +∞
麦考劳林级数是泰勒级数的特殊形式,程序中对基础泰勒级数公式进行了变形
公式:Sum =1– 1/2! + 1/3! −1/4!+ . . . ±1/n!
本帖隐藏的内容
- Sum=1 – 1/2! +1/3! −1/4! + . . . ±1/n!
- // 关于麦考劳伦级数的 —by Freddy
- #include <iostream>
- #include <cmath>
- using namespace std;
- int factorial(int n)
- {
- int fact = 1;
- if (n>0){
- for (int i=1;i<=n;i++){
- fact*=i;
- }
- }
- return fact;
- }
- double _sum(int n)
- {
- double sum = 1.0;
- if (n>1){
- for (int i=1;i<n;i++){
- sum+=pow(-1,i)/factorial(i+1);
- }
- }
- return sum;
- }
- int main(){
- int n = 0;
- cout << "Input the value of n \n";
- cin >> n;
- double sum = _sum(n);
- cout << "The value of the series is\n";
- cout << sum;
-
- return 0;
- }
4.股票收益与回报分析(Analysis of Stocks: Risk and Return)
在股票投资分析技术里,衡量股票投资与回报的工具有很多,其中最常用的是平均回报率(均值μ)和平均波动率(标准差σ)。平均回报率越高说明股票在对应时期内收益越高,标准差越大则说明股票波动越大风险越大。反之,平均回报率越小收益越低,标准差越小波动风险越小。
程序选取了中国银行(601988)于2014年11月14日至2015年2月13日期间的股票日均回报率作为分析对象(数据在txt文件里),数据来源于yahoo finance.投资爱好者们可通过同花顺,通达信等软件导出数据作为分析对象。
本帖隐藏的内容
- //中国银行股票投资回报与风险分析 -by Freddy
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <cmath>
- using namespace std;
- vector<double> x;
- double mx_fun(vector<double> x, double mx)
- { mx=0.0;
- for (int i=0;i<x.size();i++){
- mx+=x[i]/x.size();
- }
- return mx;
- }
-
- double vx_fun(vector<double> x, double vx, double mx)
- { vx=0.0;
- for (int i=0;i<x.size();i++){
- vx+=pow(x[i]- mx_fun(x,mx),2)/(x.size()-1);
- }
- return vx;
- }
-
-
-
- int main(){
- ifstream source1("bank of china.txt");
-
- double a;
- if (source1.is_open()){
- while(!source1.eof()){
- source1 >> a;
- x.push_back(a);
- }
- source1.close();
- }
- double beta=0.0,mx=0.0, my=0.0,vx=0.0,vy=0.0;
-
-
- double mean= mx_fun(x, mx);
- double var=vx_fun(x,vx, mx);
-
-
- double sdv=sqrt(var);
-
- cout<< "The average return of Bank of china is : " << mean<<endl;
- cout<<"The volatility(standard deviation) of Bank of china is : "<< sdv <<endl;
-
-
- return 0;
-
- }
附件是C++程序源代码。
复制粘贴到C++编译器(Dev)即可运行。
程序仅用于学术交流,不作为商业用途。
程序如有编得严谨的地方,敬请指点。
此外,同一目标可以有多种方法和思路.
朋友们若有更先进更简洁的算法,欢迎留言交流。
本帖隐藏的内容