fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdexcept>
  4.  
  5. using namespace std;
  6.  
  7. struct Asset {
  8. string name;
  9. string type;
  10. double quantity;
  11. double purchase_price;
  12. double current_price;
  13. double transaction_cost;
  14. };
  15.  
  16. // Initial Investment Formula
  17. double InitialInvestment(double quantity, double purchase_price) {
  18. if (quantity <= 0 || purchase_price < 0) {
  19. throw runtime_error("Error the Quantity must be positive and purchase price cannot be negative.");
  20. }
  21. return quantity * purchase_price;
  22. }
  23.  
  24. // Current Value Formula
  25. double CurrentValue(double quantity, double current_price) {
  26. if (quantity <= 0 || current_price < 0) {
  27. throw runtime_error("Error the quantity must be positive and current price cannot be negative.");
  28. }
  29. return quantity * current_price;
  30. }
  31.  
  32. // Net Gain or Loss Formula
  33. double NetGainLoss(double current_value, double initial_investment, double transaction_cost) {
  34. if (transaction_cost < 0) {
  35. throw runtime_error(" Error, the transaction cost cannot be negative.");
  36. }
  37. return current_value - initial_investment - transaction_cost;
  38. }
  39.  
  40. // Net Return (percent) Formula
  41. double NetReturn(double net_gain, double initial_investment) {
  42. if (initial_investment <= 0) {
  43. throw runtime_error("[Error] Cannot calculate return: initial investment must be greater than zero.");
  44. }
  45. return (net_gain / initial_investment) * 100.0;
  46. }
  47. //ici on definit une formule pour calculer montant total investit qui est en realite la somme
  48. //des investissement initiaux dans chaque actif
  49. double TotalInvested( vector<Asset> portfolio) {
  50. double sum = 0.0;
  51. for (int i = 0; i < portfolio.size(); ++i) {
  52. sum += InitialInvestment(portfolio[i].quantity, portfolio[i].purchase_price);
  53. }
  54. return sum;
  55. }
  56.  
  57.  
  58. double CurrentPortfolioValue(vector<Asset> portfolio) {
  59. double sum = 0.0;
  60. for (int i = 0; i < portfolio.size(); ++i) {
  61. sum += CurrentValue(portfolio[i].quantity, portfolio[i].current_price);
  62. }
  63. return sum;
  64. }
  65. //ici pour calculer les cout totaux de transaction ,
  66. //on additionne juste les cours de transaction pour tout es actifs.
  67. double TotalCost(vector<Asset> portfolio) {
  68. double sum = 0.0;
  69. for (int i = 0; i < portfolio.size(); ++i) {
  70. sum += portfolio[i].transaction_coast;
  71. }
  72. return sum;
  73. }
  74. double NetPortfolioResult(vector<Asset> portfolio)
  75. return CurrentValue(portfolio.quantity, portfolio.current_price)-TotalInvested(portfolio)-TotalCost(portfolio) ;
  76.  
  77. // pour calculer le rendement du porte feuille est le rapport entre la fonction qui calcul le resultat
  78. //net du porte feuille et la fonction qui qui calcule les couts totaux investis.
  79. double PortfolioReturn(vector<Asset> portfolio)
  80. return (NetPortfolioResult(portfolio) /TotalInvested(portfolio) ) * 100.0;
  81.  
  82.  
  83.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
2