Assignment 6 - Get Rich Using The $TL


Output updated: 3/3/21

due Tuesday, March 9th

The goal of this assignment is to give you practice using the Standard Template Library, to review the course concepts, and practice problem solving skills.

In this assignment, you have been hired by a financial advisor to analyze two mutual funds and produce reports that will aid in a recommendation to a customer.

NAV => Net Asset Value = the value of one share of a mutual fund on a particular date

Input Files: Mutual Fund history data (9/1/2004 to 02/26/2021)

VFIAX.csv NAV history file
VTSAX.csv  NAV history file
VFIAXdiv.csv  dividend history file
VTSAXdiv.csv  dividend history file

Do not modify input files.  Opening the files in a spreadsheet will give you a misleading interpretation of the contents.  Open the file in a plain editor.

VFIAX.csv

Date,Open,High,Low,Close,Adj Close,Volume
2004-09-01,102.519997,102.519997,102.519997,102.519997,73.177460,0
2004-09-02,103.669998,103.669998,103.669998,103.669998,73.998314,0
2004-09-03,103.230003,103.230003,103.230003,103.230003,73.684273,0
2004-09-07,103.949997,103.949997,103.949997,103.949997,74.198212,0
2004-09-08,103.500000,103.500000,103.500000,103.500000,73.876999,0
2004-09-09,103.699997,103.699997,103.699997,103.699997,74.019768,0
2004-09-10,104.209999,104.209999,104.209999,104.209999,74.383774,0
2004-09-13,104.419998,104.419998,104.419998,104.419998,74.533684,0
2004-09-14,104.650002,104.650002,104.650002,104.650002,74.697861,0
2004-09-15,103.910004,103.910004,103.910004,103.910004,74.169640,0

...         use this column for the NAV => |----------|  

VFIAXdiv.csv

Date,Dividends
2007-12-21,0.777
2008-09-25,0.656
2012-09-21,0.69
2020-12-21,1.386
2017-03-21,1.005
2004-09-24,0.431
2016-09-12,0.89
2009-03-26,0.544
2013-03-21,0.676
2017-06-22,1.017

...


Part 1

Read each of (the 2) NAV files into an unordered_set<string> and then write that container back out into a new file.  Use the output file names, VFIAXshuffled.csv & VTSAXshuffled.csv.

The resultant files should look something like this:

VFIAXshuffled.csv

2021-02-24,363.019989,363.019989,363.019989,363.019989,363.019989,0
2021-02-23,358.940002,358.940002,358.940002,358.940002,358.940002,0
2021-02-19,361.250000,361.250000,361.250000,361.250000,361.250000,0
2021-02-18,361.899994,361.899994,361.899994,361.899994,361.899994,0
2021-02-16,363.549988,363.549988,363.549988,363.549988,363.549988,0
2021-02-12,363.709991,363.709991,363.709991,363.709991,363.709991,0
2021-01-29,343.059998,343.059998,343.059998,343.059998,343.059998,0
2021-01-27,346.359985,346.359985,346.359985,346.359985,346.359985,0
2021-01-25,356.019989,356.019989,356.019989,356.019989,356.019989,0
2021-01-22,354.730011,354.730011,354.730011,354.730011,354.730011,0
2021-01-19,350.769989,350.769989,350.769989,350.769989,350.769989,0
...

Part 2

Create the following classes:

class Date
{
private:
   time_t date;
public:
// whatever you need
};
class Dividend
{
private:
    Date date;
    float div;
public:
// whatever you need
};
class MutualFund
{
private:
    std::string ticker;
    Date oldestDate;      // optional
    Date newestDate;      // optional
    float newestNav;      // optional
    std::map<Date, float> navHistory;
    std::set<Dividend> divHistory;
public:
// whatever you need
};

Note: you will need to construct the input and output filenames using the ticker.
The files should look like this:
VFIAXnav.txt VFIAXdiv.txt               
09/01/2004    102.52
09/02/2004    103.67
09/03/2004    103.23
09/07/2004    103.95
09/08/2004    103.50
09/09/2004    103.70
09/10/2004    104.21
09/13/2004    104.42
09/14/2004    104.65
09/15/2004    103.91
...
09/24/2004 0.431
12/23/2004 0.858
03/23/2005 0.454
06/24/2005 0.444
09/23/2005 0.557
12/28/2005 0.627
03/17/2006 0.516
06/23/2006 0.509
09/22/2006 0.547
12/26/2006 0.680
...

