参考书上的实现piechart的例子,实现了用cpp调用google chart,生成一个看涨期权股票价格和期权价格的关系的折线图,效果还不错,但是用这种方式画折线图,还是有一些low,虽然效果不错,后期考虑用一些cpp的库专门来画图和制作GUI.
#include <stdexcept>
#include <iostream>
#include <cmath>
#include <cassert>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
double normcdf_build_in(double x) {
return 0.5 * erfc(-x * sqrt(0.5));
}
double blackScholesCallPrice(double strike, double timeToMaturity,
double spot, double volatility, double riskFreeRate) {
double numetator = log(spot / strike) + (riskFreeRate + volatility * volatility / 2) * timeToMaturity;
double e_t = volatility * sqrt(timeToMaturity);
double d1 = numetator / e_t;
double d2 = d1 - e_t;
double nd1 = normcdf_build_in(d1);
//std::cout << "d1 = " << d1 << " my_normcdf(d1) = " << my_normcdf(d1) << std::endl;
double nd2 = normcdf_build_in(d2);
double c0 = spot * nd1 - strike * nd2 * exp(-riskFreeRate * timeToMaturity);
return c0;
}
void writeTopBoilerPlateOfLineChart(std::ostream& out) {
out << "<html>\n";
out << "<head>\n";
out << "<!--Load the AJAX API-->\n";
out << "<script type=\"text/javascript\"";
out << "src=\"https://www.google.com/jsapi\">";
out << "</script>\n";
out << "<script type=\"text/javascript\">\n";
out << "google.load('visualization', '1.0',";
out << " {'packages':['corechart','line']});\n";
out << "google.setOnLoadCallback(drawChart);\n";
out << "function drawChart() {\n";
out << "var data=new google.visualization.DataTable();";
out << "\n";
out << "data.addColumn('string', 'x');\n";
out << "data.addColumn('number', 'y');\n";
}
void writeDataOfLineChart(std::ostream& out,
const std::vector<double>& labels,
const std::vector<double>& values) {
out << "data.addRows([\n";
size_t nLabels = labels.size();
for (size_t i = 0; i < nLabels; i++) {
double label = labels[i];
double value = values[i];
out << "['" << label << "', " << value << "]";
if (i != nLabels - 1) {
out << ",";
}
out << "\n";
}
out << "]);\n";
}
void writeBottomBoilerPlateOfLineChart(
std::ostream& out,
const std::string& xLabelName,
const std::string& yLabelName,
int length,
int width) {
out << "var options = {'title':'A Line Chart',\n";
out << "'hAxis':{'title':'"<< xLabelName <<"'}, \n";
out << "'vAxis':{'title':'" << yLabelName <<"'}, \n";
/*std::cout << "'hAxis':{'title':" << xLabelName << "}, \n";
std::cout << "'vAxis':{'title':" << yLabelName << "}, \n";
std::cout << "'hAxis':{'title':'x'}, \n";
std::cout << "'vAxis':{'title':'y'}, \n";
out << "'hAxis':{'title':'x'}, \n";
out << "'vAxis':{'title':'y'}, \n";*/
out << "'width':"<< length <<", \n";
out << "'height':"<< width << "\n";
out << "};\n";
out << "var chart = new google.visualization.LineChart(";
out << "document.getElementById('chart_div'));\n";
out << "chart.draw(data, options);\n";
out << "}\n";
out << "</script>\n";
out << "</head>\n";
out << "<body>\n";
out << "<div id='chart_div'>\n";
out << "</body>\n";
out << "</html>";
}
void LineChart(const std::string& file,
const std::vector<double>& labels,
const std::vector<double>& values,
const std::string& xLabelName,
const std::string& yLabelName,
int length,
int width) {
std::ofstream out;
out.open(file.c_str());
writeTopBoilerPlateOfLineChart(out);
writeDataOfLineChart(out, labels, values);
writeBottomBoilerPlateOfLineChart(out, xLabelName, yLabelName, length, width);
out.close();
}
int main(){
std::vector<double> labels;
std::vector<double> vals;
for (int i = 0; i < 200; i++) {
labels.push_back(i);
vals.push_back(blackScholesCallPrice(100, 1, i, 0.1, 0.05));
}
std::string fileName = "CalloptionPriceLineChart.html";
std::string xLabelName = "stock_price";
std::string yLabelName = "call_option_price";
int length = 1200;
int width = 800;
//pieChart(fileName, labels, vals);
LineChart(fileName, labels, vals, xLabelName, yLabelName, length, width);
return 0;
}
系统当前共有 469 篇文章