Home | Psychology | Children of Narcissus |     Share This Page

Equities Market Simulation Listing (C++)

/***************************************************************************
 *   Copyright (C) 2008 by Paul Lutus                                      *
 *   lutusp@arachnoid.com                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <iostream>
#include <algorithm>
#include <vector>

// produce random double 0 <= n < 1.0

double gen_random() {
  return ((double)rand() / RAND_MAX);
}

int main()
{
  // user settings
  int investor_count = 100000;
  double initial_balance = 10000.0;
  double delta = 0.05; // maximum price change per period
  double years = 20;
  double year_periods = 52; // weeks
  // historical equities performance
  // = 12% per annum excluding the Depression
  double growth_factor = 1.0 + (0.12 / year_periods);
  // initialize the random generator
  srand(12345);
  // create the investor collection
  std::vector<double> investors(investor_count,initial_balance);
  std::vector<double>::iterator investor;
  // set up buy & hold balance
  double bh_balance = initial_balance;
  // perform market simulation
  for(int y = 0;y < years;y++) {
    for (int w = 0;w < year_periods;w++) {
      // adjust buy & hold value
      bh_balance *= growth_factor;
      for(investor = investors.begin();investor != investors.end();investor++) {
        // generate a random price fluctuation
        double adjust = 1.0 + delta * ((gen_random() * 2.0) - 1.0);
        // update this investor's portfolio
        *investor *= adjust * growth_factor;
      }
    }
  }
  // analyze results
  int losers = 0;
  int winners = 0;
  int millionaires = 0;
  for(investor = investors.begin();investor != investors.end();investor++) {
    if (*investor >= 1e6) millionaires++;
    (*investor > bh_balance)?winners++:losers++;
  }
  // sort the investor array from
  // worst to best performance
  std::sort(investors.begin(),investors.end());
  double worst = investors.front();
  double best = investors.back();
  // display results
  printf("Investors %d, initial balance $%.02f\n",investor_count,initial_balance);
  printf("Buy & hold $%.02f, worst $%.02f, best $%.02f\n",bh_balance,worst,best);
  printf("Millionaires %d, winners %d, losers %d\n",millionaires,winners,losers);
  return 0;
}

Home | Psychology | Children of Narcissus |     Share This Page