Recommended main() function

int main()
{
    // Part 1
    string ticker1 = "VFIAX", ticker2 = "VTSAX";
    shuffleNavHistoryFile(ticker1);
    shuffleNavHistoryFile(ticker2);

    // Part 2
    MutualFund vfiax(ticker1);
    MutualFund vtsax(ticker2);
    vfiax.report();
    vtsax.report();

    // Part 3
    analyze2Funds(vfiax, vtsax);
}

Part 3

Compare the two mutual funds by evaluating the performance of the funds over 100 random time periods.  Dividends are reinvested.  You will determine the gain of each fund using an initial investment of $10,000 during the time period after applying the dividends awarded during the period.  Finally, you will determine the average gains for both funds over the 100 random time periods.  

Constraints

You are to produce the console output showing the results of each random times period.  That is 100 lines of output and 2 summary average lines.
In addition to the console output, you are to write out a file containing dividend detail information for each of the random time periods.  The file, dividend_detail.txt, that shows the effect of the dividends awarded during the random time period.  That file should look like the following.  It will contain about 5300 records.

dividend_detail.txt

Dividends for VFIAX: 02/26/2018-02/26/2021
Date      Dividend   Shares    Div Amt  Sh Price Purchased  New Sh Owned
06/27/2018   1.166    38.858     45.31    249.20    0.182      39.039
09/25/2018   1.215    39.039     47.43    269.17    0.176      39.216
12/14/2018   1.281    39.216     50.24    239.87    0.209      39.425
03/20/2019   1.456    39.425     57.40    260.51    0.220      39.645
06/26/2019   1.389    39.645     55.07    268.76    0.205      39.850
    <===  Use these numbers as a check
09/25/2019   1.303    39.850     51.92    275.37    0.189      40.039
12/20/2019   1.433    40.039     57.38    297.16    0.193      40.232
03/09/2020   1.179    40.232     47.43    253.19    0.187      40.419
06/26/2020   1.435    40.419     58.00    277.62    0.209      40.628
09/28/2020   1.310    40.628     53.22    309.20    0.172      40.800
12/21/2020   1.386    40.800     56.55    340.80    0.166      40.966

Dividends for VTSAX: 02/26/2018-02/26/2021
Date      Dividend   Shares    Div Amt  Sh Price Purchased  New Sh Owned
06/21/2018   0.294   144.300     42.42     69.24    0.613     144.913
09/27/2018   0.348   144.913     50.43     72.81    0.693     145.605
12/21/2018   0.346   145.605     50.38     59.77    0.843     146.448
03/22/2019   0.374   146.448     54.77     69.49    0.788     147.237
06/14/2019   0.265   147.237     39.02     71.58    0.545     147.782
    <===  Use these numbers as a check
09/13/2019   0.339   147.782     50.10     74.37    0.674     148.455
12/23/2019   0.429   148.455     63.69     79.53    0.801     149.256
03/25/2020   0.297   149.256     44.33     60.17    0.737     149.993
06/24/2020   0.339   149.993     50.85     74.87    0.679     150.672
09/24/2020   0.326   150.672     49.12     79.96    0.614     151.286
12/23/2020   0.378   151.286     57.19     93.66    0.611     151.897

