/********************************************************************* DATASHAPING STOCK MARKET SIMULATOR www.datashaping.com/regress.shtml (source code) www.datashaping.com/finance_virtual.shtml (example) The statistical process used here is a Markov chain with infinite state space, to model the logarithm of the stock price (denoted as y). At each iteration, y is augmented by a quantity x (either positive or negative). The possible transitions are: x = pval[0] with probability ptab[0] x = pval[1] with probability ptab[1] ..... x = pval[6] with probability ptab[6] Depending on the values in pval and ptab, the underlying process may show a positive trend, a negative trend, or be stationary. Additionally, some noise is added to the process to make it more realistic. The simulated data is stored in datashap.txt. For ease, let's assume the only possible transitions are -1, 0 or +1. If ptab[2] = ptab[4], then there is no upward or downward trend. If ptab[4] > ptab[2], then the market shows a positive trend. If ptab[4] < ptab[2], the market is declining. Is ptab[3] is the largest value, it means that the market is moving slowly. The transition probabilities are actually proportional to the ptab values. Also, you will notice that in the source code, ptab[4] is very slightly greater than ptab[2], to simulate real market conditions with an overall positive long term trend (+0.02% daily return rate). === TECHNICAL SUPPORT, PRODUCT UPDATES For technical support, e-mail to vincentg@datashaping.com. Data Shaping also offers consulting services if you need customized applications. Visit our web site for further details. === OTHER SOURCE CODE AVAILABLE Web robot : http://www.datashaping.com/robot.shtml Matgrounds : http://www.datashaping.com/matgrounds.shtml Regression : http://www.datashaping.com/regress.shtml *********************************************************************/ #include #include #include #include #define N 7 int idx,l; long k,niter=50000L; double u,aux; double ptab[N]; double pval[N]; double aux,p_distrib,x,y=0; FILE *out; int main(void) { ptab[0]=0.0; ptab[1]=0.0; ptab[2]=0.5; ptab[3]=0.5; ptab[4]=0.51; ptab[5]=0.0; ptab[6]=0.0; for (l=0; lp_distrib) { idx++; p_distrib+=ptab[idx]; } x=pval[idx]; u=(double)rand()/RAND_MAX; x+=0.5*(u-0.5); y=y+x; fprintf(out,"%lf\n",exp(y/50.0)); } fclose(out); return 0; }