Monday, January 21, 2013

Cumulative return vs Daily Return for a stock

Lets us say there is a stock 'SRK' that is being actively traded on NYSE exchange. SRK has the following prices corresponding to the dates given

Date                            Price
1/21/2013                   $10
1/22/2013                   $15
1/23/2013                   $20
1/24/2013                   $25

Daily Return : Daily return is the profit/loss made by the stock compared to the previous day. A value above one indicates profit, similarly a value below one indicates loss. It is also expressed in percentage to convey the information better. (When expressed as percentage, if the value is above 0, the stock had give you profit else loss) So for the above example the daily returns would be

Date                            Daily Return                        %Daily Return
1/21/2013                   $10/$10 =  1                          -  
1/22/2013                   $15/$10 =  3/2                       50%
1/23/2013                   $20/$15 =  4/3                       33%
1/24/2013                   $25/$20 =  5/4                       20%


Cumulative Return : While daily returns are useful, it doesn't give the investor a immediate insight into the gains he had made till date, especially if the stock is very volatile. Cumulative return is computed relative to the day investment is made.  If cumulative return is above one, you are making profits else you are in loss. So for the above example cumulative gains are as follows

Date                            Cumulative Return               %Cumulative Return
1/21/2013                   $10/$10 =  1                            100 %  
1/22/2013                   $15/$10 =  3/2                         150%
1/23/2013                   $20/$10 =  2                            200%
1/24/2013                   $25/$10 =  5/2                         250%

 ==================================================================

Question : Given daily returns can you compute cumulative returns for each day ?

Date                            Daily Return                  Cumulative Return
1/21/2013                   1                                      ? 
1/22/2013                   3/2                                   ?
1/23/2013                   4/3                                   ?
1/24/2013                   5/4                                   ? 


Answer : Yes Indeed. For calculating cumulative return at the end of particular day, you just have to multiply the daily returns made by the stock for all of the days leading upto the date you are interested. For example : 

Cumulative return at the end of day3 :  Daily return of day3 *
                                                            Daily return of day2 *
                                                            Daily return of day1

With this understanding lets try to fill out the table again

Date                            Daily Return                  Cumulative Return
1/21/2013                   1                                      1
1/22/2013                   3/2                                  3/2 * 1 =  3/2
1/23/2013                   4/3                                  4/3 * 3/2 *1 =  2
1/24/2013                   5/4                                  5/4 * 4/3 * 3/2 * 1 = 5/2

As you can see cumulative return column in the above table is  similar to the one we have derived earlier.

===================================================================

Food For Thought :  Given Cumulative Return, compute Daily return

Date                            Cumulative Return                 Daily Return
1/21/2013                   1                                                 ?
1/22/2013                   3/2                                              ?
1/23/2013                   2                                                 ?
1/24/2013                   5/2                                              ?









Saturday, January 19, 2013

Course Era : Installing Dev Environment for QSTK Part 2