Dividends for VFIAX: 10/29/2004-01/25/2012
Date      Dividend   Shares    Div Amt  Sh Price Purchased  New Sh Owned
12/23/2004   0.858    95.630     82.05    111.45    0.736      96.366
03/23/2005   0.454    96.366     43.75    107.99    0.405      96.771
06/24/2005   0.444    96.771     42.97    109.81    0.391      97.162
09/23/2005   0.557    97.162     54.12    111.93    0.484      97.646
12/28/2005   0.627    97.646     61.22    115.83    0.529      98.174
03/17/2006   0.516    98.174     50.66    120.35    0.421      98.595
06/23/2006   0.509    98.595     50.19    114.60    0.438      99.033
09/22/2006   0.547    99.033     54.17    121.08    0.447      99.481
12/26/2006   0.680    99.481     67.65    130.43    0.519      99.999
03/23/2007   0.576    99.999     57.60    132.21    0.436     100.435
06/22/2007   0.598   100.435     60.06    138.33    0.434     100.869
09/21/2007   0.648   100.869     65.36    140.49    0.465     101.334
12/21/2007   0.777   101.334     78.74    136.59    0.576     101.911
03/27/2008   0.627   101.911     63.90    122.02    0.524     102.435
06/26/2008   0.594   102.435     60.85    118.12    0.515     102.950
09/25/2008   0.656   102.950     67.53    111.27    0.607     103.557
12/26/2008   0.717   103.557     74.25     80.23    0.925     104.482
03/26/2009   0.544   104.482     56.84     76.64    0.742     105.224
06/23/2009   0.454   105.224     47.77     82.47    0.579     105.803
09/28/2009   0.512   105.803     54.17     97.98    0.553     106.356
12/28/2009   0.693   106.356     73.70    103.81    0.710     107.066
03/29/2010   0.465   107.066     49.79    108.07    0.461     107.526
06/28/2010   0.532   107.526     57.20     98.93    0.578     108.105
09/23/2010   0.558   108.105     60.32    103.52    0.583     108.687
12/23/2010   0.529   108.687     57.50    115.72    0.497     109.184
03/24/2011   0.539   109.184     58.85    120.60    0.488     109.672
06/23/2011   0.574   109.672     62.95    118.19    0.533     110.205
09/22/2011   0.586   110.205     64.58    103.97    0.621     110.826
12/22/2011   0.691   110.826     76.58    115.43    0.663     111.489

Dividends for VTSAX: 10/29/2004-01/25/2012
Date      Dividend   Shares    Div Amt  Sh Price Purchased  New Sh Owned
12/23/2004   0.192   374.532     71.91     28.68    2.507     377.039
03/23/2005   0.106   377.039     39.97     27.78    1.439     378.478
06/24/2005   0.097   378.478     36.71     28.43    1.291     379.769
09/23/2005   0.138   379.769     52.41     29.12    1.800     381.569
12/23/2005   0.161   381.569     61.43     30.47    2.016     383.585
03/17/2006   0.127   383.585     48.72     31.57    1.543     385.128

...

Clarification for Dividend calculations

Requirements

  1. Use the exact names for the input and output files.
  2. Create and use the 3 classes shown above.
  3. Create and use the 3 STL containers mentioned above.
  4. Use STL functions and algorithms in your code.  Do not work outside of the containers.
  5. Do not hard code locations in the containers or mutual fund numerical statistics.
  6. Submit only your source and header files.  Input and output files are not needed.  Please compress multiple files in a zipped file.  The instructor will compile and execute your code.

Recommendations / Hints / Suggestions

  1. Allow 8 - 16 hours to complete this assignment.
  2. The console output before the first report regarding file/container I/O is not required but recommended.
  3. The report numbers (part 2) should match exactly using the provided input files.
  4. The comparison output (part 3) will vary, but the average gains should be close, but not too close.
  5. Different (updated) input files will be used for grading your solution.  We been enjoying good times, but the market could turn south.
  6. Use the mutual fund tickers to create filenames.  You may add a prefix path to the exact filenames, but do not bury this in your code.
  7. Exception handling and libraries are not required for this assignment.
  8. Use file open checks and print an error message naming the file when the file open fails.  This will save you and the instructor some time.
  9. The copy algorithm might be of use in part 1.
  10. Write overloaded insertion operator functions for the Date and Dividend classes.
  11. You can probably use an overloaded < operator in the Date and Dividend classes.
  12. Parse the NAV input files using comma (4th and 5th) locations, not absolute positions, for the NAV values
  13. Use pairs.
  14. Use auto for types, so you don't have to think so much.
  15. To determine the minimum and maximum for the last year (part 2) you can use the map::upper_bound function, minimum_element and maximum_element algorithms and a compare binary function.
  16. To determine the range of dividends to award in the random time period (part 3), the set::lower_bound and set::upper_bound functions and the map::find function might be useful.

Program Output


Reading c:/temp/VFIAX.csv into an unordered_set<string> ...
                  <=== Part 1 starts here
Input data stored in 6427 buckets
Writing unordered_set<string> data into file c:/temp/VFIAXshuffled.csv

Reading c:/temp/VTSAX.csv into an unordered_set<string> ...
Input data stored in 6427 buckets
Writing unordered_set<string> data into file c:/temp/VTSAXshuffled.csv

Reading file c:/temp/VFIAXshuffled.csv into map<Date,float> ...
              <=== Part 2 starts here
4151 records read

Reading file c:/temp/VFIAXdiv.csv into set<Dividend> ...
66 records read

