::result / (Factorial::result * Factorial::result)
36 | };
37 | };
38 |
39 | void showFactorial();
40 |
41 |
42 |
43 | #endif /* defined(__FinancialSamples__FactorialTemplate__) */
44 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/PolymonialInterpolation.h:
--------------------------------------------------------------------------------
1 | //
2 | // PolymonialInterpolation.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 | #ifndef __FinancialSamples__PolymonialInterpolation__
10 | #define __FinancialSamples__PolymonialInterpolation__
11 |
12 | #include
13 |
14 | class PolynomialInterpolation {
15 | public:
16 | PolynomialInterpolation();
17 | PolynomialInterpolation(const PolynomialInterpolation &p);
18 | ~PolynomialInterpolation();
19 | PolynomialInterpolation &operator=(const PolynomialInterpolation &);
20 | void setPoints(const std::vector &x, const std::vector &y);
21 | double getPolynomial(double x);
22 | private:
23 | std::vector m_x;
24 | std::vector m_y;
25 | };
26 |
27 | #endif /* defined(__FinancialSamples__PolymonialInterpolation__) */
28 |
--------------------------------------------------------------------------------
/FinancialCppSamples/MaximaCode/src/OptionProbabilityExportedFunctions.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // OptionProbabilityExportedFunctions.cpp
3 | #include "OptionsProbabilities.h"
4 |
5 | extern "C" double __declspec(dllexport) optionProbFinishAboveStrike(double initialPrice,
6 | double strike, double avgStep, int nDays) {
7 | OptionsProbabilities optP(initialPrice, strike, avgStep, nDays);
8 | return optP.probFinishAboveStrike();
9 | }
10 |
11 | extern "C" double __declspec(dllexport) optionProbFinishBellowStrike(double initialPrice,
12 | double strike, double avgStep, int nDays) {
13 | OptionsProbabilities optP(initialPrice, strike, avgStep, nDays);
14 | return optP.probFinishBellowStrike();
15 | }
16 |
17 | extern "C" double __declspec(dllexport) optionProbFinishBetweenPrices(double initialPrice,
18 | double strike, double avgStep, int nDays, double lowPrice, double highPrice) {
19 | OptionsProbabilities optP(initialPrice, strike, avgStep, nDays);
20 | return optP.probFinalPriceBetweenPrices(lowPrice, highPrice);
21 | }
22 |
23 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/CashFlowCalculator.h:
--------------------------------------------------------------------------------
1 | //
2 | // CashFlowCalculator.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 | #ifndef __FinancialSamples__CashFlowCalculator__
10 | #define __FinancialSamples__CashFlowCalculator__
11 |
12 | #include
13 |
14 | class CashFlowCalculator {
15 | public:
16 | CashFlowCalculator(double rate);
17 | CashFlowCalculator(const CashFlowCalculator &v);
18 | CashFlowCalculator &operator =(const CashFlowCalculator &v);
19 | ~CashFlowCalculator();
20 |
21 | void addCashPayment(double value, int timePeriod);
22 | double presentValue();
23 | private:
24 | std::vector m_cashPayments;
25 | std::vector m_timePeriods;
26 | double m_rate;
27 | double presentValue(double futureValue, int timePeriod);
28 | };
29 |
30 | #endif /* defined(__FinancialSamples__CashFlowCalculator__) */
31 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/NumVector.h:
--------------------------------------------------------------------------------
1 | //
2 | // NumVector.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 | #ifndef __FinancialSamples__NumVector__
10 | #define __FinancialSamples__NumVector__
11 |
12 | #include
13 | #include
14 |
15 | class NumVector {
16 | public:
17 | NumVector();
18 | ~NumVector();
19 | NumVector(const NumVector &);
20 | NumVector &operator =(const NumVector &);
21 |
22 | void add(double val);
23 | void removeLast();
24 | double get(int pos) const;
25 | size_t size() const;
26 | private:
27 | std::vector m_values;
28 | };
29 |
30 | NumVector operator +(const NumVector &a, const NumVector &b);
31 | NumVector operator -(const NumVector &a, const NumVector &b);
32 | NumVector operator *(const NumVector &a, const NumVector &b);
33 |
34 | #endif /* defined(__FinancialSamples__NumVector__) */
35 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/MonteCarloIntegration.h:
--------------------------------------------------------------------------------
1 | //
2 | // MonteCarloIntegration.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 | #ifndef __FinancialSamples__MONTECARLOINTEGRATION_H_
10 | #define __FinancialSamples__MONTECARLOINTEGRATION_H_
11 |
12 | template
13 | class MathFunction;
14 |
15 | class MonteCarloIntegration {
16 | public:
17 | MonteCarloIntegration(MathFunction &f);
18 | MonteCarloIntegration(const MonteCarloIntegration &p);
19 | ~MonteCarloIntegration();
20 | MonteCarloIntegration &operator=(const MonteCarloIntegration &p);
21 |
22 | void setNumSamples(int n);
23 | double getIntegral(double a, double b);
24 | double integrateRegion(double a, double b, double min, double max);
25 | private:
26 | MathFunction &m_f;
27 | int m_numSamples;
28 |
29 | };
30 |
31 |
32 |
33 |
34 | #endif /* MONTECARLOINTEGRATION_H_ */
35 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/Date.h:
--------------------------------------------------------------------------------
1 | //
2 | // Date.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 | #ifndef __FinancialSamples__Date__
10 | #define __FinancialSamples__Date__
11 |
12 | #include
13 |
14 | class Date {
15 | public:
16 |
17 | enum DayOfWeek {
18 | Sun,
19 | Mon,
20 | Tue,
21 | Wed,
22 | Thu,
23 | Fri,
24 | Sat
25 | };
26 |
27 | Date(int year, int month, int day);
28 | ~Date();
29 |
30 | bool isLeapYear();
31 | Date &operator++();
32 | bool operator<(const Date &d);
33 | DayOfWeek getDayOfWeek();
34 | int daysInterval(const Date &);
35 | bool isTradingDay();
36 | std::string toStringDate(Date::DayOfWeek day);
37 | private:
38 | int m_year;
39 | int m_month;
40 | int m_day;
41 | };
42 |
43 |
44 | #endif /* defined(__FinancialSamples__Date__) */
45 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/ParallelOptionsProbabilities.h:
--------------------------------------------------------------------------------
1 | //
2 | // ParallelRandomWalk.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 | #ifndef __FinancialSamples__ParallelRandomWalk__
10 | #define __FinancialSamples__ParallelRandomWalk__
11 |
12 | class ParallelOptionsProbabilities {
13 | public:
14 | ParallelOptionsProbabilities(int size, double strike, double sigma);
15 | ParallelOptionsProbabilities(const ParallelOptionsProbabilities &p);
16 | ~ParallelOptionsProbabilities();
17 | ParallelOptionsProbabilities &operator=(const ParallelOptionsProbabilities &p);
18 |
19 | double probFinishAboveStrike();
20 |
21 | private:
22 | int m_numSteps; // number of steps
23 | double m_step; // size of each step (in percentage)
24 | double m_strikePrice; // starting price
25 | };
26 |
27 | #endif /* defined(__FinancialSamples__ParallelRandomWalk__) */
28 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/RiskCalculator.h:
--------------------------------------------------------------------------------
1 | //
2 | // RiskCalculator.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 | #ifndef __FinancialSamples__RiskCalculator__
10 | #define __FinancialSamples__RiskCalculator__
11 |
12 | #include "CreditRisk.h"
13 |
14 | #include
15 | #include
16 |
17 | // calculates the risk associated to a portfolio
18 | class RiskCalculator {
19 | public:
20 | RiskCalculator();
21 | ~RiskCalculator();
22 | RiskCalculator(const RiskCalculator &);
23 | RiskCalculator &operator =(const RiskCalculator &);
24 |
25 | void addCreditRisk(std::shared_ptr risk);
26 |
27 | CreditRisk::RiskType portfolioMaxRisk();
28 | CreditRisk::RiskType portfolioMinRisk();
29 | private:
30 | std::vector > m_creditRisks;
31 | };
32 |
33 | #endif /* defined(__FinancialSamples__RiskCalculator__) */
34 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/BlackScholesForwardMethod.h:
--------------------------------------------------------------------------------
1 | //
2 | // BlackScholesFowardMethod.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 |
10 | #ifndef __FinancialSamples__BlackScholesFowardMethod__
11 | #define __FinancialSamples__BlackScholesFowardMethod__
12 |
13 | #include
14 |
15 | class BlackScholesForwardMethod {
16 | public:
17 | BlackScholesForwardMethod(double expiration, double maxPrice, double strike, double intRate);
18 | BlackScholesForwardMethod(const BlackScholesForwardMethod &p);
19 | ~BlackScholesForwardMethod();
20 | BlackScholesForwardMethod &operator=(const BlackScholesForwardMethod &p);
21 |
22 | std::vector solve(double volatility, int nx, int timeSteps);
23 | private:
24 | double m_expiration;
25 | double m_maxPrice;
26 | double m_strike;
27 | double m_intRate;
28 | };
29 |
30 |
31 | #endif /* defined(__FinancialSamples__BlackScholesFowardMethod__) */
32 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/DistributionData.h:
--------------------------------------------------------------------------------
1 | //
2 | // DistributionData.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 |
10 | #ifndef DISTRIBUTIONDATA_H_
11 | #define DISTRIBUTIONDATA_H_
12 |
13 | #include
14 |
15 | class DistributionData {
16 | public:
17 | DistributionData();
18 | ~DistributionData();
19 | std::vector gaussianData(int nPoints, double mean, double sigma);
20 | std::vector exponentialData(int nPoints, double rate);
21 | std::vector chiSquaredData(int nPoints, int degreesOfFreedom);
22 | std::vector logNormalData(int nPoints, double mean, double sigma);
23 |
24 | double gaussianQuantile(double x, double mean, double sigma);
25 | double chiSquaredQuantile(double x, int degreesOfFreedom);
26 | double exponentialQuantile(double x, double rate);
27 | double logNormalQuantile(double x, double mean, double sigma);
28 | };
29 |
30 | #endif /* DISTRIBUTIONDATA_H_ */
31 |
--------------------------------------------------------------------------------
/FinancialCppSamples/MaximaCode/src/OptionsProbabilities.h:
--------------------------------------------------------------------------------
1 | //
2 | // OptionsProbabilities.h
3 |
4 | #ifndef __FinancialSamples__OptionsProbabilities__
5 | #define __FinancialSamples__OptionsProbabilities__
6 |
7 | #include
8 |
9 | class OptionsProbabilities {
10 | public:
11 | OptionsProbabilities(double initialPrice, double strike, double avgStep, int nDays);
12 | OptionsProbabilities(const OptionsProbabilities &p);
13 | ~OptionsProbabilities();
14 | OptionsProbabilities &operator=(const OptionsProbabilities &p);
15 |
16 | void setNumIterations(int n);
17 |
18 | double probFinishAboveStrike();
19 | double probFinishBellowStrike();
20 | double probFinalPriceBetweenPrices(double lowPrice, double highPrice);
21 | std::vector getWalk();
22 | private:
23 | double m_initialPrice;
24 | double m_strike;
25 | double m_avgStep;
26 | int m_numDays;
27 | int m_numIterations;
28 |
29 | double gaussianValue(double mean, double sigma);
30 | double getLastPriceOfWalk();
31 | };
32 |
33 | #endif /* defined(__FinancialSamples__OptionsProbabilities__) */
34 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/Option.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // Option.cpp
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 | #include "Option.h"
10 |
11 |
12 | Option::Option(const std::string &ticker, double strike)
13 | : m_ticker(ticker),
14 | m_strike(strike)
15 | {
16 | }
17 |
18 | Option::Option(const Option &p)
19 | : m_ticker(p.m_ticker),
20 | m_strike(p.m_strike)
21 | {
22 | }
23 |
24 | Option::~Option()
25 | {
26 | }
27 |
28 | Option &Option::operator=(const Option &p)
29 | {
30 | if (this != &p)
31 | {
32 | m_ticker = p.m_ticker;
33 | m_strike = p.m_strike;
34 | }
35 | return *this;
36 | }
37 |
38 | std::string Option::ticker()
39 | {
40 | return m_ticker;
41 | }
42 |
43 | double Option::strike()
44 | {
45 | return m_strike;
46 | }
47 |
48 | void Option::setTicker(const std::string &s)
49 | {
50 | m_ticker = s;
51 | }
52 |
53 | void Option::setStrike(double val)
54 | {
55 | m_strike = val;
56 | }
57 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/TwoDimensionalLPSolver.h:
--------------------------------------------------------------------------------
1 | //
2 | // TwoDimensionalLPSolver.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 | #ifndef __FinancialSamples__TwoDimensionalLPSolver__
10 | #define __FinancialSamples__TwoDimensionalLPSolver__
11 |
12 | #include
13 |
14 | class TwoDimensionalLPSolver {
15 | public:
16 | typedef std::vector Vector;
17 |
18 | TwoDimensionalLPSolver(const Vector &c, const Vector &A1, const Vector &A2, const Vector &b);
19 | TwoDimensionalLPSolver(const TwoDimensionalLPSolver &p);
20 | ~TwoDimensionalLPSolver();
21 | TwoDimensionalLPSolver &operator=(const TwoDimensionalLPSolver &p);
22 |
23 | bool solveProblem(Vector &results, double &objVal);
24 | private:
25 | std::vector m_c;
26 | std::vector m_A1;
27 | std::vector m_A2;
28 | std::vector m_b;
29 | };
30 |
31 | #endif /* defined(__FinancialSamples__TwoDimensionalLPSolver__) */
32 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/VectorOperations.h:
--------------------------------------------------------------------------------
1 | //
2 | // VectorOperations.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 | #ifndef __FinancialSamples__VectorOperations__
10 | #define __FinancialSamples__VectorOperations__
11 |
12 | #include
13 |
14 | // performs operations on std::vector using boost ublas
15 | class VectorOperations {
16 | public:
17 | VectorOperations(const std::vector &v);
18 | VectorOperations(const VectorOperations &p);
19 | ~VectorOperations();
20 | VectorOperations &operator=(const VectorOperations &p);
21 | std::vector scalarMult(double scalar);
22 | std::vector addVector(const std::vector &v);
23 | std::vector subtractVector(const std::vector &v);
24 | double dotProd(const std::vector &v);
25 | double norm();
26 | private:
27 | std::vector m_data;
28 | };
29 |
30 | #endif /* defined(__FinancialSamples__VectorOperations__) */
31 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/Recommendation.h:
--------------------------------------------------------------------------------
1 | //
2 | // Recommendation.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 | #ifndef __FinancialSamples__Recommendation__
10 | #define __FinancialSamples__Recommendation__
11 |
12 | #include
13 |
14 | enum RecommendationType {
15 | BUY,
16 | SELL,
17 | HOLD,
18 | NO_RECOMMENDATION
19 | };
20 |
21 | class Recommendation {
22 | public:
23 | Recommendation();
24 | Recommendation(const std::string &ticker, RecommendationType rec, double target);
25 | ~Recommendation();
26 | Recommendation(const Recommendation &r);
27 | Recommendation &operator =(const Recommendation &r);
28 |
29 | double getTarget() const;
30 | RecommendationType getRecommendation() const;
31 | std::string getTicker() const;
32 |
33 | private:
34 | std::string m_ticker;
35 | RecommendationType m_recType;
36 | double m_target;
37 | };
38 |
39 | #endif /* defined(__FinancialSamples__Recommendation__) */
40 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/FinancialStatement.h:
--------------------------------------------------------------------------------
1 | //
2 | // FinancialStatement.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 | #ifndef __FinancialSamples__FinancialStatement__
10 | #define __FinancialSamples__FinancialStatement__
11 |
12 | #include
13 | #include
14 | #include
15 |
16 | class FinancialStatement {
17 | public:
18 | FinancialStatement();
19 | ~FinancialStatement();
20 | FinancialStatement(const FinancialStatement&);
21 | FinancialStatement &operator=(FinancialStatement &);
22 |
23 | double getReturn();
24 | void addTransaction(const std::string &security, double val);
25 |
26 | private:
27 | double m_return;
28 | std::vector > m_transactions;
29 | };
30 |
31 | std::auto_ptr getSampleStatement();
32 |
33 | void transferFinancialStatement(std::auto_ptr statement);
34 |
35 | #endif /* defined(__FinancialSamples__FinancialStatement__) */
36 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/CalmarRatio.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // CalmarRatio.cpp
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 |
10 | #include "CalmarRatio.h"
11 |
12 | #include
13 |
14 |
15 |
16 | boost::ratio<1, 2> one_half;
17 | boost::ratio<1, 3> one_third;
18 |
19 |
20 |
21 | void createCalmarRatio()
22 | {
23 | CalmarRatio ratio(0.15, 11);
24 | }
25 |
26 |
27 | void printRatios()
28 | {
29 | std::cout << "one_third numerator: " << one_third.num
30 | << " denominator: " << one_third.den;
31 | }
32 |
33 | //void printCalmarRatios()
34 | int main_calmar()
35 | {
36 | CalmarRatio ratio(0.110, 3.12);
37 | std::cout << "return: " << ratio.getReturn()
38 | << " drawdown: " << ratio.getDrawDown() << std::endl;
39 |
40 | CalmarRatio bpsRatio(480, 2.15);
41 | std::cout << "return: " << bpsRatio.getReturn()
42 | << " drawdown: " << bpsRatio.getDrawDown() << std::endl;
43 | return 0;
44 | }
45 |
46 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/CalmarRatio.h:
--------------------------------------------------------------------------------
1 | //
2 | // CalmarRatio.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 |
10 | #ifndef CALMARRATIO_H_
11 | #define CALMARRATIO_H_
12 |
13 | #include
14 |
15 | typedef boost::ratio<1, 1>::type CalmarRatioType;
16 | typedef boost::ratio<1, 100>::type CalmarRatioBPS;
17 | typedef boost::ratio<1, 1>::type CalmarRatioPerc;
18 |
19 | template
20 | class CalmarRatio {
21 | public:
22 | CalmarRatio(double calmar, double ret) : m_calmar(calmar), m_return(ret) {}
23 | virtual ~CalmarRatio() {}
24 |
25 | double getReturn()
26 | {
27 | return m_return;
28 | }
29 |
30 | double getDrawDown()
31 | {
32 | return m_return / m_calmar * m_ratio.den;
33 | }
34 | private:
35 | Ratio m_ratio;
36 | double m_calmar;
37 | double m_return;
38 | };
39 |
40 | template <>
41 | double CalmarRatio::getDrawDown()
42 | {
43 | return m_return / m_calmar * m_ratio.den * 100;
44 | }
45 |
46 | #endif /* CALMARRATIO_H_ */
47 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/ModifiedCAP.h:
--------------------------------------------------------------------------------
1 | //
2 | // ModifiedCAP.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 | #ifndef __FinancialSamples__ModifiedCAP__
10 | #define __FinancialSamples__ModifiedCAP__
11 |
12 | #include "Matrix.h"
13 |
14 | // a modified (linearized) model for Capital Asset Pricing
15 | class ModifiedCAP {
16 | public:
17 | ModifiedCAP(int N, int T, double R, Matrix &retMatrix, const std::vector &ret);
18 | ModifiedCAP(const ModifiedCAP &p);
19 | ~ModifiedCAP();
20 | ModifiedCAP &operator=(const ModifiedCAP &p);
21 |
22 | void solveModel(std::vector &results, double &objVal);
23 | void solveExtendedModel(std::vector &results, double &objVal);
24 | private:
25 | int m_N; // number of investment
26 | int m_T; // number of periods
27 | double m_R; // desired return
28 | Matrix m_retMatrix;
29 | std::vector m_assetRet; // single returns
30 | };
31 |
32 | #endif /* defined(__FinancialSamples__ModifiedCAP__) */
33 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/TimeSeriesTransformations.h:
--------------------------------------------------------------------------------
1 | //
2 | // TimeSeriesTransformations.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 | #ifndef __FinancialSamples__TimeSeriesAnalysis__
10 | #define __FinancialSamples__TimeSeriesAnalysis__
11 |
12 | #include
13 |
14 | class TimeSeriesTransformations {
15 | public:
16 | TimeSeriesTransformations();
17 | TimeSeriesTransformations(const TimeSeriesTransformations &);
18 | ~TimeSeriesTransformations();
19 | TimeSeriesTransformations &operator=(const TimeSeriesTransformations &);
20 | void reducePrices(double val);
21 | void increasePrices(double val);
22 | void removePricesLessThan(double val);
23 | void removePricesGreaterThan(double val);
24 | double getFirstPriceLessThan(double val);
25 | void addValue(double val);
26 | void addValues(const std::vector &val);
27 | private:
28 | std::vector m_prices;
29 | };
30 |
31 | #endif /* defined(__FinancialSamples__TimeSeriesAnalysis__) */
32 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/MatrixOperations.h:
--------------------------------------------------------------------------------
1 | //
2 | // MatrixOperations.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 | #ifndef __FinancialSamples__MatrixOperations__
10 | #define __FinancialSamples__MatrixOperations__
11 |
12 | #include
13 |
14 | #include "Matrix.h"
15 |
16 | class MatrixOperations {
17 | public:
18 | MatrixOperations();
19 | ~MatrixOperations();
20 | MatrixOperations(const MatrixOperations &p);
21 | MatrixOperations &operator=(const MatrixOperations &p);
22 |
23 | void addRow(const std::vector &row);
24 | Matrix multiply(Matrix &m);
25 | Matrix transpose();
26 | Matrix elementwiseMultiply(Matrix &m);
27 | Matrix scalarMultiply(double scalar);
28 | std::vector preMultiply(const std::vector &v);
29 | std::vector postMultiply(const std::vector &v);
30 |
31 |
32 | private:
33 | std::vector > m_rows;
34 | };
35 |
36 |
37 | #endif /* defined(__FinancialSamples__MatrixOperations__) */
38 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/Matrix.h:
--------------------------------------------------------------------------------
1 | //
2 | // Matrix.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 | #ifndef __FinancialSamples__Matrix__
10 | #define __FinancialSamples__Matrix__
11 |
12 | #include
13 |
14 | class Matrix {
15 | public:
16 | typedef std::vector Row;
17 |
18 | Matrix(int size);
19 | Matrix(int size1, int size2);
20 | Matrix(const Matrix &s);
21 | ~Matrix();
22 | Matrix &operator=(const Matrix &s);
23 |
24 | void transpose();
25 | double trace();
26 | void add(const Matrix &s);
27 | void subtract(const Matrix &s);
28 | void multiply(const Matrix &s);
29 |
30 | Row & operator[](int pos);
31 | int numRows() const;
32 | private:
33 | std::vector m_rows;
34 | };
35 |
36 | // free operators
37 | //
38 | Matrix operator+(const Matrix &s1, const Matrix &s2);
39 | Matrix operator-(const Matrix &s1, const Matrix &s2);
40 | Matrix operator*(const Matrix &s1, const Matrix &s2);
41 |
42 | #endif /* defined(__FinancialSamples__Matrix__) */
43 |
--------------------------------------------------------------------------------
/FinancialCppSamples/FinancialSamples/RecommendationsProcessor.h:
--------------------------------------------------------------------------------
1 | //
2 | // RecommendationsProcessor.h
3 | //
4 | // (c) 2015 Carlos Oliveira
5 | // This code is part of the book "Practical C++ Financial Programming"
6 | // by Carlos Oliveira, Apress, 2015.
7 | // For more information, visit http://coliveira.net
8 |
9 | #ifndef __FinancialSamples__RecommendationsProcessor__
10 | #define __FinancialSamples__RecommendationsProcessor__
11 |
12 |
13 | #include