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) ...
$   line=getline();
$   until strlen(line)==0;
$   s=strtokens(line,",");
$   D=D|evaluate(s[7]);
$return fliplr(D);

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.

>NormedData=Data/Data[:,1];  ...
>col=[green,blue,red]; names=["Siemens","IBM","Google"]; ...
>plot2d(100*NormedData,color=col,title="Portfolio Development",yl="%");  ...


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); ...


>plot2d(c'*Data/V,color=col,title="Portfolio Weights"); ...


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


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.