Writing NAV history data into file c:/temp/VFIAXnav.txt ... done
Writing dividend history data into file c:/temp/VFIAXdiv.txt ... done

Reading file c:/temp/VTSAXshuffled.csv into map<Date,float> ...
4151 records read

Reading file c:/temp/VTSAXdiv.csv into set<Dividend> ...
66 records read

Writing NAV history data into file c:/temp/VTSAXnav.txt ... done
Writing dividend history data into file c:/temp/VTSAXdiv.txt ... done

Fund: VFIAX
Analysis Date: 02/26/2021
Latest NAV: $352.54
Minimum for the last year = $206.42
Maximum for the last year = $363.71
50 day moving average = $351.81
200 day moving average = $318.72
% gain over 200 Day Moving Avg: 10.61%
% return over the last 3 years: 44.42%

Fund: VTSAX
Analysis Date: 02/26/2021
Latest NAV: $97.44
Minimum for the last year = $54.49
Maximum for the last year = $100.94
50 day moving average = $96.98
200 day moving average = $86.02
% gain over 200 Day Moving Avg: 13.27%
% return over the last 3 years: 48.01%
                                                                          <=== Part 3 starts here
  Buy Date    Sell Date VFIAX Value  VFIAX Gain VTSAX Value  VTSAX Gain VFIAX-VTSAX
10/29/2004   01/25/2012    13626.24     3626.24    14330.88     4330.88     -704.64
11/07/2014   08/29/2017    12767.27     2767.27    12648.37     2648.37      118.90
10/31/2012   05/29/2019    22560.78    12560.78    22341.97    12341.97      218.81
11/02/2010   07/14/2017    23717.41    13717.41    23535.76    13535.76      181.65
07/26/2016   02/19/2019    13485.86     3485.86    13502.57     3502.57      -16.70
05/10/2007   10/08/2009     7564.82    -2435.18     7684.11    -2315.89     -119.28
10/27/2005   11/13/2020    41509.82    31509.82    42316.74    32316.74     -806.92
11/29/2007   01/23/2013    11346.66     1346.66    11719.51     1719.51     -372.85
11/14/2013   07/11/2017    14607.08     4607.08    14394.25     4394.25      212.83
02/02/2012   10/20/2017    21904.15    11904.15    21648.13    11648.13      256.02
09/11/2007   03/16/2010     8328.05    -1671.95     8538.60    -1461.40     -210.55
05/18/2006   12/28/2010    11014.10     1014.10    11348.57     1348.57     -334.48
05/07/2015   10/22/2018    14158.34     4158.34    13924.44     3924.44      233.90
03/21/2007   07/20/2015    17656.37     7656.37    18109.21     8109.21     -452.84
05/04/2006   05/20/2010     8920.58    -1079.42     9052.11     -947.89     -131.53
05/14/2010   12/22/2011    11414.81     1414.81    11393.58     1393.58       21.22
10/21/2005   10/30/2008     8581.78    -1418.22     8579.52    -1420.48        2.26
10/03/2014   10/12/2016    11343.67     1343.67    11292.19     1292.19       51.48
03/21/2013   09/20/2018    21201.75    11201.75    20957.30    10957.30      244.45
...
03/03/2010   12/11/2019    34158.81    24158.81    33667.81    23667.81      491.00
04/02/2007   04/04/2018    23466.96    13466.96    23831.28    13831.28     -364.32
01/11/2007   09/25/2020    30838.52    20838.52    31048.12    21048.12     -209.60
12/12/2005   01/18/2017    22668.30    12668.30    23245.04    13245.04     -576.74
01/19/2010   07/17/2017    24992.18    14992.18    25090.68    15090.68      -98.50
03/19/2012   01/28/2019    21489.48    11489.48    21253.44    11253.44      236.04
10/06/2005   07/08/2008    11265.12     1265.12    11380.99     1380.99     -115.87
01/17/2018   08/28/2020    13169.74     3169.74    12986.04     2986.04      183.70
04/24/2009   06/07/2017    33333.09    23333.09    33691.75    23691.75     -358.66
08/31/2011   04/09/2015    18411.30     8411.30    18520.70     8520.70     -109.40
01/03/2007   10/01/2010     8791.86    -1208.14     9040.43     -959.57     -248.57

Average gain for VFIAX = 8780.52
Average gain for VTSAX = 8900.53