#!/usr/local/bin/perl # simtrade.pl. This program simulates fluctuating # stock prices for the WWWS system. $historySize = 30; $dataPath = "/home/boutell/wwwws"; $iterations = 1; srand(); if (int(@ARGV) == 1) { $iterations = $ARGV[0]; } elsif (int(@ARGV) == 0) { # OK, assume 1 iteration } else { die "Usage: simtrade [# of iterations]\n"; } for ($iter = 0; ($iter < $iterations); $iter++) { $stocks = 0; $s = $dataPath . "/database"; $snew = $dataPath . "/database.new"; open(IN, $s) || die "Can't open database file " . $s . "for reading.\n"; open(OUT, ">" . $snew) || die "Can't open temporary file " . $s . " for writing.\n"; while() { #Remove any trailing space s/\s+$//g; @fields = split(/ /); $symbol = $fields[0]; if ($symbol eq "") { break; } $historyTotal = int(@fields) - 1; if ($historyTotal == $historySize) { for ($i = 0; ($i < ($historySize - 1)); $i++) { $fields[$i + 1] = $fields[$i + 2]; } $#fields -= 1; $historyTotal--; } # Generate a price fluctuation at random. Some would say this # method is quite realistic. if ($historyTotal) { $fields[$historyTotal + 1] = $fields[$historyTotal] + (int(rand(40)) - 19.5) * .125; if ($fields[$historyTotal + 1] < 0.0) { $fields[$historyTotal + 1] = 0.125; } } else { $fields[$historyTotal + 1] = (int(rand(80)) + 80) / 2.0; } $historyTotal++; # Write the updated record. print OUT join(' ', @fields), "\n"; $stocks++; } print $stocks, " stocks updated.\n"; close(IN); close(OUT); rename($snew, $s); } exit 0;