#! /usr/bin/gawk -f # Last edited on 2015-02-23 21:49:04 by stolfilocal # Creates a fake trade data file with random volumes, where the log of the price is a # brownian variable. # Reads from standard input a real trade data file, with some headers and then # date and time as the first two fields. Writes to standard output # some appropriate headers, and then one line for each input data line, # with the same date and time, with random prices and volumes. # Users must define (with "-v") the random generator's {seed}, the initial price {P0}, # and the deviation of the increment in the decimal log of the price. BEGIN \ { if (seed == "") { arg_error("must define {seed}"); } if (P0 == "") { arg_error("must define {P0}"); } if (dev == "") { arg_error("must define {dev}"); } srand(seed); z = log(P0/1000.0)/log(10.0); # Decimal log of (price/1000). printf "# Generated by {fake_brownian_data.gawk}. Do not edit.\n"; printf "# The decimal log of the price is a Brownian variable whose increments\n"; printf "# are normally distributed with mean 0 and deviation %9.7f.\n", dev; # Print {txtable-reformat}'s header: printf "Timestamp "; printf " ! %12s ! %12s ! %12s ! %12s", "Open", "High", "Low", "Close"; printf " ! %16s ! %16s ! %12s", "V.BTC", "V.USD", "WTPrice"; printf "\n"; } /^20[0-9][0-9][-]/ \ { if (NF != 16) { data_error(("bad field count " NF)); } # Copy date and time from the input file: dt = $1; tm = $2; # Brownian integration: dz = dev * nrandom(); z = z + dz; # Convert to linear scale: P = exp(log(10)*z)*1000; # Price in USD. VB = 2000*rand(); # Volume in BTC. VU = P*VB; printf "%s %s", dt, tm; printf " | %12.5f | %12.5f | %12.5f | %12.5f", P, P, P, P; printf " | %16.4f | %16.4f | %12.5f", VB, VU, P; printf "\n"; } function nrandom( n,i,sum,avg,dev) { # Normal random variable with zero mean and unit deviation. n = 20; # Number of uniform variables to add. sum = 0; for (i = 0; i < n; i++) { sum = sum + rand(); } avg = 0.5*n; # Expcted value of {sum}. dev = sqrt(n/12.0); return (sum - avg)/dev; } function data_error(msg) { printf "%s:%s: ** %s\n", FILENAME, FNR, msg > "/dev/stderr"; printf " «%s»\n", $0 > "/dev/stderr"; abort = 1; exit(abort); } function arg_error(msg) { printf "** %s\n", msg > "/dev/stderr"; abort = 1; exit(abort); }