<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1440440391871056920</id><updated>2012-01-06T02:02:50.604-08:00</updated><category term='CUSIP'/><category term='Portfolio Construction'/><category term='yahoo'/><category term='Asset Allocation'/><category term='Dickey-Fuller Test'/><category term='matlab web scraper'/><category term='IB'/><category term='Implied Jump'/><category term='Market Profile'/><category term='New vix methodology'/><category term='Tick Data'/><category term='regexp'/><category term='tick aggregation'/><category term='rolling window'/><category term='CUSIP lookup'/><category term='Writing Options'/><category term='Volatility Cones'/><category term='AMERITRADE'/><category term='Covered Call'/><category term='Black-Litterman'/><category term='normal distribution'/><category term='Expected Returns Range'/><category term='Stock Symbol'/><category term='Convert'/><category term='tick2bar'/><category term='Cointegration'/><category term='Johansen'/><category term='yahoo option chains'/><category term='Interest Rate Spread'/><category term='INDIA'/><category term='Strangles'/><category term='IB matlab2ib'/><category term='VIX'/><category term='0% credit card'/><category term='automated futures'/><category term='Why 22? Why 30? Central Limit Theorem'/><category term='calls'/><category term='MATLAB scraper'/><category term='CBOT'/><category term='Historical Volatility'/><category term='API'/><category term='BROKERS'/><category term='Volatility Distribution'/><category term='IB Product list scraper'/><category term='Options'/><category term='Sample Size'/><category term='Fidelity'/><category term='Volatility Trading'/><category term='MATLAB'/><category term='matlab2ib'/><category term='CBOE'/><category term='Market Depth'/><category term='He-Litterman'/><category term='Database'/><category term='Stock option prices'/><category term='Straddles'/><category term='Yang and Zhang'/><category term='VIX vs SP 500'/><category term='Implied Volatility'/><category term='MB TRADER'/><category term='puts'/><title type='text'>Quantitative Futures, stocks and Options Trading</title><subtitle type='html'>Quantitative Futures, stocks and Options Trading</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>22</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-192921137515291181</id><published>2011-09-22T21:42:00.000-07:00</published><updated>2011-09-22T21:48:02.265-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Volatility Trading'/><category scheme='http://www.blogger.com/atom/ns#' term='MB TRADER'/><category scheme='http://www.blogger.com/atom/ns#' term='API'/><category scheme='http://www.blogger.com/atom/ns#' term='AMERITRADE'/><category scheme='http://www.blogger.com/atom/ns#' term='BROKERS'/><title type='text'>MATLAB 2 AMERITRADE, MATLAB 2 MB TRADER</title><content type='html'>Hi,&lt;br /&gt;&lt;br /&gt;I will be updating this blog with the two products that I will be releasing soon. As you may know , I have already released the product MATLAB 2 IB three years ago. Now I wil be releasing two more products :&lt;br /&gt;MATLAB 2 AMERITRADE .... A link between MATLAB and TD AMERITRADE API.&lt;br /&gt;MATLAB 2 MB TRADER .... A link between MATLAB and MB TRADER API.&lt;br /&gt;&lt;br /&gt;Would people be interested in such a product ? Please leave your comments in the "Comments section" . I wanted to gauge public interest in these products&lt;br /&gt;&lt;br /&gt;Thanks&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-192921137515291181?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/192921137515291181/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=192921137515291181' title='14 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/192921137515291181'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/192921137515291181'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2011/09/matlab-2-ameritarde-matlab-2-mb-trader.html' title='MATLAB 2 AMERITRADE, MATLAB 2 MB TRADER'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>14</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-7384489925333449399</id><published>2011-01-09T14:22:00.000-08:00</published><updated>2011-09-11T19:08:40.265-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='matlab web scraper'/><category scheme='http://www.blogger.com/atom/ns#' term='yahoo option chains'/><title type='text'>Yahoo Options Data web scraper in matlab</title><content type='html'>I use Yahoo Option Chains Data to quickly check on the Option prices. I wrote a web scraper that conveniently fetches the Yahoo Option Chain Data into matlab. I hope it will be useful for some people. &lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;UPDATE : I have edited the link below. It seems like the link was blocked for some reason.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="https://sites.google.com/site/tradingusingmatlab/matlab-files/Get_Yahoo_Options_Data.m?attredirects=0&amp;amp;d=1"&gt;Yahoo Options Data&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Thanks&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-7384489925333449399?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/7384489925333449399/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=7384489925333449399' title='12 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/7384489925333449399'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/7384489925333449399'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2011/01/yahoo-options-data-web-scraper-in.html' title='Yahoo Options Data web scraper in matlab'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>12</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-2718281377323787038</id><published>2011-01-07T19:26:00.000-08:00</published><updated>2011-01-07T20:06:47.157-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='IB Product list scraper'/><category scheme='http://www.blogger.com/atom/ns#' term='MATLAB scraper'/><title type='text'>Interactive Brokers Product List Web Scraper</title><content type='html'>I use Interactive Brokers to trade for my personal account. Several times I needed to check the Product list page to get the symbols and other information . I wrote a small program to download it automatically into matlab. Hopefully the readers will find it useful.&lt;br /&gt;&lt;br /&gt;The file is available here :&lt;br /&gt;&lt;a href="https://sites.google.com/site/tradingusingmatlab/matlab-files/IB_Product_List_Scrapper.m?attredirects=0&amp;amp;d=1"&gt;IB_Product_List_Scrapper.m&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;tr[^&gt;&lt;td[^&gt;&lt;a[^&gt;&lt;asglu&gt;&lt;agrow&gt;&lt;br /&gt;&lt;/agrow&gt;&lt;/asglu&gt;&lt;/a[^&gt;&lt;/td[^&gt;&lt;/tr[^&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-2718281377323787038?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/2718281377323787038/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=2718281377323787038' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/2718281377323787038'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/2718281377323787038'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2011/01/interactive-brokers-product-list-web.html' title='Interactive Brokers Product List Web Scraper'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-4531564342578990811</id><published>2009-12-20T14:52:00.000-08:00</published><updated>2009-12-20T19:42:23.601-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='MATLAB'/><category scheme='http://www.blogger.com/atom/ns#' term='Johansen'/><category scheme='http://www.blogger.com/atom/ns#' term='Cointegration'/><category scheme='http://www.blogger.com/atom/ns#' term='Dickey-Fuller Test'/><title type='text'>Pairs Trading--Cointegration Testing</title><content type='html'>There are several papers on this topic. A quick google search gives you a list of research papers on this topic. Cointegration technique is sometimes used to do Pairs trading. By checking if a pair of stocks are cointegrated, one could go long on one stock and short on the other (multiplied by Hedge Ratio). We are thus trying to be market neutral. Carol Alexander in the book "Market Models" gives a very good explaination of the theory behind it. &lt;br /&gt;   &lt;br /&gt;A set of I(1) series are termed "cointegrated" if there is a linear combination of these series that is stationary. Stock A and Stock B are cointegrated if A,B are approximately I(1), but there is Hedge Ratio such that &lt;br /&gt;&lt;br /&gt;Spread =  StockA - Hedge_Ratio * StockB  is Approximately I(0). I.e The spread is stationary or mean reverting.&lt;br /&gt;&lt;br /&gt;So We perform the following Steps to Check if two stocks are cointegrated:&lt;br /&gt;&lt;br /&gt;Step1 : Check if the two stocks are atleast integrated of order 1, I(1)&lt;br /&gt;        This is done with Augmented Dickey fuller Test&lt;br /&gt;&lt;br /&gt;Step2: After they pass the above test, perform a Cointegration augmented dickey fuller test&lt;br /&gt;&lt;br /&gt;Step 3: After it passes the ADF test, we can perform a Ordinary Least Squares Regression to get the Hedge Ratio (The Beta of the regression)&lt;br /&gt;&lt;br /&gt;Step 4: So the Spread =  StockA - Hedge_Ratio * StockB would now be Cointegrated (mean reverting)&lt;br /&gt;&lt;br /&gt;Step 5: Figuring when to exit : Calculate Half Life&lt;br /&gt;&lt;br /&gt;Half life basically tells you how much time it takes for the spread to revert back to half the distance of the mean.&lt;br /&gt;&lt;br /&gt;Step 6: Calculate the Spread TODAY. Calculate the Standard deviation of the spread upto the day before. Check how far the current spread is from the historical average. If it is greater than 1.5 standard deviations (or any other threshold), then go short the spread otherwise go long the spread. I.e Go Short Stock A and Long Hedge Ratio * Stock B. Be in the trade until the half life calculated for the pair. If the Half Life time period has passed, Get out of the trade.&lt;br /&gt;&lt;br /&gt;These are simple steps. One should put on more work and research on it to develop it into a practical trading strategy.&lt;br /&gt;&lt;br /&gt;Some interesting papers are here: &lt;br /&gt;&lt;br /&gt;&lt;a href="http://sites.google.com/site/tradingusingmatlab/matlab-files/cointegration-en.pdf?attredirects=0&amp;d=1"&gt;Cointegration Paper I&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://sites.google.com/site/tradingusingmatlab/matlab-files/CarolAlexanderCoint.pdf?attredirects=0&amp;d=1"&gt;Cointegration Carol Alexander&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;MATLAB Code Here:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://sites.google.com/site/tradingusingmatlab/matlab-files/cointegration.zip?attredirects=0&amp;d=1"&gt;MATLAB COINTEGRATION CODE&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;CointPairsTrade.m is the main function and calls all other functions&lt;br /&gt;&lt;br /&gt;Please let me know if there are any bugs&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-4531564342578990811?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/4531564342578990811/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=4531564342578990811' title='31 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/4531564342578990811'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/4531564342578990811'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2009/12/pairs-trading-cointegration-testing.html' title='Pairs Trading--Cointegration Testing'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>31</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-1993448204637503215</id><published>2009-12-06T15:50:00.000-08:00</published><updated>2009-12-06T16:06:15.314-08:00</updated><title type='text'>Calculating Implied Dividend Yield from Option Prices</title><content type='html'>Here is the formula that one could use to calculate the Implied dividend yield:&lt;br /&gt;&lt;br /&gt;PV(Dividend) = -CALL + PUT + (Spot - Strike) + ((Strike * exp(r*T)) - Strike)&lt;br /&gt;&lt;br /&gt;where r is the interest rate to expiration. T is the time to maturity in Years.&lt;br /&gt;&lt;br /&gt;Implied Dividend Yield = PV(Dividend)/(T * Spot)&lt;br /&gt;&lt;br /&gt;Taking the Example of PFE, Pfizer Inc&lt;br /&gt;&lt;br /&gt;Taking the Option prices of June 2010 Expiration&lt;br /&gt;&lt;br /&gt;Spot = 18.49&lt;br /&gt;CALL = 1.72&lt;br /&gt;PUT = 1.52&lt;br /&gt;r = 0.16%&lt;br /&gt;T = 0.5 (Approx)&lt;br /&gt;ATM Strike  = 18&lt;br /&gt;&lt;br /&gt;Substituting the above values into the equation below:&lt;br /&gt;&lt;br /&gt;PV(Div) = -1.72 + 1.52 + (18.49-18)+ ((18 * exp(0.0016*0.5))-18)&lt;br /&gt;&lt;br /&gt;PV(Div) = 0.3044&lt;br /&gt;&lt;br /&gt;Implied Dividend Yield = 0.3044/(0.5 * 18.49) = 0.0329 = 3.29%&lt;br /&gt;&lt;br /&gt;To Express an opinion that the dividend yield will be reduced, one should go long PFE June 2010 18 CALL, Short one June 2010 18 PUT, and SHORT 100 shares of PFE stock.&lt;br /&gt;&lt;br /&gt;To Express an opinion that the dividend yield will be increased, one should go short PFE June 2010 18 CALL, long one June 2010 18 PUT, and long 100 shares of PFE stock.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-1993448204637503215?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/1993448204637503215/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=1993448204637503215' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/1993448204637503215'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/1993448204637503215'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2009/12/calculating-implied-dividend-yield-from.html' title='Calculating Implied Dividend Yield from Option Prices'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-2232880255246544988</id><published>2009-07-26T15:30:00.001-07:00</published><updated>2009-07-26T16:46:04.142-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='rolling window'/><category scheme='http://www.blogger.com/atom/ns#' term='Strangles'/><category scheme='http://www.blogger.com/atom/ns#' term='Straddles'/><title type='text'>A simple way to Backtest Option Straddles</title><content type='html'>Here, I show how one could follow a simple approach to backtest the profitability of Option Straddles. Straddles are a way to get exposure to volatility of a stock. A Long Straddle means buying an AT-THE-MONEY CALL and PUT option of the same expiration date. Vice versa for a Short Straddle position.&lt;br /&gt;&lt;br /&gt;TO profit from a straddle position, One should be able to calculate, historically, how many times did the stock move beyond the premium one would pay for the STRADDLE Position. For example, If a Straddle on a stock costs $4, It makes sense to check how many times in the past did the stock move more than $4, UP or DOWN. I.e if the option expires in 30 days, one needs to find out on any given 30 days in the past, how many times has the stock moved beyond $4. This is only one of the many things one needs to do before buying or selling straddles. This combined with the Volatility Cones and calculation of "Average" spread between Realized Volatility and Implied Volatility should give an investor some information to trade them. One can also look into "Strangle".&lt;br /&gt;&lt;br /&gt;Here is a figure that shows a stocks historical movements over the past 3 years for a 30 day rolling window period.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_iRk9_jAnzAI/SmzcL5kaTuI/AAAAAAAAADI/_044My7ijU8/s1600-h/CNH_Backtest_Straddles.jpg"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 403px; height: 259px;" src="http://4.bp.blogspot.com/_iRk9_jAnzAI/SmzcL5kaTuI/AAAAAAAAADI/_044My7ijU8/s400/CNH_Backtest_Straddles.jpg" alt="" id="BLOGGER_PHOTO_ID_5362903353281629922" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;One can see from the chart that in the past 3 years, CNH has moved beyond $5 only 50% of the time, 60% more than $3.8, 70% more than $2.66 etc. So if a straddle costs only $2.66, then Historically, 70% of the time it has moved more than $2.66 in 30 days. On the other side, there is still a 30% chance that it will not move beyond $2.66.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;MATLAB CODE:&lt;br /&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;&lt;br /&gt;&lt;a style="color: rgb(255, 0, 0);" href="http://sites.google.com/site/tradingusingmatlab/matlab-files/Backtest_Straddles.m?attredirects=0"&gt;Backtest_Straddles.m&lt;/a&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-2232880255246544988?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/2232880255246544988/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=2232880255246544988' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/2232880255246544988'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/2232880255246544988'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2009/07/simple-way-to-backtest-option-straddles.html' title='A simple way to Backtest Option Straddles'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_iRk9_jAnzAI/SmzcL5kaTuI/AAAAAAAAADI/_044My7ijU8/s72-c/CNH_Backtest_Straddles.jpg' height='72' width='72'/><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-8350601118399814710</id><published>2009-06-29T19:30:00.001-07:00</published><updated>2009-07-01T18:19:49.706-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Volatility Cones'/><category scheme='http://www.blogger.com/atom/ns#' term='Volatility Distribution'/><category scheme='http://www.blogger.com/atom/ns#' term='Implied Volatility'/><title type='text'>Using Volatility Cones</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_iRk9_jAnzAI/SklzLe82lRI/AAAAAAAAADA/GbPwvgXSwks/s1600-h/CNH_VolCone0629.jpg"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 400px; height: 300px;" src="http://3.bp.blogspot.com/_iRk9_jAnzAI/SklzLe82lRI/AAAAAAAAADA/GbPwvgXSwks/s400/CNH_VolCone0629.jpg" alt="" id="BLOGGER_PHOTO_ID_5352936273230599442" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;This post describes what volatility cones are and how I usually use them. As with many of my posts, I will attach code to this post.&lt;br /&gt;&lt;br /&gt;Volatility cones are a graphical representation of realized volatility ranges over different time horizons such as 30,60,90,120 days. It gives a volatility distribution. It puts the  current implied volatility into perspective. Some quotes from the book volatility trading by euan sinclair :&lt;br /&gt;&lt;br /&gt;" The volatility cone is very useful for placing current market information  (realized  volatility, implied volatility and the spread between them) into historical context.We may be best served by comparing implied volatility to the historical volatility distribution given by the volatility cone. Selling one-month implied volatility at 35% because this is in the 90th percentile for one-month volatility over the past 2 years can form the basis of a sensible trading plan."&lt;br /&gt;&lt;br /&gt;One needs to calculate the spread between 30 day rolling close to close volatility and the Implied Volatility. One needs to calculate the AVERAGE of the above spread over a sufficient time period.  If the spread is Above normal then one needs to look carefully as an opportunity to trade.&lt;br /&gt;&lt;br /&gt;As an Example, The above plot shows the Volatility cone of stock : CNH as of today 06/29/2009&lt;br /&gt;&lt;br /&gt;The 30, 60, 90, and 120 day rolling volatilities and their percentiles are shown below and plotted above in the figure.&lt;br /&gt;&lt;br /&gt;                30                 60           90      120&lt;br /&gt;               -----------------------------------&lt;br /&gt;Current:  0.6252    0.7363    0.9564    1.1798&lt;br /&gt;Min:         0.1205    0.177       0.19         0.2023&lt;br /&gt;25% :       0.3324    0.3301    0.3318    0.3149&lt;br /&gt;50% :      0.4039    0.4118    0.3943    0.3924&lt;br /&gt;75%:       0.6239    0.6538    0.639      0.6148&lt;br /&gt;90%:      1.1666    1.2162     1.2797      1.1961&lt;br /&gt;MAX:    1.7115    1.5827     1.5581      1.4694&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;As you can see from the above table, CNH 60 day volatility has varied from a minimum of 17.7% to a maximum of 158%.  The close price today is $14.32. The nearest Strike for 08/22/2009 Expiration is $15.  The Implied Volatility of the call Option is 69.5 ( from Options Express website).  The number of days to Expiry is 53. The Implied vol of 69.5 falls between 75% to 90% . So Approx 80% of the time in the past 4 years, the Realized Volatility of 60 days has been below that number of 69.5%. So we are in the third quartile. Whether this is high enough to sell the implied volatility or not depends on the person's risk appetite. This information gives me confidence in making the trade intelligently.&lt;br /&gt;&lt;br /&gt;Code:&lt;br /&gt;&lt;br /&gt;Please download all the three files into the same directory and run VolCones_CC.m&lt;br /&gt;&lt;br /&gt;&lt;a style="font-weight: bold; color: rgb(255, 0, 0);" href="http://sites.google.com/site/tradingusingmatlab/matlab-files/VolCones_CC.m?attredirects=0"&gt;VolCones_CC.m&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a style="font-weight: bold; color: rgb(255, 0, 0);" href="http://sites.google.com/site/tradingusingmatlab/matlab-files/EstimateVol_CC.m?attredirects=0"&gt;EstimateVol_CC.m&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a style="font-weight: bold; color: rgb(255, 0, 0);" href="http://sites.google.com/site/tradingusingmatlab/matlab-files/hist_stock_data.m?attredirects=0"&gt;hist_stock_data.m&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-8350601118399814710?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/8350601118399814710/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=8350601118399814710' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/8350601118399814710'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/8350601118399814710'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2009/06/using-volatility-cones_29.html' title='Using Volatility Cones'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_iRk9_jAnzAI/SklzLe82lRI/AAAAAAAAADA/GbPwvgXSwks/s72-c/CNH_VolCone0629.jpg' height='72' width='72'/><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-4509423436805983843</id><published>2009-06-27T12:56:00.000-07:00</published><updated>2009-06-27T13:13:01.307-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='VIX vs SP 500'/><category scheme='http://www.blogger.com/atom/ns#' term='Expected Returns Range'/><category scheme='http://www.blogger.com/atom/ns#' term='VIX'/><title type='text'>VIX vs S&amp;P 500 Expected Returns Range</title><content type='html'>In this post, I give out a ready to use figure or plot that one could keep handy to relate VIX level to the Expected S&amp;amp;P 500 returns Range over the next 30 days. I also show how the plot is derived. DOUBLE CLICK THE PLOT FOR A BIGGER IMAGE&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_iRk9_jAnzAI/SkZ6yNEcXOI/AAAAAAAAAC4/WOGQR-R0jzc/s1600-h/VIXvsSP500.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 518px; height: 304px;" src="http://1.bp.blogspot.com/_iRk9_jAnzAI/SkZ6yNEcXOI/AAAAAAAAAC4/WOGQR-R0jzc/s400/VIXvsSP500.jpg" alt="" id="BLOGGER_PHOTO_ID_5352100210096757986" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;As you can see from the Above figure, one can easily figure out the expected range of S&amp;amp;P 500 Returns with a given VIX Level for a given Probability. For example, Currently the VIX level is 30%. So looking at the above plot, for 90% Probability,30% VIX,on the X-Axis and following it vertically and across, we can see that over the next 30 days, the expected range for S&amp;amp;P 500 Returns is +/- 14.24% .&lt;br /&gt;&lt;br /&gt;In general, The linear relationship is as follows:&lt;br /&gt;&lt;br /&gt;Expected Range with 50% Probability = 0.1947 * VIX&lt;br /&gt;&lt;br /&gt;Expected Range with 68% Probability = 0.2871 * VIX&lt;br /&gt;&lt;br /&gt;Expected Range with 75% Probability = 0.3321* VIX&lt;br /&gt;&lt;br /&gt;Expected Range with 90% Probability = 0.4748* VIX&lt;br /&gt;&lt;br /&gt;Expected Range with 95% Probability = 0.5658 * VIX&lt;br /&gt;&lt;br /&gt;Expected Range with 99% Probability = 0.7436 * VIX&lt;br /&gt;&lt;br /&gt;Now I show how I derived the Above Plot&lt;br /&gt;&lt;br /&gt;% probabilities&lt;br /&gt;p = [0.50 0.68 0.75 0.90 0.95 0.99]&lt;br /&gt;% Properly adjust those probabilities to be passed into norminv program&lt;br /&gt;p2 = 0.50 + p./2;&lt;br /&gt;&lt;br /&gt;% Before we proceed we should make an important assumption that&lt;br /&gt;% The rate of return on the S&amp;amp;P 500 over the next 30 days is normally&lt;br /&gt;% distributed&lt;br /&gt;&lt;br /&gt;% USe the norminv command to get the number of standard deviations&lt;br /&gt;% that a number drawn from unit normal distribution will be&lt;br /&gt;&lt;br /&gt;nstd = norminv(p2,0,1);&lt;br /&gt;&lt;br /&gt;% since VIX is is an annualized standard deviation, We divide the values&lt;br /&gt;% Obtainded above by sqrt(12)&lt;br /&gt;&lt;br /&gt;nstd = nstd ./ sqrt(12);&lt;br /&gt;&lt;br /&gt;% Now the Linear Relationship between Level of VIX and S&amp;amp;P 500 return range&lt;br /&gt;% would be: ExpRangeSP500  = nstd .* VIX&lt;br /&gt;% Now let us plot using various values of VIX&lt;br /&gt;&lt;br /&gt;VIX = 0:10:100;&lt;br /&gt;ExpRangeSP500  = nstd' * VIX;&lt;br /&gt;&lt;br /&gt;%%PLOT&lt;br /&gt;plot(VIX,ExpRangeSP500,'-*')&lt;br /&gt;set(gca,'YTick',[0:5:max(ExpRangeSP500(:))])&lt;br /&gt;set(gca,'YtickLabel',cellstr(strcat(num2str([0:5:max(ExpRangeSP500(:))]'),'%')))&lt;br /&gt;set(gca,'XTickLabel',cellstr(strcat(num2str(VIX'),'%')))&lt;br /&gt;title('VIX vs S&amp;amp;P 500 returns Expected Range over next 30 days')&lt;br /&gt;xlabel('VIX')&lt;br /&gt;ylabel('S&amp;amp;P 500 returns Expected Range over next 30 days')&lt;br /&gt;plottitles = strcat(cellstr(num2str(p' .*100)),'%');&lt;br /&gt;legend(plottitles,'Location','best')&lt;br /&gt;text(VIX(9)*ones(6,1),ExpRangeSP500(:,9),plottitles,'FontSize',14,'FontWeight','bold')&lt;br /&gt;grid on&lt;br /&gt;axis tight&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-4509423436805983843?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/4509423436805983843/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=4509423436805983843' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/4509423436805983843'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/4509423436805983843'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2009/06/vix-vs-s-500-expected-returns-range.html' title='VIX vs S&amp;P 500 Expected Returns Range'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_iRk9_jAnzAI/SkZ6yNEcXOI/AAAAAAAAAC4/WOGQR-R0jzc/s72-c/VIXvsSP500.jpg' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-287913159095831482</id><published>2009-06-24T10:52:00.000-07:00</published><updated>2009-06-24T20:21:44.485-07:00</updated><title type='text'>Applying VIX methodology to Stocks (American )</title><content type='html'>GOOGLEDOCS file:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/View?id=ddb2j6dw_13dg24q4mk"&gt;http://docs.google.com/View?id=ddb2j6dw_13dg24q4mk&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;iframe marginwidth="0" marginheight="0" src="http://docs.google.com/View?id=ddb2j6dw_13dg24q4mk&amp;amp;output=html&amp;amp;widget=true" scrolling="no" width="500" frameborder="0" height="680"&gt;&lt;/iframe&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In this post I show how one could utilize the VIX methodology for American&lt;br /&gt;Options.VIX was designed with European Type Options. It was designed&lt;br /&gt;for S&amp;P500 Options ( which are European ). But when applied to American &lt;br /&gt;Options,These have a bias due to early exercise and Dividend and disbursement&lt;br /&gt;events. If the forecasted period avoids dividends, then the bias should&lt;br /&gt;be minimal. Neverthelss, It can be used as a valuable forecast or a&lt;br /&gt;technical indicator.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;function  VIX = ReplicateVixStock(Data,TM,Rf,CT)&lt;br /&gt;%REPLICATEVIXSTOCK applies VIX methodology for stocks (American Options)&lt;br /&gt;% VIX was designed with European Type Options. It was designed for S&amp;P500&lt;br /&gt;% Options ( which are European ). But when applied to American Options,&lt;br /&gt;% These have a bias due to early exercise and Dividend and disbursement&lt;br /&gt;% events. If the forecasted period avoids dividends, then the bias should&lt;br /&gt;% be minimal. Neverthelss, It can be used as a valuable forecast or a&lt;br /&gt;% technical indicator.&lt;br /&gt;% Inputs: If NO Inputs are provided, Example will run&lt;br /&gt;% Data: Should be cell array with separate data for two Maturities&lt;br /&gt;%   centered around 30 days. I.e One option expiry must be less than 30&lt;br /&gt;%   days and the other should be greater than 30 days.&lt;br /&gt;%   Data is a three column data with Strike, Call and Put Prices.&lt;br /&gt;%   Data{1} should be Near Term Option Data&lt;br /&gt;%   Data{2} should be far Term Option Data&lt;br /&gt;% TM : Time to maturity for two options&lt;br /&gt;% Rf : Risk free Rate&lt;br /&gt;% CT : Current Time ( Time Stamp when The data was collected )&lt;br /&gt;% Output : VIX-- A single number that Applies the VIX methodology to the&lt;br /&gt;% American Options&lt;br /&gt;% Example : Try running with NO inputs&lt;br /&gt;&lt;br /&gt;if(nargin==0)&lt;br /&gt;    % Near-Term&lt;br /&gt;    % Strike Call Put&lt;br /&gt;    Data{1} =   [75 11.75 0.05;...&lt;br /&gt;        80 6.90 0.08;...&lt;br /&gt;        85 2.40 0.60;...&lt;br /&gt;        90 0.18 3.40;...&lt;br /&gt;        95 0.05 8.30;...&lt;br /&gt;        ];&lt;br /&gt;&lt;br /&gt;    % Next Term&lt;br /&gt;    % Strike Call Put&lt;br /&gt;    Data{2} =   [75 NaN NaN;...&lt;br /&gt;        80 7.70 0.73;...&lt;br /&gt;        85 3.80 1.80;...&lt;br /&gt;        90 1.05 4.05;...&lt;br /&gt;        95 NaN NaN;...&lt;br /&gt;        ];&lt;br /&gt;    %Time_To_Maturity&lt;br /&gt;    TM = [9;37];&lt;br /&gt;    %Risk_Free_Rate&lt;br /&gt;    Rf = 1.1625/100; %Per Annum&lt;br /&gt;    % Current Time&lt;br /&gt;    CT = '12:09:00';&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;% remove NaN Rows&lt;br /&gt;Data{1}((any(isnan(Data{1}),2)),:)=[];&lt;br /&gt;Data{2}((any(isnan(Data{2}),2)),:)=[];&lt;br /&gt;&lt;br /&gt;% Difference between Calls and Puts (Absolute Value)&lt;br /&gt;DF{1} = abs(Data{1}(:,2) - Data{1}(:,3));&lt;br /&gt;DF{2} = abs(Data{2}(:,2) - Data{2}(:,3));&lt;br /&gt;&lt;br /&gt;% FInd Hour, Minute, Second from the time using datevec function&lt;br /&gt;[Year, Month, Day, Hour, Minute, Second] = datevec(CT);&lt;br /&gt;%In Years&lt;br /&gt;%1440 is the number of minutes in a day and 510 is the number&lt;br /&gt;% of minutes to 8:30 AM which is the time the option expires&lt;br /&gt;% on its expiration date&lt;br /&gt;NumYears(1) =[1440 - (Hour * 60 + Minute + Second/60) + 510]/ ...&lt;br /&gt;(1440 * 365) + [(TM(1) - 2)/365];&lt;br /&gt;NumYears(2) =[1440 - (Hour * 60 + Minute + Second/60) + 510]/ ...&lt;br /&gt;(1440 * 365) + [(TM(2) - 2)/365];&lt;br /&gt;% In days&lt;br /&gt;NumDays = NumYears .* 365;&lt;br /&gt;% Find the minimum of the difference in Call and Put&lt;br /&gt;% Prices and Get the corresponding Strike Price.&lt;br /&gt;ATM(1,:) = Data{1}((DF{1}==min(DF{1})),:);&lt;br /&gt;ATM(2,:) = Data{2}((DF{2}==min(DF{2})),:);&lt;br /&gt;% Calculate Forward Price Level and Referential Strike&lt;br /&gt;% Application of PUT CALL Parity&lt;br /&gt;Level = ATM(:,1) + exp(Rf*NumYears(:)) .* (ATM(:,2) - ATM(:,3));&lt;br /&gt;%Reference Strike&lt;br /&gt;for i = 1:2&lt;br /&gt;    Strike = ATM(i,1);&lt;br /&gt;    if(ATM(i,2)&gt;=ATM(i,3))&lt;br /&gt;        Ref_Strike(i)=ATM(i,1);&lt;br /&gt;    else&lt;br /&gt;        Ref_Strike(i) = Data{i}(find(Data{i}(:,1) &lt; ATM(i,1),1,'last'),1);&lt;br /&gt;    end&lt;br /&gt;    % Differences of Strikes&lt;br /&gt;    Temp = diff(Data{i}(:,1));&lt;br /&gt;    Delta_Strike{i} = [Temp(1);Temp];&lt;br /&gt;&lt;br /&gt;% If the strike is above the “reference strike” , use the call price&lt;br /&gt;% If the strike is below the “reference strike” , use the put price&lt;br /&gt;%If the strike equals the “reference strike” , use the average of the call&lt;br /&gt;% and put prices&lt;br /&gt;&lt;br /&gt;cpval= zeros(size(Data{i},1),1);&lt;br /&gt;cid = find(Data{i}(:,1) &gt; Ref_Strike(i));&lt;br /&gt;cpval(cid) = Data{i}(cid,2);&lt;br /&gt;pid = find(Data{i}(:,1) &lt; Ref_Strike(i));&lt;br /&gt;cpval(pid) = Data{i}(pid,3);&lt;br /&gt;Aid = find(Data{i}(:,1) == Ref_Strike(i));&lt;br /&gt;cpval(Aid) = (Data{i}(Aid,2) + Data{i}(Aid,3))/2;&lt;br /&gt;&lt;br /&gt;    % Now do the math as given in the paper vixwhite.pdf&lt;br /&gt;&lt;br /&gt;vix{i} = Delta_Strike{i} * exp(Rf*NumYears(i)) .* cpval ./(Data{i}(:,1).^2);&lt;br /&gt;Var(i) = (2/NumYears(i)) * sum(vix{i}) - ((Level(i)/Ref_Strike(i) ...&lt;br /&gt; - 1).^2)/NumYears(i);&lt;br /&gt;&lt;br /&gt;% Center the data to 30 days&lt;br /&gt;if(i==1)&lt;br /&gt;    Term(i) = NumYears(i) * Var(i) * ((NumDays(i+1)-30)/(NumDays(i+1)-NumDays(i)));&lt;br /&gt;elseif(i==2)&lt;br /&gt;    Term(i) = NumYears(i) * Var(i) * ((-NumDays(i-1)+30)/(NumDays(i)-NumDays(i-1)));&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;end %i&lt;br /&gt;&lt;br /&gt;% Final Vix Calculation&lt;br /&gt;VIX = sqrt(sum(Term) * 365/30) * 100;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-287913159095831482?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/287913159095831482/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=287913159095831482' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/287913159095831482'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/287913159095831482'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2009/06/applying-vix-methodology-to-stocks.html' title='Applying VIX methodology to Stocks (American )'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-2031936941701711980</id><published>2009-06-14T16:41:00.000-07:00</published><updated>2009-06-14T17:43:11.554-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Options'/><category scheme='http://www.blogger.com/atom/ns#' term='Covered Call'/><category scheme='http://www.blogger.com/atom/ns#' term='Writing Options'/><category scheme='http://www.blogger.com/atom/ns#' term='yahoo'/><category scheme='http://www.blogger.com/atom/ns#' term='Database'/><title type='text'>Search for Covered Calls and also Build Options data Database</title><content type='html'>This post is in continuation to my previous blog post on getting the Options Data from websites such as Yahoo, Optionetics and Options Express. I wanted to collect End-Of-Day Options Data from those websites and search for Covered Calls that I could trade. Covered Call is a strategy wherein you buy the stock and write an Out-of-the money CALL option and thus generate monthly income from the stock. This strategy can also be used if you already own a stock and want to earn some income on it. You can also write In the Money Call Option which will give you more downside protection, but less return. At the end of each day one can run the following program and thus store the options Data and use it for further analysis.&lt;br /&gt;&lt;br /&gt;After collecting the data, One could search for those stocks that have the highest premium and which you think are good stocks and wont mind holding on to them.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Note that this function depends on Get_Yahoo_Options_Data2.m function that I talked about in my previous blog post. One can purchase it, if interested.&lt;br /&gt;&lt;br /&gt;%%%%%%%%%% CODE %%%%%%%%%%%%%%%%&lt;br /&gt;&lt;br /&gt;function Out = CoveredCalls(SymbolList)&lt;br /&gt;%CoveredCalls gives the Options Data and Calculates Covered Calls returns&lt;br /&gt;%for a given Symbol for that particular day&lt;br /&gt;% This function can also be used to build a database of Options Data&lt;br /&gt;% on a daily basis.&lt;br /&gt;% NOTE that This function requires Get_Yahoo_Options_Data2.m function&lt;br /&gt;&lt;br /&gt;% Inputs: A cell array of Symbols&lt;br /&gt;% Output: A structure with the following fields:&lt;br /&gt;%       calldata : This contains the Calls data and also has Flat and&lt;br /&gt;%       Exercised returns&lt;br /&gt;%       The colummn names are as follows:&lt;br /&gt;%       {'Symbol','Strike','Last','Change','Bid','Ask','Volume','Open Int',&lt;br /&gt;%       'Expiry','MonthNum','time Value','Exercise Return','Flat Return'};&lt;br /&gt;%       RawData : This contains Both the Calls and Puts Data&lt;br /&gt;%       The column names are as follow:&lt;br /&gt;%   {'Symbol','Last','Change','Bid','Ask','Volume','Open Int','Strike',&lt;br /&gt;%   'Symbol','Last','Change','Bid','Ask','Volume','Open Int',&lt;br /&gt;%   'Expiry','MonthNum','Last Price'};&lt;br /&gt;&lt;br /&gt;% Example:&lt;br /&gt;% Out = CoveredCalls({'cnh','ibm'});&lt;br /&gt;% If user wants a single big cell array, one can get it by using command:&lt;br /&gt;%               vertcat(Out.calldata{:})&lt;br /&gt;&lt;br /&gt;% (c) tradingwithmatlab.blogspot.com&lt;br /&gt;&lt;br /&gt;% Atleast one input is required&lt;br /&gt;if(nargin &lt; 1)&lt;br /&gt;  error('Atleast one Input is needed')&lt;br /&gt;end&lt;br /&gt;% Check if its either a cell array or Character&lt;br /&gt;if~(ischar(SymbolList)||iscell(SymbolList))&lt;br /&gt;  error('SymbolList needs to be either a character or Cell Array')&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;if(iscell(SymbolList) &amp;amp;&amp;amp; ~isvector(SymbolList))&lt;br /&gt;  error('SymbolList needs to be a cell array')&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;% Convert Char to a cell string&lt;br /&gt;SymbolList = cellstr(SymbolList);&lt;br /&gt;&lt;br /&gt;% Number of Symbols&lt;br /&gt;nsymbols = length(SymbolList);&lt;br /&gt;% Initialize&lt;br /&gt;Out.calldata = cell(nsymbols,1);&lt;br /&gt;Out.RawData = cell(nsymbols,1);&lt;br /&gt;&lt;br /&gt;% for each symbol, Get the Options Data and Calculate The returns&lt;br /&gt;&lt;br /&gt;for idx = 1:nsymbols;idx&lt;br /&gt;  % Get Data from YAHOO&lt;br /&gt;  data = Get_Yahoo_Options_Data2(SymbolList{idx});&lt;br /&gt;  % We are interested only in CALL options&lt;br /&gt;&lt;br /&gt;  calldata = data.Calls;&lt;br /&gt;  LastPrice=data.Last;&lt;br /&gt;&lt;br /&gt;  if(isempty(calldata))&lt;br /&gt;      continue&lt;br /&gt;  end&lt;br /&gt;  % Get the Parameters&lt;br /&gt;  Strike = cell2mat(calldata(:,2));&lt;br /&gt;  Bid = cell2mat(calldata(:,5));&lt;br /&gt;  LastPrice = LastPrice(ones(length(Bid),1));&lt;br /&gt;&lt;br /&gt;  % Calculate Time value---If Option Exercised&lt;br /&gt;  TimeValue = Strike + Bid - LastPrice;&lt;br /&gt;  TimeValuePercent = TimeValue./LastPrice;&lt;br /&gt;&lt;br /&gt;  % Calculate Flat return&lt;br /&gt;  FlatReturn = Bid ./ LastPrice;&lt;br /&gt;&lt;br /&gt;  % Store the data&lt;br /&gt;Out.calldata{idx} = [repmat(SymbolList(idx),size(calldata,1),1),...&lt;br /&gt;           calldata num2cell([TimeValue TimeValuePercent FlatReturn])];&lt;br /&gt;    &lt;br /&gt;Out.RawData{idx} = [data.data num2cell(LastPrice)];&lt;br /&gt;end&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-2031936941701711980?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/2031936941701711980/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=2031936941701711980' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/2031936941701711980'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/2031936941701711980'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2009/06/search-for-covered-calls-and-also-build.html' title='Search for Covered Calls and also Build Options data Database'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-5344703747400949178</id><published>2009-06-05T22:07:00.000-07:00</published><updated>2009-06-05T22:29:11.855-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='regexp'/><category scheme='http://www.blogger.com/atom/ns#' term='Convert'/><category scheme='http://www.blogger.com/atom/ns#' term='CUSIP'/><category scheme='http://www.blogger.com/atom/ns#' term='Stock Symbol'/><category scheme='http://www.blogger.com/atom/ns#' term='Fidelity'/><category scheme='http://www.blogger.com/atom/ns#' term='IB matlab2ib'/><category scheme='http://www.blogger.com/atom/ns#' term='CUSIP lookup'/><title type='text'>Convert Stock CUSIPs to Stock Symbols with a MATLAB program</title><content type='html'>CUSIP is a 9 character(alpha-numeric ) identifier. It actually stands for "Committee on Uniform Security Identification Procedures". Sometimes it is very useful to be able to look up the Stock Symbol that the CUSIP represents. I had a list of CUSIPS and some data associated with it. I did not have the Stock Symbols Associated with Them. I was only interested in Stock CUSIPS. I searched online and found out that there was no automated way of finding out the stock symbol associated with stock CUSIPS. So I wrote the following program to look up the CUSIP at the Fidelity website and grab the stock symbol associated with it. I extensively used regular expressions. I hope this program will be useful to others.&lt;br /&gt;&lt;br /&gt;For example:&lt;br /&gt;'031162100' given Amgen Inc---AMGN&lt;br /&gt;Symbols = CusipToSymbolLookUp({'031162100';'03116210';})&lt;br /&gt;&lt;br /&gt;function Symbols = CusipToSymbolLookUp(Cusip)&lt;br /&gt;%CUSIPTOSYMBOLLOOKUP converts STOCK CUSIPS to STOCK Symbols&lt;br /&gt;% Symbols = CusipToSymbolLookUp(Cusip) gives a list of STOCK symbols that&lt;br /&gt;% correspond to a list of Cusips.&lt;br /&gt;% This function looks up a STOCK symbol for a given CUSIP&lt;br /&gt;% The CUSIP needs to be 8 or 9 characters long&lt;br /&gt;% If a valid 8 character Cusip is given, then a 9th check digit is added if&lt;br /&gt;% possible.It accesses the fidelity website and does html parsing&lt;br /&gt;% to get the symbol name. Cusip can be a cell array of Cusips&lt;br /&gt;%    More information on CUSIPs can be found at:&lt;br /&gt;%       http://www.cusip.com&lt;br /&gt;% I Thank Nabeel Azar for his program checkcusip.m&lt;br /&gt;% Example:&lt;br /&gt;% Symbols = CusipToSymbolLookUp({'031162100';'03116210';})&lt;br /&gt;&lt;br /&gt;% Atleast one input is required&lt;br /&gt;if(nargin &lt; 1)&lt;br /&gt;    error('Atleast one Input is needed')&lt;br /&gt;end&lt;br /&gt;% Check if its either a cell array or Character&lt;br /&gt;if~(ischar(Cusip)||iscell(Cusip))&lt;br /&gt;    error('Cusip needs to be either a character or Cell Array')&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;if(iscell(Cusip) &amp;amp;&amp;amp; ~isvector(Cusip))&lt;br /&gt;    error('Cusip needs to be a cell array')&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;% Convert Char to a cell string&lt;br /&gt;Cusip = cellstr(Cusip);&lt;br /&gt;&lt;br /&gt;% Find how many cusips were given&lt;br /&gt;ncusips = length(Cusip);&lt;br /&gt;&lt;br /&gt;% Intial Web URL&lt;br /&gt;weburl = ['http://activequote.fidelity.com/mmnet/',...&lt;br /&gt;        'SymLookup.phtml?QUOTE_TYPE=&amp;amp;',...&lt;br /&gt;        'scCode=E&amp;amp;searchBy=C&amp;amp;'];&lt;br /&gt;   &lt;br /&gt;% Pre assign the Output    &lt;br /&gt;Symbols = cell(ncusips,1);&lt;br /&gt;&lt;br /&gt;% Now go through the List and do the processing&lt;br /&gt;for idx = 1:ncusips&lt;br /&gt;   &lt;br /&gt;    % If The Length of the Cusip is 8 digits/characters long,&lt;br /&gt;    % Then It is converted into 9 digits using a program called checkcusip&lt;br /&gt;    % If it returns a logical false, then it is a wrong CUSIP&lt;br /&gt;    % If it returns a double digit, then join the checkdigit to the&lt;br /&gt;    % original CUSIP&lt;br /&gt;   &lt;br /&gt;    if(length(Cusip{idx})==8)&lt;br /&gt;        Result = checkcusip(Cusip(idx));&lt;br /&gt;        if (islogical(Result{1}) &amp;amp;&amp;amp; Result{1}==false)&lt;br /&gt;        continue&lt;br /&gt;        else&lt;br /&gt;            % Join the Cusip with CheckDigit&lt;br /&gt;            Cusip{idx} = [Cusip{idx} num2str(Result{:})];&lt;br /&gt;        end&lt;br /&gt;       &lt;br /&gt;        % If the Length is not equal to 9, then just continue&lt;br /&gt;    elseif(length(Cusip{idx})~=9)&lt;br /&gt;        continue&lt;br /&gt;    end&lt;br /&gt;   &lt;br /&gt;    % Construct the URL using the Cusip and read the url&lt;br /&gt;    data = urlread([weburl 'searchFor=' Cusip{idx} '&amp;amp;submit=Search']);&lt;br /&gt;   &lt;br /&gt;    % Search for a preliminary pattern&lt;br /&gt;    pat = '&lt;(a HREF).*?&gt;.*?&lt;/td&gt;';&lt;br /&gt;   &lt;br /&gt;    % Use regexp to match the pattern&lt;br /&gt;    data2=regexp(data,pat,'match');&lt;br /&gt;    sname = '';&lt;br /&gt;   &lt;br /&gt;    % Use another pattern to get the symbol&lt;br /&gt;    if(~isempty(data2))&lt;br /&gt;    pat = '&gt;\w*&lt;';&lt;br /&gt;    sname=char(regexp(data2{1},pat,'match'));&lt;br /&gt;    end&lt;br /&gt;   &lt;br /&gt;    % Pre-Process the Output before storing it in an array&lt;br /&gt;    if(~isempty(sname))&lt;br /&gt;        sname([1 end])='';&lt;br /&gt;        Symbols{idx} = sname;&lt;br /&gt;    end&lt;br /&gt;   &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;function Result = checkcusip(inputCell)&lt;br /&gt;%CHECKCUSIP Check a CUSIP&lt;br /&gt;%   CHECKCUSIP is used to validate 9 digit CUSIPs and&lt;br /&gt;%   provide the checkdigit for 8 digit CUSIPs.&lt;br /&gt;%&lt;br /&gt;%   Note that if you give this function a combination of &lt;br /&gt;%   8 and 9 digit CUSIPs, you need to check both the class&lt;br /&gt;%   (logical or non-logical) as well as the value of the&lt;br /&gt;%   output.  Logicals are used to indicate validity of a&lt;br /&gt;%   9 digit CUSIP, while non-logical doubles are used to&lt;br /&gt;%   supply the checkdigit for an 8 digit CUSIP.&lt;br /&gt;&lt;br /&gt;%   Convert the input to a char array&lt;br /&gt;if iscell(inputCell)&lt;br /&gt;    cusipCharArray = strvcat(inputCell{:});&lt;br /&gt;else&lt;br /&gt;    error(['Inputs must be cell arrays of CUSIP strings.'])&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;%   Make sure there are at least 8 columns&lt;br /&gt;if size(cusipCharArray,2)&lt;8&lt;br /&gt;    error(['Must supply 8 or 9 digit CUSIPs.']);&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;%   Make them all lowercase&lt;br /&gt;cusipCharArray = lower(cusipCharArray);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br /&gt;%   Convert the string digits to numerical values and&lt;br /&gt;%   the characters to their numerical values, with 'A':=10&lt;br /&gt;%   Set spaces (for computing the checkdigit) to NaNs&lt;br /&gt;&lt;br /&gt;%   Transpose the array, and work down the columns.&lt;br /&gt;longCusipString    = double(cusipCharArray);&lt;br /&gt;longCusipString    = longCusipString.';&lt;br /&gt;numericalLocations = (longCusipString&gt;='0' &amp;amp; longCusipString&lt;='9');&lt;br /&gt;charLocations      = (longCusipString&gt;='a' &amp;amp; longCusipString&lt;='z');&lt;br /&gt;NaNLocations       = longCusipString==' ';&lt;br /&gt;&lt;br /&gt;longCusipString(numericalLocations) = longCusipString(numericalLocations) - '0';&lt;br /&gt;longCusipString(charLocations)      = longCusipString(charLocations) - 'a' + 10;&lt;br /&gt;longCusipString(NaNLocations)       = NaN;&lt;br /&gt;&lt;br /&gt;%   Get the cusip digits&lt;br /&gt;cusipNums = longCusipString(1:8,:);&lt;br /&gt;&lt;br /&gt;%   Scale with scaling factors&lt;br /&gt;cusipNums = diag([1 2 1 2 1 2 1 2]) * cusipNums;&lt;br /&gt;&lt;br /&gt;%   Sum the digits in each term &gt;=10;&lt;br /&gt;gt_10 = cusipNums&gt;=10;&lt;br /&gt;cusipNums(gt_10) = floor(cusipNums(gt_10)/10) + rem(cusipNums(gt_10),10);&lt;br /&gt;&lt;br /&gt;%   Sum the resulting values&lt;br /&gt;cusipNums = sum(cusipNums);&lt;br /&gt;&lt;br /&gt;%   Get the last digit&lt;br /&gt;lastDigit = rem(cusipNums,10);&lt;br /&gt;&lt;br /&gt;%   Generate the checkdigit&lt;br /&gt;checkDigit = 10 - lastDigit;&lt;br /&gt;checkDigit(checkDigit==10) = 0;&lt;br /&gt;&lt;br /&gt;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;%   Create a cell array the right size for the output.&lt;br /&gt;Result = cell(numel(inputCell),1);&lt;br /&gt;&lt;br /&gt;%   If no check digit was given in the input, output the checkdigit&lt;br /&gt;if size(longCusipString,1)==9&lt;br /&gt;    needCusip = isnan(longCusipString(9,:));&lt;br /&gt;else&lt;br /&gt;    needCusip = logical(ones(1,size(longCusipString,2)));&lt;br /&gt;end&lt;br /&gt;Result(needCusip) = num2cell(checkDigit(needCusip));&lt;br /&gt;&lt;br /&gt;%   If a check digit was given, validate it&lt;br /&gt;if size(longCusipString,1)==9&lt;br /&gt;    isCheckdigitCorrect = longCusipString(9,~needCusip)==checkDigit(~needCusip);&lt;br /&gt;    Result(~needCusip) = num2cell(isCheckdigitCorrect);&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;%   Only the 1st, 4th, 5th, or 6th digit may be an alphanumeric letter&lt;br /&gt;%   (1st for international issues)&lt;br /&gt;badIdx = any(longCusipString([2 3 7 8],:)&gt;=10,1);&lt;br /&gt;Result(badIdx) = {logical(0)};&lt;br /&gt;&lt;br /&gt;%   The first digit cannot be an i, o, or z&lt;br /&gt;badIdx = any(longCusipString(1,:)=='i' | longCusipString(1,:)=='o' | longCusipString(1,:)=='z',1);&lt;br /&gt;Result(badIdx) = {logical(0)};&lt;br /&gt;&lt;br /&gt;%   Reshape the result&lt;br /&gt;Result = reshape(Result,size(inputCell));&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-5344703747400949178?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/5344703747400949178/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=5344703747400949178' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/5344703747400949178'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/5344703747400949178'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2009/06/convert-stock-cusips-to-stock-symbols.html' title='Convert Stock CUSIPs to Stock Symbols with a MATLAB program'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-7371190568992296209</id><published>2009-02-04T18:49:00.000-08:00</published><updated>2009-04-12T11:42:41.795-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='CBOE'/><category scheme='http://www.blogger.com/atom/ns#' term='calls'/><category scheme='http://www.blogger.com/atom/ns#' term='puts'/><category scheme='http://www.blogger.com/atom/ns#' term='New vix methodology'/><category scheme='http://www.blogger.com/atom/ns#' term='VIX'/><title type='text'>Replicate VIX (CBOE Volatility Index) --MATLAB Implementation</title><content type='html'>This post is just to demonstrate how to replicate the Calculations behind the CBOE Volatility Index, Commonly called VIX. It is also commonly thought of investor gauge of fear. One can read more about at www.cboe.com&lt;br /&gt;&lt;br /&gt;The Implementation below follows the methodology as illustrated in the following white paper:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.cboe.com/micro/vix/vixwhite.pdf"&gt;http://www.cboe.com/micro/vix/vixwhite.pdf&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;M FILE is here:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?id=ddb2j6dw_12fjk57bfx"&gt;http://docs.google.com/Doc?id=ddb2j6dw_12fjk57bfx&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;iframe marginwidth="0" marginheight="0" src="http://docs.google.com/Doc?id=ddb2j6dw_12fjk57bfx&amp;amp;output=html&amp;amp;widget=true" scrolling="no" width="560" frameborder="0" height="680"&gt;&lt;/iframe&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;CODE Published Here:&lt;br /&gt;&lt;br /&gt;%% ReplicateVIX&lt;br /&gt;% Near-Term&lt;br /&gt;% Strike Call Put&lt;br /&gt;Data{1} = [775 125.48 0.11;...&lt;br /&gt;      800 100.79 0.41;...&lt;br /&gt;      825 76.70 1.30;...&lt;br /&gt;      850 54.01 3.60;...&lt;br /&gt;      875 34.05 8.64;...&lt;br /&gt;      900 18.41 17.98;...&lt;br /&gt;      925 8.07 32.63;...&lt;br /&gt;      950 2.68 52.33;...&lt;br /&gt;      975 0.62 75.16;...&lt;br /&gt;      1000 0.09 99.61;...&lt;br /&gt;      1025 0.01 124.52];&lt;br /&gt;% Next Term &lt;br /&gt;% Strike Call Put&lt;br /&gt;Data{2} = [775 128.78 2.72;...&lt;br /&gt;      800 105.85 4.76;...&lt;br /&gt;      825 84.14 8.01;...&lt;br /&gt;      850 64.13 12.97;...&lt;br /&gt;      875 46.38 20.18;...&lt;br /&gt;      900 31.40 30.17;...&lt;br /&gt;      925 19.57 43.31;...&lt;br /&gt;      950 11.00 59.70;...&lt;br /&gt;      975 5.43 79.10;...&lt;br /&gt;      1000 2.28 100.91;...&lt;br /&gt;      1025 0.78 124.38];&lt;br /&gt;&lt;br /&gt; %Time_To_Maturity&lt;br /&gt; TM = [16;44];     &lt;br /&gt; %Risk_Free_Rate  &lt;br /&gt; Rf = 1.1625/100; %Per Annum&lt;br /&gt; % Difference between Calls and Puts (Absolute Value)&lt;br /&gt; DF{1} = abs(Data{1}(:,2) - Data{1}(:,3));&lt;br /&gt; DF{2} = abs(Data{2}(:,2) - Data{2}(:,3));&lt;br /&gt; % Current Time&lt;br /&gt; CT = '08:30:00';&lt;br /&gt; % FInd Hour, Minute, Second from the time using datevec function&lt;br /&gt; [Year, Month, Day, Hour, Minute, Second] = datevec(CT);&lt;br /&gt; %In Years&lt;br /&gt; %1440 is the number of minutes in a day and 510 is the number of minutes to 8:30 AM which&lt;br /&gt; %is the time the option expires on its expiration date&lt;br /&gt; NumYears(1) = [1440 - (Hour * 60 + Minute + Second/60) + 510]/(1440 * 365) + [(TM(1) - 2)/365];&lt;br /&gt; NumYears(2) = [1440 - (Hour * 60 + Minute + Second/60) + 510]/(1440 * 365) + [(TM(2) - 2)/365];&lt;br /&gt; % In days&lt;br /&gt; NumDays = NumYears .* 365;&lt;br /&gt; % Find the minimum of the difference in Call and Put&lt;br /&gt; % Prices and Get the corresponding Strike Price.&lt;br /&gt; ATM(1,:) = Data{1}(find(DF{1}==min(DF{1})),:);&lt;br /&gt; ATM(2,:) = Data{2}(find(DF{2}==min(DF{2})),:);&lt;br /&gt; % Calculate Forward Price Level and Referential Strike&lt;br /&gt; % Application of PUT CALL Parity&lt;br /&gt; Level = ATM(:,1) + exp(Rf*NumYears(:)) .* (ATM(:,2) - ATM(:,3));&lt;br /&gt; %Reference Strike&lt;br /&gt; for i = 1:2&lt;br /&gt;     Strike = ATM(i,1);&lt;br /&gt;     if(ATM(i,2)&gt;=ATM(i,3))&lt;br /&gt;         Ref_Strike(i)=ATM(i,1);&lt;br /&gt;     else&lt;br /&gt;         Ref_Strike(i) = Data{i}(find(Data{i}(:,1) &lt; ATM(i,1),1,'last'),1);&lt;br /&gt;     end&lt;br /&gt;     % Differences of Strikes&lt;br /&gt;     Temp = diff(Data{i}(:,1));&lt;br /&gt;     Delta_Strike{i} = [Temp(1);Temp];&lt;br /&gt;   %{&lt;br /&gt;    ? If the strike is above the “reference strike” , use the call price&lt;br /&gt;    ? If the strike is below the “reference strike” , use the put price&lt;br /&gt;    ? If the strike equals the “reference strike” , use the average of the call&lt;br /&gt;      and put prices&lt;br /&gt;     %}&lt;br /&gt;     cpval= zeros(size(Data{i},1),1);&lt;br /&gt;     cid = find(Data{i}(:,1) &gt; Ref_Strike(i));&lt;br /&gt;     cpval(cid) = Data{i}(cid,2);&lt;br /&gt;     pid = find(Data{i}(:,1) &lt; Ref_Strike(i));&lt;br /&gt;     cpval(pid) = Data{i}(pid,3);&lt;br /&gt;     Aid = find(Data{i}(:,1) == Ref_Strike(i));&lt;br /&gt;     cpval(Aid) = (Data{i}(Aid,2) + Data{i}(Aid,3))/2;&lt;br /&gt;    &lt;br /&gt;     % Now do the math as given in the paper vixwhite.pdf&lt;br /&gt;    &lt;br /&gt;     vix{i} = Delta_Strike{i} * exp(Rf*NumYears(i)) .* cpval ./(Data{i}(:,1).^2);&lt;br /&gt;     Var(i) = (2/NumYears(i)) * sum(vix{i}) - ((Level(i)/Ref_Strike(i) - 1).^2)/NumYears(i);&lt;br /&gt;    &lt;br /&gt;     % Center the data to 30 days&lt;br /&gt;     if(i==1)&lt;br /&gt;        Term(i) = NumYears(i) * Var(i) * ((NumDays(i+1)-30)/(NumDays(i+1)-NumDays(i)));&lt;br /&gt;     elseif(i==2)&lt;br /&gt;        Term(i) = NumYears(i) * Var(i) * ((-NumDays(i-1)+30)/(NumDays(i)-NumDays(i-1)));&lt;br /&gt;     end&lt;br /&gt;        &lt;br /&gt;    &lt;br /&gt; end&lt;br /&gt;&lt;br /&gt;    % Final Vix Calculation&lt;br /&gt;    VIX = sqrt(sum(Term) * 365/30) * 100&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-7371190568992296209?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/7371190568992296209/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=7371190568992296209' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/7371190568992296209'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/7371190568992296209'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2009/02/replicate-vix-cboe-volatility-index.html' title='Replicate VIX (CBOE Volatility Index) --MATLAB Implementation'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-5461196661718897719</id><published>2008-11-17T13:08:00.000-08:00</published><updated>2008-11-17T20:19:30.279-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Why 22? Why 30? Central Limit Theorem'/><category scheme='http://www.blogger.com/atom/ns#' term='Sample Size'/><category scheme='http://www.blogger.com/atom/ns#' term='normal distribution'/><title type='text'>Sample Size Determination and History behind Rules of Thumbs( Why 22? Why 25? Why 30?)</title><content type='html'>&lt;div align="center"&gt;          &lt;a href="http://www.blogger.com/post-create.g?blogID=1440440391871056920#1"&gt;Sample Size equation that is predicated on Central Limit Theorem&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;&lt;div align="center"&gt;&lt;a href="http://www.blogger.com/post-create.g?blogID=1440440391871056920#2"&gt;Correction Factor for Sample Size based on finite Population Size&lt;/a&gt;&lt;/div&gt;&lt;/li&gt;&lt;p align="center"&gt;&lt;a href="http://www.blogger.com/post-create.g?blogID=1440440391871056920#4"&gt;Plot of SampleSize Required vs Probability:&lt;/a&gt;&lt;/p&gt;&lt;p align="center"&gt;&lt;a href="http://www.blogger.com/post-create.g?blogID=1440440391871056920#5"&gt;T-distribution table VS Sample Size and closeness to Standard Normal:&lt;/a&gt;&lt;/p&gt;&lt;p align="center"&gt;&lt;a href="http://www.blogger.com/post-create.g?blogID=1440440391871056920#6"&gt;Survey Example: Some tests for Sample Sizes:&lt;/a&gt;&lt;/p&gt;&lt;p align="center"&gt;&lt;a href="http://www.blogger.com/post-create.g?blogID=1440440391871056920#7"&gt;NULL HYPOTHESIS:&lt;/a&gt;&lt;/p&gt;&lt;p align="center"&gt;&lt;a href="http://www.blogger.com/post-create.g?blogID=1440440391871056920#8"&gt;T-stat formula&lt;/a&gt;&lt;/p&gt;&lt;p align="center"&gt;&lt;a href="http://www.blogger.com/post-create.g?blogID=1440440391871056920#9"&gt;Reverse Experiment: calculating Percentages Needed with a given Sample&lt;/a&gt;&lt;a href="http://www.blogger.com/post-create.g?blogID=1440440391871056920#10"&gt;Size to have statistical significance.&lt;/a&gt;&lt;/p&gt;&lt;li&gt;&lt;div align="center"&gt;&lt;a href="http://www.blogger.com/post-create.g?blogID=1440440391871056920#13"&gt;History behind "Rules-of-thumb" (Why 22? 25? 30?)&lt;/a&gt;&lt;/div&gt;&lt;/li&gt;&lt;p align="center"&gt;&lt;a href="http://www.blogger.com/post-create.g?blogID=1440440391871056920#14"&gt;References:&lt;/a&gt;&lt;/p&gt;&lt;/ul&gt;&lt;strong&gt;&lt;span style="font-size:130%;"&gt;Sample Size equation that is predicated on Central Limit Theorem&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="http://3.bp.blogspot.com/_iRk9_jAnzAI/SSHfgoVFb9I/AAAAAAAAABw/meaaWaY7oLs/s1600-h/SampleSize2_eq69388.png"&gt;&lt;img id="BLOGGER_PHOTO_ID_5269738790674853842" style="FLOAT: left; MARGIN: 0px 10px 10px 0px; WIDTH: 320px; CURSOR: hand; HEIGHT: 14px" alt="" src="http://3.bp.blogspot.com/_iRk9_jAnzAI/SSHfgoVFb9I/AAAAAAAAABw/meaaWaY7oLs/s320/SampleSize2_eq69388.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;&lt;/h2&gt;&lt;h2&gt;&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Correction Factor for Sample Size based on finite Population Size&lt;a name="2"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;&lt;a href="http://2.bp.blogspot.com/_iRk9_jAnzAI/SSHfWeFd6uI/AAAAAAAAABo/qWMnb29weRk/s1600-h/SampleSize2_eq48406.png"&gt;&lt;img id="BLOGGER_PHOTO_ID_5269738616126302946" style="FLOAT: left; MARGIN: 0px 10px 10px 0px; WIDTH: 320px; CURSOR: hand; HEIGHT: 14px" alt="" src="http://2.bp.blogspot.com/_iRk9_jAnzAI/SSHfWeFd6uI/AAAAAAAAABo/qWMnb29weRk/s320/SampleSize2_eq48406.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;For a 95% Level, Z = 1.96. Let us say that we want the result to be within 5% error--Confidence Interval and lets have P varying&lt;br /&gt;from 0.1 to 0.9. Then inserting the above numbers into the equation, we get:&lt;br /&gt;ConfidenceInterval = 0.05;Z = 1.96;&lt;br /&gt;&lt;br /&gt;P = 0.1:0.1:0.9;&lt;br /&gt;SampleSize = [Z^2 .* P .* (1-P)]/(ConfidenceInterval^2);&lt;br /&gt;SampleSizeRequired = [P ;SampleSize].'&lt;br /&gt;SampleSizeRequired =&lt;br /&gt;0.1000 138.2976&lt;br /&gt;0.2000 245.8624&lt;br /&gt;0.3000 322.6944&lt;br /&gt;0.4000 368.7936&lt;br /&gt;0.5000 384.1600&lt;br /&gt;0.6000 368.7936&lt;br /&gt;0.7000 322.6944&lt;br /&gt;0.8000 245.8624&lt;br /&gt;0.9000 138.2976&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Plot of SampleSize Required vs Probability:&lt;/strong&gt;&lt;a name="4"&gt;&lt;/a&gt;&lt;/p&gt;&lt;pre class="codeinput"&gt;figure()&lt;br /&gt;&lt;br /&gt;plot(SampleSizeRequired(:,1),SampleSizeRequired(:,2))&lt;br /&gt;&lt;br /&gt;xlabel(&lt;span class="string"&gt;'Probability P'&lt;/span&gt;)&lt;br /&gt;&lt;br /&gt;ylabel(&lt;span class="string"&gt;'Sample Size Required'&lt;/span&gt;)&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_iRk9_jAnzAI/SSHfoOrCHAI/AAAAAAAAACA/I1WtMvKkxAo/s1600-h/SampleSize2_01.png"&gt;&lt;img id="BLOGGER_PHOTO_ID_5269738921226542082" style="FLOAT: left; MARGIN: 0px 10px 10px 0px; WIDTH: 370px; CURSOR: hand; HEIGHT: 185px" alt="" src="http://2.bp.blogspot.com/_iRk9_jAnzAI/SSHfoOrCHAI/AAAAAAAAACA/I1WtMvKkxAo/s320/SampleSize2_01.png" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;T-distribution table VS Sample Size and closeness to Standard Normal:&lt;/h2&gt;&lt;h2&gt;(Source: Jeffrey Russell NOTES)&lt;a name="5"&gt;&lt;/a&gt;&lt;/h2&gt;T distribution with n-1 degrees of freedom at 0.05 level( 95%) It becomes normal when T is Approx 2 ( 1.96 to be exact)&lt;br /&gt;&lt;pre class="codeinput"&gt;&lt;span class="comment"&gt;% T     n&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;[4.303   3&lt;br /&gt;2.228   11&lt;br /&gt;&lt;strong&gt;2.086   21&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;2.042   31&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;2.00    61&lt;/strong&gt;];&lt;br /&gt;&lt;br /&gt;&lt;span class="comment"&gt;% As can be seen above Starting from about &lt;strong&gt;21&lt;/strong&gt; we can see it get closer to 2&lt;/span&gt;&lt;/pre&gt;&lt;h2&gt;Survey Example: Some tests for Sample Sizes:&lt;/h2&gt;&lt;h2&gt;&lt;a name="6"&gt;(Source: Jeffrey Russell NOTES)&lt;/a&gt;&lt;a name="5"&gt;&lt;/a&gt;&lt;a name="6"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;pre class="codeinput"&gt;&lt;span class="comment"&gt;Assume IID (Independent Identical distribution). So now let us say we&lt;/span&gt;&lt;br /&gt;&lt;span class="comment"&gt;need to do a survey. Let us assume that we have&lt;/span&gt;&lt;br /&gt;&lt;span class="comment"&gt;ONLY 21 (From the above Table) people to survey from.&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span class="comment"&gt;After survey we found that 60% People say A and 40% say B.&lt;/span&gt; &lt;span class="comment"&gt;Now We need to&lt;/span&gt;&lt;span class="comment"&gt; find out if it is significant or not. We need to find out by calculating&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="comment"&gt;% the T-stat and comparing it with 2 (1.96 to be exact).&lt;/span&gt;&lt;br /&gt;&lt;h2&gt;NULL HYPOTHESIS:&lt;/h2&gt;&lt;p&gt;IT IS TIE (50-50). I.e Even though 60% of the people say A, we want to make sure it is different from being a TIE (50-50)&lt;br /&gt;when a bigger population is considered.&lt;br /&gt;&lt;/p&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;strong&gt;T-stat formula ( Any Basic Stats Textbook is the source here)&lt;/strong&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;T_Stat = (P - p0)/sqrt(p0 * (1-p0)/n); Where p0 is NULL hypothesis. (0.50 in our case)&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;pre class="codeinput"&gt;&lt;span class="comment"&gt;% Now lets put some values into the equation:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;P = 0.60; p0 = 0.50;&lt;span class="comment"&gt;%( NULL Hypothesis)&lt;/span&gt;&lt;br /&gt;n = 21;&lt;br /&gt;t_stat = (P - p0)/sqrt(p0 * (1-p0)/n)&lt;/pre&gt;&lt;pre class="codeinput"&gt;t_stat = 0.9165&lt;/pre&gt;&lt;pre class="codeinput"&gt;&lt;span class="comment"&gt;% We GOT &lt;strong&gt;0.9165&lt;/strong&gt; Which is LESS than &lt;strong&gt;2&lt;/strong&gt;. So we &lt;strong&gt;FAIL&lt;/strong&gt; to &lt;strong&gt;reject&lt;/strong&gt; the &lt;strong&gt;NUL&lt;/strong&gt;L&lt;/span&gt;&lt;br /&gt;&lt;span class="comment"&gt;% Hypothesis. So we cannot say with certainty that it is NOT a TIE.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="comment"&gt;% Now lets increase the n to 30.&lt;/span&gt;&lt;br /&gt;n=30;&lt;br /&gt;t_stat = (P - p0)/sqrt(p0 * (1-p0)/n)&lt;/pre&gt;&lt;p&gt;t_stat = 1.0954&lt;br /&gt;&lt;br /&gt;&lt;span class="comment"&gt;% We GOT 1.0954 Which is STILL LESS than 2. So we &lt;strong&gt;FAIL&lt;/strong&gt; to reject the &lt;strong&gt;NULL&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="comment"&gt;% Hypothesis. So we cannot say with certainty that it is NOT a TIE.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="comment"&gt;% Now lets go further and calculate it for a bunch of n&lt;/span&gt;&lt;br /&gt;n = 30:10:100;&lt;br /&gt;t_stat = (P - p0)./sqrt(p0 * (1-p0)./n);&lt;br /&gt;&lt;br /&gt;figure()&lt;br /&gt;plot(n,t_stat)&lt;br /&gt;xlabel(&lt;span class="string"&gt;'Sample Size'&lt;/span&gt;)&lt;br /&gt;ylabel(&lt;span class="string"&gt;'T stat'&lt;/span&gt;)&lt;br /&gt;&lt;br /&gt;&lt;span class="comment"&gt;% BINGO: We got it. So at a sample Size of 100, we get Exactly what we&lt;/span&gt;&lt;br /&gt;&lt;span class="comment"&gt;% wanted. We REJECT the NULL Hypothesis that It was a TIE and declare A as&lt;/span&gt;&lt;br /&gt;&lt;span class="comment"&gt;% our winner in the survey.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;h2&gt;&lt;/h2&gt;&lt;h2&gt;&lt;a href="http://4.bp.blogspot.com/_iRk9_jAnzAI/SSHfuJoQU3I/AAAAAAAAACQ/xXt9mAOIfQI/s1600-h/SampleSize2_04.png"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_iRk9_jAnzAI/SSHl0NQ8YVI/AAAAAAAAACg/A5bu8_NMzec/s1600-h/SampleSize2_02.png"&gt;&lt;img id="BLOGGER_PHOTO_ID_5269745724076876114" style="FLOAT: left; MARGIN: 0px 10px 10px 0px; WIDTH: 320px; CURSOR: hand; HEIGHT: 240px" alt="" src="http://4.bp.blogspot.com/_iRk9_jAnzAI/SSHl0NQ8YVI/AAAAAAAAACg/A5bu8_NMzec/s320/SampleSize2_02.png" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;h2&gt;&lt;a href="http://4.bp.blogspot.com/_iRk9_jAnzAI/SSHfuJoQU3I/AAAAAAAAACQ/xXt9mAOIfQI/s1600-h/SampleSize2_04.png"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;&lt;a href="http://4.bp.blogspot.com/_iRk9_jAnzAI/SSHfuJoQU3I/AAAAAAAAACQ/xXt9mAOIfQI/s1600-h/SampleSize2_04.png"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;&lt;a href="http://4.bp.blogspot.com/_iRk9_jAnzAI/SSHfuJoQU3I/AAAAAAAAACQ/xXt9mAOIfQI/s1600-h/SampleSize2_04.png"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;&lt;/h2&gt;&lt;h2&gt;&lt;/h2&gt;&lt;h2&gt;&lt;/h2&gt;&lt;h2&gt; &lt;/h2&gt;&lt;h2&gt; &lt;/h2&gt;&lt;h2&gt; &lt;/h2&gt;&lt;h2&gt;&lt;br /&gt;&lt;/h2&gt;&lt;h2&gt;&lt;br /&gt;&lt;/h2&gt;&lt;h2&gt;Reverse Experiment: calculating Percentages Needed with a given Sample Size to have statistical significance.&lt;a name="10"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;pre class="codeinput"&gt;&lt;span class="comment"&gt;% So here we need to find out for what P value is needed.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="comment"&gt;% With Some Linear Algebraic Manipulations, we get:&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;&lt;a href="http://4.bp.blogspot.com/_iRk9_jAnzAI/SSHfS7KViKI/AAAAAAAAABg/gLMWXzHOFYY/s1600-h/SampleSize2_eq23633.png"&gt;&lt;img id="BLOGGER_PHOTO_ID_5269738555211876514" style="FLOAT: left; MARGIN: 0px 10px 10px 0px; WIDTH: 230px; CURSOR: hand; HEIGHT: 15px" alt="" src="http://4.bp.blogspot.com/_iRk9_jAnzAI/SSHfS7KViKI/AAAAAAAAABg/gLMWXzHOFYY/s320/SampleSize2_eq23633.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;pre class="codeinput"&gt;&lt;span class="comment"&gt;% Now find out for different n , what P is needed..&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;n = 21:30;&lt;br /&gt;P = p0 + [2 * sqrt(p0 * (1-p0)./n)];&lt;br /&gt;&lt;br /&gt;figure()&lt;br /&gt;&lt;br /&gt;plot(n,P)&lt;br /&gt;&lt;br /&gt;xlabel(&lt;span class="string"&gt;'Sample Size'&lt;/span&gt;)&lt;br /&gt;&lt;br /&gt;ylabel(&lt;span class="string"&gt;'Probability Needed to have statistical siginificance'&lt;/span&gt;)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="comment"&gt;% For Larger Samples:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;n = 30:1000;&lt;br /&gt;P = p0 + [2 * sqrt(p0 * (1-p0)./n)];&lt;br /&gt;&lt;br /&gt;figure()&lt;br /&gt;&lt;br /&gt;plot(n,P)&lt;br /&gt;&lt;br /&gt;xlabel(&lt;span class="string"&gt;'Sample Size'&lt;/span&gt;)&lt;br /&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_iRk9_jAnzAI/SSHfrTxFT8I/AAAAAAAAACI/_bu0z_-GwEI/s1600-h/SampleSize2_03.png"&gt;&lt;img id="BLOGGER_PHOTO_ID_5269738974133702594" style="FLOAT: left; MARGIN: 0px 10px 10px 0px; WIDTH: 311px; CURSOR: hand; HEIGHT: 218px" alt="" src="http://2.bp.blogspot.com/_iRk9_jAnzAI/SSHfrTxFT8I/AAAAAAAAACI/_bu0z_-GwEI/s320/SampleSize2_03.png" border="0" /&gt;&lt;/a&gt;ylabel(&lt;span class="string"&gt;'Probability Needed to have statistical siginificance'&lt;/span&gt;)&lt;a href="http://4.bp.blogspot.com/_iRk9_jAnzAI/SSHfuJoQU3I/AAAAAAAAACQ/xXt9mAOIfQI/s1600-h/SampleSize2_04.png"&gt;&lt;img id="BLOGGER_PHOTO_ID_5269739022951928690" style="FLOAT: left; MARGIN: 0px 10px 10px 0px; WIDTH: 256px; CURSOR: hand; HEIGHT: 212px" alt="" src="http://4.bp.blogspot.com/_iRk9_jAnzAI/SSHfuJoQU3I/AAAAAAAAACQ/xXt9mAOIfQI/s320/SampleSize2_04.png" border="0" /&gt;&lt;/a&gt;&lt;/pre&gt;&lt;pre class="codeinput"&gt;&lt;a href="http://1.bp.blogspot.com/_iRk9_jAnzAI/SSHlHN5hEdI/AAAAAAAAACY/Fc_4Vq6tA3E/s1600-h/SampleSize2_03.png"&gt;&lt;img id="BLOGGER_PHOTO_ID_5269744951152939474" style="FLOAT: right; MARGIN: 0px 0px 10px 10px; WIDTH: 184px; CURSOR: hand; HEIGHT: 173px" alt="" src="http://1.bp.blogspot.com/_iRk9_jAnzAI/SSHlHN5hEdI/AAAAAAAAACY/Fc_4Vq6tA3E/s320/SampleSize2_03.png" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_iRk9_jAnzAI/SSHfuJoQU3I/AAAAAAAAACQ/xXt9mAOIfQI/s1600-h/SampleSize2_04.png"&gt;&lt;/a&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_iRk9_jAnzAI/SSHfrTxFT8I/AAAAAAAAACI/_bu0z_-GwEI/s1600-h/SampleSize2_03.png"&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;S&lt;/div&gt;&lt;div&gt;ee How the PLOT flattens out after certain number of Samples( &gt;450). &lt;/div&gt;&lt;div&gt;That is why most of the proffessional Pollsters pick a sample size of Approximately 500&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt; &lt;/h2&gt;&lt;h2&gt;History behind "Rules-of-thumb" (Why 22? 25? 30?)&lt;/h2&gt;&lt;h2&gt;(Source: iSixSigma)&lt;/h2&gt;&lt;div&gt;n=22 was proposed by Fisher in Statistical Mehthod, p. 44, when he reviewed the impact of the the exeeding of the standard deviation once in evey three trials. Twice the standard deviation is exceeded in about 22 trials "For p-value = 0.05, or 1 in 20 and 1.96 or nearly 2; it is convenient to take the point as a limit in judging whether a deviation is to be condisered significant or not. Deviations exceeding twice the standard deviation are thus formally regarded as significant. Using this criterion we should be led to follow up a false indication only once in 22 trials even if the statsitics were the only guide. Small effects will still escape notice if the data are insufficiently numerous to bring them out, but lowering of the standard of signficicance meet this difficulty.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;n = 25 has a truly statistical justification. At n = 25 the Law of Large numbers will start to show a pronounced symmetric/normal distribution of the sample means around the population mean. This normal distribution becomes more pronounced as n is increased. &lt;/p&gt;&lt;p&gt;n = 30 comes from a quote from Student (Gosset) in a 1908 paper "On the probable error of a Correlation" in Biometrika. In this paper he reviews the error associated with drawing of two independent samples from infinitely large population and their correlation (not the individual errors of each sample relative to the sample mean and the population mean!). The text reviews&lt;br /&gt;different corrections to the correlation coefficient given various forms of the joint distribution. In a few sentences, Student says that at n = 30 (which is his own experience) the correction factors don't make a big difference. &lt;/p&gt;&lt;p&gt;Later, Fisher showed that the sample for a correlation needs to be determined based on a z-transformation of the correlation. So, Student's argument is only interesting historically. Also, Student wrote his introduction of the t-test in Biometrika during the same year (his&lt;br /&gt;prior article). Historically, the n = 30 discussed in his correlation paper has been confused with the t-test paper, which only introduced the t-statistic up to sample size 10.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;References:&lt;a name="14"&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;1. &lt;a href="http://faculty.chicagogsb.edu/jeffrey.russell/teaching/bstats/handouts/notes.pdf"&gt;http://faculty.chicagogsb.edu/jeffrey.russell/teaching/bstats/handouts/notes.pdf&lt;/a&gt; &lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;2. &lt;a href="http://www.sixsigma.com/"&gt;http://www.isixsigma.com/&lt;/a&gt; &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;3. &lt;a href="http://www.itl.nist.gov/div898/handbook/ppc/section3/ppc333.htm"&gt;http://www.itl.nist.gov/div898/handbook/ppc/section3/ppc333.htm&lt;/a&gt; &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;4. &lt;a href="http://www.itl.nist.gov/div898/handbook/prc/section2/prc222.htm"&gt;http://www.itl.nist.gov/div898/handbook/prc/section2/prc222.htm&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;5. &lt;a href="http://www.itl.nist.gov/div898/handbook/prc/section2/prc243.htm"&gt;http://www.itl.nist.gov/div898/handbook/prc/section2/prc243.htm&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p class="footer"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-5461196661718897719?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/5461196661718897719/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=5461196661718897719' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/5461196661718897719'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/5461196661718897719'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2008/11/sample-size-determination-and-history.html' title='Sample Size Determination and History behind Rules of Thumbs( Why 22? Why 25? Why 30?)'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_iRk9_jAnzAI/SSHfgoVFb9I/AAAAAAAAABw/meaaWaY7oLs/s72-c/SampleSize2_eq69388.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-6922649098631970606</id><published>2008-07-27T01:23:00.000-07:00</published><updated>2008-07-27T10:30:29.535-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Portfolio Construction'/><category scheme='http://www.blogger.com/atom/ns#' term='Black-Litterman'/><category scheme='http://www.blogger.com/atom/ns#' term='He-Litterman'/><category scheme='http://www.blogger.com/atom/ns#' term='Asset Allocation'/><category scheme='http://www.blogger.com/atom/ns#' term='IB matlab2ib'/><title type='text'>Black Litterman Model in MATLAB (He &amp; litterman implementation)</title><content type='html'> &lt;title&gt;HeLitterman&lt;/title&gt;&lt;meta name="generator" content="MATLAB 7.6"&gt;&lt;meta name="date" content="2008-07-27"&gt;&lt;meta name="m-file" content="HeLitterman"&gt;&lt;style&gt;&lt;br /&gt;&lt;br /&gt;body {&lt;br /&gt;  background-color: white;&lt;br /&gt;  margin:10px;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;h1 {&lt;br /&gt;  color: #990000; &lt;br /&gt;  font-size: x-large;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;h2 {&lt;br /&gt;  color: #990000;&lt;br /&gt;  font-size: medium;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* Make the text shrink to fit narrow windows, but not stretch too far in &lt;br /&gt;wide windows. */ &lt;br /&gt;p,h1,h2,div.content div {&lt;br /&gt;  max-width: 600px;&lt;br /&gt;  /* Hack for IE6 */&lt;br /&gt;  width: auto !important; width: 600px;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;pre.codeinput {&lt;br /&gt;  background: #EEEEEE;&lt;br /&gt;  padding: 10px;&lt;br /&gt;}&lt;br /&gt;@media print {&lt;br /&gt;  pre.codeinput {word-wrap:break-word; width:100%;}&lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;span.keyword {color: #0000FF}&lt;br /&gt;span.comment {color: #228B22}&lt;br /&gt;span.string {color: #A020F0}&lt;br /&gt;span.untermstring {color: #B20000}&lt;br /&gt;span.syscmd {color: #B28C00}&lt;br /&gt;&lt;br /&gt;pre.codeoutput {&lt;br /&gt;  color: #666666;&lt;br /&gt;  padding: 10px;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;pre.error {&lt;br /&gt;  color: red;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;p.footer {&lt;br /&gt;  text-align: right;&lt;br /&gt;  font-size: xx-small;&lt;br /&gt;  font-weight: lighter;&lt;br /&gt;  font-style: italic;&lt;br /&gt;  color: gray;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/style&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;MATLAB Implementation of Black Litterman Model&lt;/span&gt;&lt;a name="1"&gt;&lt;/a&gt;&lt;div class="content"&gt;&lt;br /&gt;      &lt;p&gt;This code implements the Black and Litterman Model As given in the paper He &amp;amp; Litterman: The intuition Behind Black- Litterman&lt;br /&gt;         Model Portfolios.&lt;br /&gt;      &lt;/p&gt;&lt;pre class="codeinput"&gt;&lt;span class="comment"&gt;% First The Inputs:&lt;/span&gt;&lt;br /&gt;&lt;span class="comment"&gt;% Correlation Matrix: Page 18&lt;/span&gt;&lt;br /&gt;Corrmat = &lt;span class="keyword"&gt;...&lt;/span&gt;&lt;br /&gt; [1,0.4880,0.4780,0.5150,0.4390,0.5120,0.4910;&lt;br /&gt; 0.4880,1,0.6640,0.6550,0.3100,0.6080,0.7790;&lt;br /&gt; 0.4780,0.6640,1,0.8610,0.3550,0.7830,0.6680;&lt;br /&gt; 0.5150,0.6550,0.8610,1,0.3540,0.7770,0.6530;&lt;br /&gt; 0.4390,0.3100,0.3550,0.3540,1,0.4050,0.3060;&lt;br /&gt; 0.5120,0.6080,0.7830,0.7770,0.4050,1,0.6520;&lt;br /&gt; 0.4910,0.7790,0.6680,0.6530,0.3060,0.6520,1;];&lt;br /&gt;&lt;br /&gt;&lt;span class="comment"&gt;% Risk Aversion Parameter---Page 10&lt;/span&gt;&lt;br /&gt;RiskAversion = 2.5;&lt;br /&gt;&lt;br /&gt;&lt;span class="comment"&gt;% Standard Deviations and Market Capitalization Weights--Table 2 Page 19&lt;/span&gt;&lt;br /&gt;stdevs = &lt;span class="keyword"&gt;...&lt;/span&gt;&lt;br /&gt;     [16.0000&lt;br /&gt;     20.3000&lt;br /&gt;     24.8000&lt;br /&gt;     27.1000&lt;br /&gt;     21.0000&lt;br /&gt;     20.0000&lt;br /&gt;     18.7000]./100;&lt;br /&gt;&lt;br /&gt;MktWeight = &lt;span class="keyword"&gt;...&lt;/span&gt;&lt;br /&gt; [   1.6000&lt;br /&gt;     2.2000&lt;br /&gt;     5.2000&lt;br /&gt;     5.5000&lt;br /&gt;     11.6000&lt;br /&gt;     12.4000&lt;br /&gt;     61.5000]./100;&lt;br /&gt;&lt;br /&gt; tau = 0.05;&lt;br /&gt;&lt;/pre&gt;&lt;h2&gt;Market Equilibrium Risk Premiums : THE PI&lt;a name="2"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;br /&gt;      &lt;p&gt;Equation 2 Page 3 PI = RiskAversion * Covariance * MktWeight&lt;/p&gt;&lt;pre class="codeinput"&gt;&lt;span class="comment"&gt;% Need to Convert Correlation into Covariance&lt;/span&gt;&lt;br /&gt;Covmat = Corrmat .* (stdevs * stdevs');&lt;br /&gt;&lt;br /&gt;&lt;span class="comment"&gt;% Now we have everything to implement the Equation&lt;/span&gt;&lt;br /&gt;&lt;span class="comment"&gt;%EqRiskPrem = RiskAversion * Covmat * MktWeight;&lt;/span&gt;&lt;br /&gt;EqRiskPrem = RiskAversion * Covmat * MktWeight;&lt;br /&gt;&lt;span class="comment"&gt;% Print Out Table 2 in Page 19&lt;/span&gt;&lt;br /&gt;AssetNames = {&lt;span class="string"&gt;'Australia'&lt;/span&gt;,&lt;span class="string"&gt;'Canada'&lt;/span&gt;,&lt;span class="string"&gt;'France'&lt;/span&gt;,&lt;span class="string"&gt;'Germany'&lt;/span&gt;,&lt;span class="string"&gt;'Japan'&lt;/span&gt;,&lt;span class="keyword"&gt;...&lt;/span&gt;&lt;br /&gt; &lt;span class="string"&gt;'UK'&lt;/span&gt;,&lt;span class="string"&gt;'USA'&lt;/span&gt;};&lt;br /&gt;&lt;br /&gt;Table2 = [{&lt;span class="string"&gt;'Assets'&lt;/span&gt; &lt;span class="string"&gt;'Std Dev'&lt;/span&gt; &lt;span class="string"&gt;'Weq'&lt;/span&gt; &lt;span class="string"&gt;'PI'&lt;/span&gt;};&lt;br /&gt;        {&lt;span class="string"&gt;'------'&lt;/span&gt; &lt;span class="string"&gt;'------'&lt;/span&gt; &lt;span class="string"&gt;'---'&lt;/span&gt;  &lt;span class="string"&gt;'--'&lt;/span&gt;};&lt;br /&gt;AssetNames' num2cell([stdevs MktWeight EqRiskPrem]*100)]&lt;br /&gt;&lt;/pre&gt;&lt;pre class="codeoutput"&gt;&lt;br /&gt;Table2 =&lt;br /&gt;&lt;br /&gt; 'Assets'       'Std Dev'    'Weq'        'PI' &lt;br /&gt; '------'       '------'     '---'        '--' &lt;br /&gt; 'Australia'    [     16]    [ 1.6000]    [3.9376]&lt;br /&gt; 'Canada'       [20.3000]    [ 2.2000]    [6.9152]&lt;br /&gt; 'France'       [24.8000]    [ 5.2000]    [8.3581]&lt;br /&gt; 'Germany'      [27.1000]    [ 5.5000]    [9.0272]&lt;br /&gt; 'Japan'        [     21]    [11.6000]    [4.3028]&lt;br /&gt; 'UK'           [     20]    [12.4000]    [6.7677]&lt;br /&gt; 'USA'          [18.7000]    [61.5000]    [7.5600]&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;h2&gt;Views Based Optimal Weights&lt;a name="3"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;pre class="codeinput"&gt;&lt;span class="comment"&gt;%View1 is The German Equity Market Will Outperform the rest of European&lt;/span&gt;&lt;br /&gt;&lt;span class="comment"&gt;% Markets by 5% a year.&lt;/span&gt;&lt;br /&gt;P = [  0   &lt;span class="comment"&gt;%Australia&lt;/span&gt;&lt;br /&gt;     0   &lt;span class="comment"&gt;%Canada&lt;/span&gt;&lt;br /&gt;     -29.5  &lt;span class="comment"&gt;%France&lt;/span&gt;&lt;br /&gt;     100   &lt;span class="comment"&gt;%Germany&lt;/span&gt;&lt;br /&gt;     0   &lt;span class="comment"&gt;%Japan&lt;/span&gt;&lt;br /&gt;     -70.5  &lt;span class="comment"&gt;%UK&lt;/span&gt;&lt;br /&gt;     0]'./100; &lt;span class="comment"&gt;%USA&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Q = 5/100;&lt;br /&gt;&lt;span class="comment"&gt;% The Black Litterman Expected Returns are Calculated&lt;/span&gt;&lt;br /&gt;&lt;span class="comment"&gt;% By Equation 8 and the Optimal Portfolio Weights are&lt;/span&gt;&lt;br /&gt;&lt;span class="comment"&gt;% Calculated By&lt;/span&gt;&lt;br /&gt;&lt;span class="comment"&gt;%RiskAversion * Covariance * ExpectedReturns(MU)&lt;/span&gt;&lt;br /&gt;&lt;span class="comment"&gt;% Lambda = 0.302; Page 11&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Omega = diag(diag(P*tau*Covmat*P'));&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="comment"&gt;% Equation 8--Expected Returns : MU&lt;/span&gt;&lt;br /&gt;PostCov = inv(inv(tau*Covmat) + (P' * inv(Omega) * P));&lt;br /&gt;SigmaP = Covmat + PostCov;&lt;br /&gt;ExpRet=inv(inv(tau*SigmaP)+P'*inv(Omega)*P)* &lt;span class="keyword"&gt;...&lt;/span&gt;&lt;br /&gt;(inv(tau*SigmaP)*EqRiskPrem +P'*inv(Omega)*Q);&lt;br /&gt;&lt;span class="comment"&gt;% ExpRet = inv(inv(tau*Covmat) + P' * inv(Omega) * P) * ...&lt;/span&gt;&lt;br /&gt;&lt;span class="comment"&gt;%(inv(tau*Covmat) * EqRiskPrem + P' * inv(Omega) * Q);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="comment"&gt;% Optimal Weights&lt;/span&gt;&lt;br /&gt;OptimalWeights = (1/RiskAversion)* inv(SigmaP) * ExpRet;&lt;br /&gt;&lt;br /&gt;Tab4Col4 = OptimalWeights - (MktWeight)/(1+tau);&lt;br /&gt;&lt;br /&gt;Table4 = [{&lt;span class="string"&gt;'Assets'&lt;/span&gt;  &lt;span class="string"&gt;'P'&lt;/span&gt;  &lt;span class="string"&gt;'MU'&lt;/span&gt;   &lt;span class="string"&gt;'W'&lt;/span&gt; &lt;span class="string"&gt;'W - Weq/1+tau'&lt;/span&gt;};&lt;br /&gt;        {&lt;span class="string"&gt;'------'&lt;/span&gt; &lt;span class="string"&gt;'--'&lt;/span&gt; &lt;span class="string"&gt;'---'&lt;/span&gt;  &lt;span class="string"&gt;'--'&lt;/span&gt; &lt;span class="string"&gt;'-------------'&lt;/span&gt;};&lt;br /&gt;AssetNames' num2cell([P' ExpRet OptimalWeights &lt;span class="keyword"&gt;...&lt;/span&gt;&lt;br /&gt;round(Tab4Col4 * 1000)./1000]*100)]&lt;br /&gt;&lt;/pre&gt;&lt;pre class="codeoutput"&gt;&lt;br /&gt;Table4 =&lt;br /&gt;&lt;br /&gt; 'Assets'       'P'           'MU'         'W'          'W - Weq/1+tau'&lt;br /&gt; '------'       '--'          '---'        '--'         '-------------'&lt;br /&gt; 'Australia'    [       0]    [ 4.3328]    [ 1.5238]    [            0]&lt;br /&gt; 'Canada'       [       0]    [ 7.5838]    [ 2.0952]    [            0]&lt;br /&gt; 'France'       [-29.5000]    [ 9.2991]    [-4.0555]    [           -9]&lt;br /&gt; 'Germany'      [     100]    [11.0615]    [35.7733]    [      30.5000]&lt;br /&gt; 'Japan'        [       0]    [ 4.5087]    [11.0476]    [            0]&lt;br /&gt; 'UK'           [-70.5000]    [ 6.9550]    [-9.7178]    [     -21.5000]&lt;br /&gt; 'USA'          [       0]    [ 8.0756]    [58.5714]    [            0]&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;p class="footer"&gt;&lt;br /&gt;&lt;br /&gt;         Published with MATLAB® 7.6&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;   &lt;/div&gt;&lt;br /&gt;   &lt;!-- ##### SOURCE BEGIN ##### %% MATLAB Implementation of Black Litterman Model % This code implements the Black and Litterman Model % As given in the paper He &amp; Litterman: The intuition Behind Black- % Litterman Model Portfolios.  % First The Inputs: % Correlation Matrix: Page 18 Corrmat = ...     [1,0.4880,0.4780,0.5150,0.4390,0.5120,0.4910;     0.4880,1,0.6640,0.6550,0.3100,0.6080,0.7790;     0.4780,0.6640,1,0.8610,0.3550,0.7830,0.6680;     0.5150,0.6550,0.8610,1,0.3540,0.7770,0.6530;     0.4390,0.3100,0.3550,0.3540,1,0.4050,0.3060;     0.5120,0.6080,0.7830,0.7770,0.4050,1,0.6520;     0.4910,0.7790,0.6680,0.6530,0.3060,0.6520,1;];  % Risk Aversion ParameterREPLACE_WITH_DASH_DASH-Page 10 RiskAversion = 2.5;  % Standard Deviations and Market Capitalization WeightsREPLACE_WITH_DASH_DASHTable 2 Page 19 stdevs = ...         [16.0000         20.3000         24.8000         27.1000         21.0000         20.0000         18.7000]./100;      MktWeight = ...     [   1.6000         2.2000         5.2000         5.5000         11.6000         12.4000         61.5000]./100;          tau = 0.05; %% Market Equilibrium Risk Premiums : THE PI % Equation 2 Page 3 PI = RiskAversion * Covariance * MktWeight  % Need to Convert Correlation into Covariance Covmat = Corrmat .* (stdevs * stdevs');      % Now we have everything to implement the Equation %EqRiskPrem = RiskAversion * Covmat * MktWeight; EqRiskPrem = RiskAversion * Covmat * MktWeight; % Print Out Table 2 in Page 19 AssetNames = {'Australia','Canada','France','Germany','Japan',...     'UK','USA'};  Table2 = [{'Assets' 'Std Dev' 'Weq' 'PI'};            {'REPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASH' 'REPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASH' 'REPLACE_WITH_DASH_DASH-'  'REPLACE_WITH_DASH_DASH'};  AssetNames' num2cell([stdevs MktWeight EqRiskPrem]*100)]  %% Views Based Optimal Weights %View1 is The German Equity Market Will Outperform the rest of European % Markets by 5% a year. P = [  0   %Australia         0   %Canada         -29.5  %France         100   %Germany         0   %Japan         -70.5  %UK         0]'./100; %USA       Q = 5/100; % The Black Litterman Expected Returns are Calculated % By Equation 8 and the Optimal Portfolio Weights are % Calculated By  %RiskAversion * Covariance * ExpectedReturns(MU) % Lambda = 0.302; Page 11      Omega = diag(diag(P*tau*Covmat*P'));       % Equation 8REPLACE_WITH_DASH_DASHExpected Returns : MU PostCov = inv(inv(tau*Covmat) + (P' * inv(Omega) * P)); SigmaP = Covmat + PostCov; ExpRet=inv(inv(tau*SigmaP)+P'*inv(Omega)*P)* ...  (inv(tau*SigmaP)*EqRiskPrem +P'*inv(Omega)*Q); % ExpRet = inv(inv(tau*Covmat) + P' * inv(Omega) * P) * ... %(inv(tau*Covmat) * EqRiskPrem + P' * inv(Omega) * Q);  % Optimal Weights OptimalWeights = (1/RiskAversion)* inv(SigmaP) * ExpRet;  Tab4Col4 = OptimalWeights - (MktWeight)/(1+tau);  Table4 = [{'Assets'  'P'  'MU'   'W' 'W - Weq/1+tau'};            {'REPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASH' 'REPLACE_WITH_DASH_DASH' 'REPLACE_WITH_DASH_DASH-'  'REPLACE_WITH_DASH_DASH' 'REPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASH-'}; AssetNames' num2cell([P' ExpRet OptimalWeights ... round(Tab4Col4 * 1000)./1000]*100)]   ##### SOURCE END ##### --&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-6922649098631970606?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/6922649098631970606/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=6922649098631970606' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/6922649098631970606'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/6922649098631970606'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2008/07/black-litterman-model-in-matlab-he.html' title='Black Litterman Model in MATLAB (He &amp; litterman implementation)'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-4550677267849209562</id><published>2008-07-19T19:38:00.000-07:00</published><updated>2008-07-19T20:03:44.644-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Volatility Trading'/><category scheme='http://www.blogger.com/atom/ns#' term='Stock option prices'/><category scheme='http://www.blogger.com/atom/ns#' term='Implied Volatility'/><category scheme='http://www.blogger.com/atom/ns#' term='Implied Jump'/><title type='text'>Implied Jump for stocks Before an Important Announcement</title><content type='html'>I always used to wonder how much jump in prices is the market really implying right before an earnings or another important announcement. My research regarding the concept led me to this book " Volatility Trading" by euan sinclair. There he clearly shows how to calculate the implied jump in the stock by options prices. The Strategy is to sell the options so that one can take advantage of the fact that implied volatility usually drops after an announcement. So we will be basically short volatility. one should also expect that the front month implied volatility should be greater than that of further expiration implied volatility. &lt;br /&gt;&lt;br /&gt;First, assuming, sigma_1 as the front month implied volatility and sigma_2 as the second month implied volatility, forward volatility,&lt;br /&gt;&lt;br /&gt;sigma_12 is given by&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_iRk9_jAnzAI/SIKoW5iL77I/AAAAAAAAABA/7TWo45Grn3I/s1600-h/equation.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://3.bp.blogspot.com/_iRk9_jAnzAI/SIKoW5iL77I/AAAAAAAAABA/7TWo45Grn3I/s320/equation.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5224923629057994674" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The volatilty attributed to the event is the difference between front month volatility and forward volatility.&lt;br /&gt;&lt;br /&gt;Sigma_E = &lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_iRk9_jAnzAI/SIKpAn_7w7I/AAAAAAAAABI/hM7I2tQfwdc/s1600-h/equation(2).png"&gt;&lt;img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_iRk9_jAnzAI/SIKpAn_7w7I/AAAAAAAAABI/hM7I2tQfwdc/s320/equation(2).png" border="0" alt=""id="BLOGGER_PHOTO_ID_5224924345905431474" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The Expected Jump is :&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_iRk9_jAnzAI/SIKpMLFdl5I/AAAAAAAAABQ/4lWhDXt70UA/s1600-h/equation(3).png"&gt;&lt;img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_iRk9_jAnzAI/SIKpMLFdl5I/AAAAAAAAABQ/4lWhDXt70UA/s320/equation(3).png" border="0" alt=""id="BLOGGER_PHOTO_ID_5224924544302421906" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;A Trader needs to compare this estimate to that of his own estimate of how much the stock is going to jump after the announcement and should trade accordingly. Not definitely for the faint hearted.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-4550677267849209562?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/4550677267849209562/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=4550677267849209562' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/4550677267849209562'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/4550677267849209562'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2008/07/implied-jump-for-stocks-before.html' title='Implied Jump for stocks Before an Important Announcement'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_iRk9_jAnzAI/SIKoW5iL77I/AAAAAAAAABA/7TWo45Grn3I/s72-c/equation.png' height='72' width='72'/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-7161605276977151823</id><published>2008-06-23T11:45:00.000-07:00</published><updated>2008-06-27T22:02:34.232-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='INDIA'/><category scheme='http://www.blogger.com/atom/ns#' term='Interest Rate Spread'/><category scheme='http://www.blogger.com/atom/ns#' term='0% credit card'/><title type='text'>Trading the India-USA Interest Rate Spread (By using 0% credit cards) :</title><content type='html'>&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;div align="justify"&gt;Several articles are available on the web that suggest making a “Risk-free” profit by transferring a low interest paying credit card into a high interest paying savings account.&lt;br /&gt;The interest rates in usa are low for savings or fixed deposit account. At the time of writing this post, The best I got was a 3% for an year. So when all is said and done, the max you could make is 3% gain.&lt;br /&gt;&lt;br /&gt;Here in this post, I take a different route. This post is only for Indian citizens. Here is a step by step procedure on how to setup the trade and the risk analysis.&lt;br /&gt;&lt;br /&gt;I got several 0% interest rate credit cards that are valid upto 1 year and some with 4.99% for the life until paid in full.&lt;br /&gt;&lt;br /&gt;Step I :&lt;br /&gt;1. Open a Citibank Checking account in USA. Also, open a Citibank rupee checking account as well as a Citibank account in INDIA branch. The second India account is for the ease of transfer of money from India to usa. But if you have some other account with other banks, then also it should be OK.&lt;br /&gt;2. Having opened the accounts, transfer a 0% interest rate credit card into a local checking account in usa.&lt;br /&gt;3. Send the money from US branch into your Citibank NRE rupee checking account.&lt;br /&gt;4. Take the money out of your rupee checking account (can be done online or through a beneficiary in India) and put it into a fixed deposit.&lt;br /&gt;5. Fixed deposit interest rates in INDIA vary from 6.5% to 9% and 6 months to more than a year.&lt;br /&gt;6. When the Fixed Deposit matures, Transfer the money through your local bank in INDIA to the one In usa. If you have a Citibank rupee account and a local Citibank in INDIA, the transfer of money to USA is very easy.&lt;br /&gt;&lt;br /&gt;Now, Let us analyse the risk associated with such a trade: The major risk is the exchange rate risk. We ideally Rupee to appreciate against the dollar so that we could get more dollars when we convert rupees into dollars at the end of maturity of the Fixed Deposit. We lose if Rupee depreciates against the Dollar, as we would get lesser number of dollars for Rupees.&lt;br /&gt;&lt;br /&gt;BegDollars $10,000.00&lt;br /&gt;BegExRate( 1$ = ) INR 43.00&lt;br /&gt;Total Investment(Rs) INR 430,000.00&lt;br /&gt;End Balance INR 544,705.41&lt;br /&gt;Int Rate 9.00%&lt;br /&gt;US int Rate 0%&lt;br /&gt;&lt;br /&gt;&lt;iframe marginwidth="0" marginheight="0" src="http://spreadsheets.google.com/pub?key=pJLbFEhdVQdZSY7kwXDyrcQ&amp;output=html&amp;widget=true" frameborder="0" height="280" scrolling="no" width="360"&gt;&lt;/iframe&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-7161605276977151823?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/7161605276977151823/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=7161605276977151823' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/7161605276977151823'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/7161605276977151823'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2008/06/trading-india-usa-interest-rate-spread.html' title='Trading the India-USA Interest Rate Spread (By using 0% credit cards) :'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>10</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-6249358527771303806</id><published>2008-06-19T19:41:00.000-07:00</published><updated>2008-06-22T11:09:17.498-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Market Depth'/><category scheme='http://www.blogger.com/atom/ns#' term='Tick Data'/><title type='text'>Combining Market Depth and Tick Files</title><content type='html'>What is MARKET Depth ?? Market Depth is another name for Order book information. Order Book displays 5-10 levels of Bid and Offer Prices along with the volume sizes. this is very useful information for high frequency trading. Order Book tells us about what state the market is in. The amount of data is huge. Here I give some tips and useful information in how to combine the Order book Data with the Time &amp;amp; sales Tick Data.&lt;br /&gt;&lt;br /&gt;CME usually provides 5 levels of information for futures contracts.&lt;br /&gt;So, To construct a sequential price flow, we need to combine the Depth and Tick Data. By doing this, we can reconstruct the price action that has taken place in the market.&lt;br /&gt;&lt;br /&gt;It is best to work with the Best Bid Best Offer and Time and sales data to reduce the memory requirement.&lt;br /&gt;&lt;br /&gt;Always, Market Depth Updates first and then trade gets updated. If one is collecting the data, one needs to note down the Time stamps (upto msec level) and the corresponding Data.&lt;br /&gt;&lt;br /&gt;For Example:&lt;br /&gt;&lt;br /&gt;Depth Update 08:30:01.324 1925.50 1925.75&lt;br /&gt;Trade Update 08:30:01.325 1925.75 500 (volume)&lt;br /&gt;&lt;br /&gt;As you can see, one needs to see the time stamp for the last trade and see what was the best bid and best offer right before it and then combine the both into one row as follows:&lt;br /&gt;&lt;br /&gt;08:30:01.325 1925.50 1925.75 1925.75 500&lt;br /&gt;&lt;br /&gt;This gives a better picture of what really happened.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-6249358527771303806?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/6249358527771303806/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=6249358527771303806' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/6249358527771303806'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/6249358527771303806'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2008/06/combining-market-depth-and-tick-files.html' title='Combining Market Depth and Tick Files'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-3971451311049153488</id><published>2008-06-15T15:56:00.000-07:00</published><updated>2008-06-22T11:12:34.294-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='tick aggregation'/><category scheme='http://www.blogger.com/atom/ns#' term='tick2bar'/><title type='text'>Intraday Trading---Converting Ticks into Bars</title><content type='html'>Sometimes it is easier to work with bars instead of ticks. In such cases, If we have Raw Tick Data, The following matlab programs would do the work for you. It is a collection of various methods that could be used to construct bars.&lt;br /&gt;&lt;br /&gt;Tick2bar program at the matlab file exchange:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=3398"&gt;http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=3398&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Another method that was posted at a forum &lt;a href="http://www.nuclearphynance.com/"&gt;http://www.nuclearphynance.com/&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;% numberOfBarsPerDay: 24 = 1 hour bars (60 minutes)&lt;br /&gt;% 48 = 30 minute bars (24*60)/30&lt;br /&gt;% 72 = 20 minute bars (24*60)/20&lt;br /&gt;% 96 = 15 minute bars (24*60)/15&lt;br /&gt;% 144 = 10 minute bars (24*60)/10&lt;br /&gt;% 288 = 5 minute bars (24*60)/5&lt;br /&gt;% 1440 = 1 minute bars (24*60)/1&lt;br /&gt;%%--------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;numberOfBarsPerDay=24;&lt;br /&gt;x=Bid;&lt;br /&gt;datetimeGrid=(floor(datetime.*numberOfBarsPerDay))./numberOfBarsPerDay;&lt;br /&gt;timeChgPointIndex=find(diff(datetimeGrid)~=0);&lt;br /&gt;intervalDatetimeStart=datetimeGrid(timeChgPointIndex);&lt;br /&gt;intervalDatetimeEnd=datetimeGrid(timeChgPointIndex+1);&lt;br /&gt;intervalDatetimeActual=datetime(timeChgPointIndex);&lt;br /&gt;intervalData=x(timeChgPointIndex);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;3. Another method using histc&lt;br /&gt;[numberInInterval,intervalIndex]=histc(datetime,intervals);&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-3971451311049153488?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/3971451311049153488/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=3971451311049153488' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/3971451311049153488'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/3971451311049153488'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2008/06/intraday-trading-converting-ticks-into.html' title='Intraday Trading---Converting Ticks into Bars'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-2033276321099823159</id><published>2008-06-15T15:30:00.000-07:00</published><updated>2008-06-22T11:09:59.112-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='MATLAB'/><category scheme='http://www.blogger.com/atom/ns#' term='CBOT'/><category scheme='http://www.blogger.com/atom/ns#' term='Market Profile'/><title type='text'>Creating Market profile with MATLAB</title><content type='html'>&lt;a href="http://www.cbot.com/cbot/docs/CMADayProfil.gif"&gt;&lt;img style="margin: 0px auto 10px; display: block; width: 320px; text-align: center;" alt="" src="http://www.cbot.com/cbot/docs/CMADayProfil.gif" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;div&gt;Some Day Traders use CBOT Market Profile for Day Trading purposes. I am NOT a fan of Technical Analysis, but nevertheless, I thought why not write a matlab program to create one.&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Here is a quote from CBOT----&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Market Profile is a graphical organization of price and time information. Market Profile displays price on the vertical axis and time on the horizontal axis. Letters are used to symbolize time brackets. marketprofile is an analytical decision support tool for traders—not a trading system.&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;CBOT Overview:&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;a href="http://www.cbot.com/cbot/pub/page/0,3181,1168,00.html"&gt;http://www.cbot.com/cbot/pub/page/0,3181,1168,00.html&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;CBOT Examples:&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;MATLAB CODE:&lt;/div&gt;&lt;br /&gt;&lt;div&gt;% clear all&lt;br /&gt;% clc;&lt;br /&gt;% %assuming tick by tick data&lt;br /&gt;% data = load('may01d.mat');&lt;br /&gt;% ESdata = data.ESlast;&lt;br /&gt;ESdata=close;&lt;br /&gt;&lt;br /&gt;ESprices=unique(ESdata);&lt;br /&gt;bars=20;&lt;br /&gt;timertick=1;&lt;br /&gt;periods=floor(size(ESdata,1)/(timertick*bars));&lt;br /&gt;MP=zeros(size(ESprices,1),periods);&lt;br /&gt;for i=1:periods&lt;br /&gt;g=ESdata(timertick*bars*(i-1)+1:timertick*bars*i,1);&lt;br /&gt;for m=1:size(g,1)&lt;br /&gt;s=g(m);&lt;br /&gt;s1=find(ESprices==s);&lt;br /&gt;MP(s1,i)=i;&lt;br /&gt;end&lt;br /&gt;end&lt;/div&gt;&lt;div&gt;&lt;br /&gt;B = MP;%A(:,2:end) ;&lt;br /&gt;B(B==0) = inf ;&lt;br /&gt;B = sort(B,2) ; % sort each row&lt;br /&gt;B(isinf(B)) = 0 ;&lt;br /&gt;&lt;br /&gt;TPO=zeros(size(B));&lt;br /&gt;for l=1:numel(B)&lt;br /&gt;if B(l)~=0&lt;br /&gt;TPO(l)=B(l)+64;&lt;br /&gt;end&lt;br /&gt;end &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;br /&gt;Final_MP={ESprices char(TPO)}; &lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-2033276321099823159?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/2033276321099823159/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=2033276321099823159' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/2033276321099823159'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/2033276321099823159'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2008/06/creating-market-profile-with-matlab.html' title='Creating Market profile with MATLAB'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-4988831002913348341</id><published>2008-06-15T08:01:00.000-07:00</published><updated>2008-06-22T11:10:22.907-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='MATLAB'/><category scheme='http://www.blogger.com/atom/ns#' term='Yang and Zhang'/><category scheme='http://www.blogger.com/atom/ns#' term='Historical Volatility'/><title type='text'>Estimate Historical Volatility</title><content type='html'>Here is the MATLAB code that one could use to estimate historical volatility using different methods&lt;br /&gt;&lt;br /&gt;Historical Close-to-Close volatility&lt;br /&gt;Historical High Low Parkinson Volatility&lt;br /&gt;Historical Garman Klass Volatility&lt;br /&gt;Historical Garman Klass Volatility modified by Yang and Zhang&lt;br /&gt;Historical Roger and Satchell Volatility&lt;br /&gt;Historical Yang and Zhang Volatility&lt;br /&gt;Average of all the historical volatilities calculated above&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: center;"&gt;&lt;div style="text-align: left;"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;function vol = EstimateVol(O,H,L,C,n)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% Estimate Volatility using different methods&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% EstimateVol(O,H,L,C)gives an estimate of volatility based on Open, High,&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% Low, Close prices.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% INPUTS:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% O--Open Price&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% H--High Price&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% L--Low Price&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% C--Close Price&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% n--Number of historical days used in the volatility estimate&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% OUTPUT:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% Vol is a structure with volatilities using different methods.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% hccv -- Historical Close-to-Close volatility&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% hhlv -- Historical High Low Parkinson Volatility&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% hgkv -- Historical Garman Klass Volatility&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% hgkvM -- Historical Garman Klass Volatility modified by Yang and Zhang&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% hrsv -- Historical Roger and Satchell Volatility&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% hyzv -- Historical Yang and Zhang Volatility&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% AVGV -- Average of all the historical volatilities calculated above&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% web: http://www.sitmo.com&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;try&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;OHLC = [O H L C];&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;catch %#ok&lt;/span&gt;&lt;/span&gt;&lt;ctch&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;error('O H L C must be of the same size');&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;rethrow(lasterror);&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;if(n&lt;=length(O))&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;fh = @(x) x(length(x)-n+1:end);&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;else&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;error('n should be less than or equal to the length of the prices')&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;open = fh(O); %O(length(O)-n+1:end);&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;high = fh(H); %H(length(H)-n+1:end);&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;low = fh(L); %L(length(L)-n+1:end);&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;close = fh(C); %C(length(C)-n+1:end);&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;Z = 252; %Number of trading Days in a year&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;vol.hccv = hccv();&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;vol.hhlv = hhlv();&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;vol.hgkv = hgkv();&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;vol.hgkvm = hgkvM();&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;vol.hrsv = hrsv();&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;vol.hyzv = hyzv();&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;vol.AVGV = mean(cell2mat(struct2cell(vol)));&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;function vol1 = hccv()&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% historical close to close volatility&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%Historical volatility calculation using close-to-close prices.&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;r = log(close(2:end)./close(1:end-1));&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;rbar = mean(r);&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;vol1 = sqrt((Z/(n-2)) * sum((r - rbar).^2));&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;function vol2 = hhlv()&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%The Parkinson formula for estimating the historical volatility of an&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%underlying based on high and low prices.&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;vol2 = sqrt((Z/(4*n*log(2))) * sum((log(high./low)).^2));&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;function vol3 = hgkv()&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% The Garman and Klass estimator for estimating historical volatility &lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% assumes Brownian motion with zero drift and no opening jumps &lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%(i.e. the opening = close of the previous period). This estimator is &lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;% 7.4 times more efficient than the close-to-close estimator. &lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;vol3 = sqrt((Z/n)* sum((0.5*(log(high./low)).^2) - (2*log(2) - 1).*(log(close./open)).^2));&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;function vol4 = hgkvM()&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%Yang and Zhang derived an extension to the Garman Glass historical &lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%volatility estimator that allows for opening jumps. It assumes &lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%Brownian motion with zero drift. This is currently the preferred &lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%version of open-high-low-close volatility estimator for zero drift &lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%and has an efficiency of 8 times the classic close-to-close estimator.&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%Note that when the drift is nonzero, but instead relative large to the&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%volatility, this estimator will tend to overestimate the volatility.&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;vol4 = sqrt((Z/n)* sum((log(open(2:end)./close(1:end-1))).^2 + (0.5*(log(high(2:end)./low(2:end))).^2) - (2*log(2) - 1)*(log(close(2:end)./open(2:end))).^2));&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;function vol5 = hrsv()&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%The Roger and Satchell historical volatility estimator allows for&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%non-zero drift, but assumed no opening jump. &lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;vol5 = sqrt((Z/n)*sum((log(high./close).*log(high./open)) + (log(low./close).*log(low./open))));&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;function vol6 = hyzv()&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%Yang and Zhang were the first to derive an historical volatility &lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%estimator that has a minimum estimation error, is independent of &lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%the drift, and independent of opening gaps. This estimator is &lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%maximally 14 times more efficient than the close-to-close estimator. &lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%It can be interpreted as a weighted average of the Rogers and Satchell&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%estimator, the close-open volatility and the open-close volatility. &lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%The performance degrades to the classic close-to-close estimator when&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;%the price process is heavily dominated by opening jumps. &lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;muO = (1/n)*sum(log(open(2:end)./close(1:end-1)));&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;sigmaO = (Z/(n-1)) * sum((log(open(2:end)./close(1:end-1)) - muO).^2);&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;muC = (1/n)*sum(log(close./open));&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;sigmaC = (Z/(n-1)) * sum((log(close./open) - muC).^2);&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;sigmaRS = hrsv();&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;sigmaRS = sigmaRS^2;&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;k = 0.34/(1+((n+1)/(n-1)));&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;vol6 = sqrt(sigmaO^2+(k*sigmaC^2)+((1-k)*(sigmaRS)));&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;ctch&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/ctch&gt;&lt;br /&gt;&lt;/div&gt;&lt;ctch&gt;&lt;/ctch&gt;&lt;/div&gt;&lt;ctch&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/ctch&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-4988831002913348341?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/4988831002913348341/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=4988831002913348341' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/4988831002913348341'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/4988831002913348341'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2008/06/estimate-historical-volatility.html' title='Estimate Historical Volatility'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-8365060141991881459</id><published>2008-06-15T06:47:00.000-07:00</published><updated>2008-07-20T07:33:36.584-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='IB'/><category scheme='http://www.blogger.com/atom/ns#' term='MATLAB'/><category scheme='http://www.blogger.com/atom/ns#' term='automated futures'/><category scheme='http://www.blogger.com/atom/ns#' term='matlab2ib'/><title type='text'>MATLAB2IB</title><content type='html'>MATLAB2IB is the product I created along with Exchange Systems Inc, which Connects to the JAVA TWS API from Interactive Brokers. it is socket based. It is much better than ActiveX based programming that IB also Provides. It is more efficient.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.exchangeapi.com"&gt;MATLAB2IB&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;provides all the information that you would need.&lt;br /&gt;&lt;br /&gt;It includes all the commands that are neccessary for building an automated trading system in MATLAB. (ATS). This gives you a powerful combination of MATHEMATICAL software such as MATLAB and a cheap commissions based broker such as Interactive Brokers.&lt;br /&gt;&lt;br /&gt;MATLAB has wide variety of toolboxes available that one could use in building a trading system.&lt;br /&gt;&lt;br /&gt;There are a lot of freely available toolboxes .&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.mathworks.com/matlabcentral/fileexchange/loadCategory.do"&gt;File Exchange&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Interactive Brokers:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://individuals.interactivebrokers.com/en/main.php"&gt;InteractiveBrokers&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Interactive Brokers API (Application Programming Interface) :&lt;br /&gt;&lt;br /&gt;&lt;a href="http://individuals.interactivebrokers.com/en/main.php"&gt;IB API&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-8365060141991881459?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/8365060141991881459/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=8365060141991881459' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/8365060141991881459'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/8365060141991881459'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2008/06/matlab2ib.html' title='MATLAB2IB'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1440440391871056920.post-6827263335382513593</id><published>2008-06-13T21:41:00.000-07:00</published><updated>2008-06-19T19:36:25.214-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='IB matlab2ib'/><title type='text'>Trading Interactive Brokers with MATLAB</title><content type='html'>In This Blog, I will be writing about how to build automated trading systems using matlab and Interactive Brokers. I have previously worked as a Futures Trader and currently work as an Analyst in a Quantitative Strategies Team at a Hedge Fund of Fund.&lt;br /&gt;&lt;br /&gt;My Main Interests are in building trading systems that take advantage of several Techniques such as Machine Learning, Artificial intelligence and Market Microstructure.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1440440391871056920-6827263335382513593?l=tradingwithmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tradingwithmatlab.blogspot.com/feeds/6827263335382513593/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1440440391871056920&amp;postID=6827263335382513593' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/6827263335382513593'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1440440391871056920/posts/default/6827263335382513593'/><link rel='alternate' type='text/html' href='http://tradingwithmatlab.blogspot.com/2008/06/trading-interactive-brokers-with-matlab.html' title='Trading Interactive Brokers with MATLAB'/><author><name>TradingwithMatlab</name><uri>http://www.blogger.com/profile/10033724108963582814</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry></feed>