While learning  about QSTK, there would be many times, where you would edit certain files, compile them and see how the software behave. In this post I am listing out the steps that would help you to do it in Eclipse.

  1. Install Eclipse. I have Eclipse Indigo installed.
  2. Get python plugin for eclipse from Pydev website (http://pydev.org/manual_101_root.html)
  3. Choosing python 2.7 that was installed for QSTK as python interpretor for pydev
 
 
 
 
 
 
 
Click on the Eclipse-->Preferences, expand Pydev and Select Interpretor-Python
In the python interpreter section on the right side, do not click on auto-configure button. When I clicked on it, libraries for the default version of Python that comes with MAC got selected. Remember that QSTK software requires Python 2.7. 










Choosing Python 2.7 interpreter
 
So this is what you have to do. Click on New, then in the window that pops up, click on Browse and select the path where python 2.7 is installed. If you have followed the steps provided for installing QSTK, python 2.7 will be installed in /opt/local/bin/python-2.7 as shown as in the figure. 
 
 
 
 
 
 
 
Interpreter and PYTHONPATH
 
 
 
Once that is selected, python interpreter and PYTHONPATH will be set correctly

 
 
 
 
4. Import QSTK source code into Eclipse
 
Create a new Pydev Project
 
Click on File--> New--> PyDev Project  
 
 
 
 
 
 
 
Project Selection
 
 
 
 
 
For the project name select QSTK. Unselect "Use Default". Click on the Browse button next to Directory and select the location where QSTK software is downloaded. If you were following the steps mentioned in the installation of QSTK, this location is usually "/Users/srk/QSTK"  
Make sure you have "Add Project directory to the PYTHONPATH" selected  as shown in the figure and click Finish









Project Path in Eclipse
 
 
 
Once this step is completed. You should see the entire directory structure of QSTK in Eclipse as shown in the figure. If your directory structure is not similar to the one shown in the figure, go back and make sure you have followed the steps correctly











5. Setting environment variables
 
There are many environment variables QSTK depends on. These are in config.sh under QSTK directory.  The last step is to set these variables in Eclipse
 
 
 
Environment Tab
 Goto Eclipse-->Preferences-->Pydev-->Interpreter Python. On the Right hand side select environment in the bottom section as shown in the figure
 
 
 




Now you have to various environment variables by clicking on the NEW button. Open config.sh file and keep adding various environment variables. These are the ones I have added. 

Name                           :             Value
CACHESTALLTIME  :              12
QS                                :              /Users/srk/QSTK
QSBIN                         :              /Users/srk/QSTK/Bin
QSDATA                      :             /Users/srk/QSTK/QSData
QSDATAPROCESSED:            /Users/srk/QSTK/QSData/Processed
QSDATATMP               :            /Users/srk/QSTK/QSData/Tmp
QSSCRATCH               :            /Users/srk/QSTK/QSData/Scratch

At the end of this, this how my environment Tab looks. Close the Preferences Panel


Environment Variables           






 6. Run sample tutorial1.
 

Running a sample tutorial
 
Open tutorial1.py located in Examples folder. Identify the play button at the top of eclipse, select Run as and click "Python run" as shown in the figure





 
Console output
 
 
 
Once it starts running, you should see the messages in the console that are similar to the ones shown in the lecture video.
 
 
 
 
 
 
 
 
 
 
 
Hurrah..!! You now have QSTK in eclipse. I hope you found these instructions useful. You can now leverage the powerful features of the eclipse editor, and understand QSTK/Python better :-)..






Course Era : Installing Dev Environment for QSTK Part I

As the name of the course suggests, it teaches investing in stocks, utilizing computational software. The computation software used is QSTK. In this blog I intend to cover the problems I faced while installing this software. In my next post, I will write about setting up dev environment for QSTK in eclipse.


Installing QSTK :
(http://wiki.quantsoftware.org/index.php?title=QSToolKit_Installation_Guide_Mac)
The above link gives us detailed instructions for installing QSTK along with other depedency software. The only problem I faced while following the steps mentioned in the above link on my Snow Leapord MAC is while installing matplotlib ("sudo port install py27-matplotlib @1.1.0"). The error I was getting was that "freetype files" are not found my path. You might not run into this problem if you are using later version of OSX. After googling and following different suggestions , I found a certain comment in stack overflow (http://stackoverflow.com/questions/4092994/unable-to-install-matplotlib-on-mac-os-x/10843478#10843478) very useful. I am copying over the relevant contents here for completion

<Start stack overflow comment>
You can, however, get matplotlib to compile by simply symlinking the existing freetype/libpng headers and libraries into the /usr/local tree with:

udo mkdir -p /usr/local/include
sudo ln -s /usr/X11/include/freetype2/freetype /usr/local/include/freetype
sudo ln -s /usr/X11/include/ft2build.h /usr/local/include/ft2build.h
sudo ln -s /usr/X11/include/png.h /usr/local/include/png.h
sudo ln -s /usr/X11/include/pngconf.h /usr/local/include/pngconf.h
sudo ln -s /usr/X11/include/pnglibconf.h /usr/local/include/pnglibconf.h
sudo mkdir -p /usr/local/lib
sudo ln -s /usr/X11/lib/libfreetype.dylib /usr/local/lib/libfreetype.dylib
sudo ln -s /usr/X11/lib/libpng.dylib /usr/local/lib/libpng.dylib

<End stack overflow comment> 

After following the the above suggestions rest of the steps suggested in the QSTK installation link went smoothly. The next post will deal with installing QSTK project in eclipse.