EMT – Reading Stock Data from CSV Files

The following is inspired by a course in the Iversity about Monte Carlo simulation in finance.

We need to download the historical stock prices from this website. The link should bring you to the historical data for Google in 2013 throughout the year. You can download these data as CSV (comma separated values) to the desktop. If you have an English computer you can open these files in Excel directly. Otherwise, you can import the data. But we want to open the data directly in EMT.

Currently, readtable() does not have a feature to use other separators than blanks. In the next version, I will add this feature. Meanwhile, we use the following function to read the CSV into EMT.

>function readcsv (filename) ...
$open(filename+".csv","r");
$getline();
$D=[];
$repeat;
$   line=getline();
$   until strlen(line)==0;
$   s=strtokens(line,",");
$   D=D|evaluate(s[7]);
$end;
$close();
$return fliplr(D);
$endfunction

As you see, it is a matter of reading the file line by line, separating the items in each line, and evaluating the 7th item. I downloaded stocks for Siemens, IBM and Google for the year 2013 into three files. The notebook with the function is in the same directory. So we can read all data and plot them.

>SI=readcsv("SI");
>IBM=readcsv("IBM");
>Google=readcsv("Google");
>Data=[SI;IBM;Google];
>NormedData=Data/Data[:,1];  ...
>col=[green,blue,red]; names=["Siemens","IBM","Google"]; ...
>plot2d(100*NormedData,color=col,title="Portfolio Development",yl="%");  ...
>labelbox(names,colors=col,>left,x=0.1,w=0.3):

data1

The data are normalized by dividing them by their first element. In the video of the course, Matlab is used for this, and it seems that a loop is necessary for all columns. In EMT, you have a more flexible matrix language.

The video goes on to explain the value of a portfolio and the weights of each asset in the portfolio. Let us do the same. It is a bit easier in EMT than in Matlab. We buy 55, 155, 24 of Siemens, IBM and Google respectively.

>c=[55,155,34]; V=c.Data; ...
>plot2d(100*V/V[1],color=yellow,thickness=3,>add); ...
>labelbox("Portfolio",colors=yellow,x=0.1,y=0.85,w=0.3,>left):

data2

>plot2d(c'*Data/V,color=col,title="Portfolio Weights"); ...
>labelbox(names,colors=col,>left,x=0.1,y=0.7,w=0.3):

data3

The actual values of the assets was the following, by the way.

data4

This is just to show you how to handle real life data in EMT. I hope this kind of instructions is useful for you.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.