Monthly Archives: April 2016

Mean reversion

Category : Uncategorized

Mean reversion

Please make sure you have the most updated mfd_dm, mfd_ta and mfd_tex packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
net from "http://www.researchforprofit.com/stata/mfd_ta"
net from "http://www.researchforprofit.com/stata/mfd_tex"
* click on the blue mfd_dm package and then "click here to install" 
*

Mean reversion is one of the more theoretical technical analysis tools. It simply argues that the prices will revert to their means.

Mean reversion test procedure

  • Difference between price and sma
  • While price is above/below the sma, the difference should decrease

Mean reversion test steps

  • For a symbol
  • Download daily prices
  • Calculate Diff=Price-SMA
  • Calculate daily change in Diff (for mean reversion, this change should be negative)

Single stocks mean reversion: 

*
clear all
cd "/Users/mfd/Desktop"

mfd_dm_prices AAPL, freq(d) chg(ln) start(01jan2000) end(31dec2013) field(c)
gen day=_n
tsset day

mfd_ta_MA close_AAPL, period(20) ma_type(sma)

gen diff_AAPL=close_AAPL-sma20_AAPL if (close_AAPL>sma20_AAPL)
replace diff_AAPL=sma20_AAPL-close_AAPL if (close_AAPL0) & (ln_diff_AAPL!=.)

summ ln_diff_AAPL revert_AAPL
*

Screen Shot 2013-09-07 at 7.17.28 PM

AAPL had return that is inline with mean reversion 51.89% of the time. In other words, 51.89% of the time the price moved towards the mean and 48.11% of the time price moved away from it. On average, daily change of difference (price-MA) should be negative to mean reversion. However, 0.0004472 is positive. Thus, it means no reversion.

Multicompany (NYA) and multi SMA window to LaTeX (using all historic prices already downloaded):

*
clear all
cd "/Users/mfd/Desktop"

mfd_dm_components, symbol(^NYA)
levelsof Symbol, local(tickers)

mfd_dm_prices `tickers', freq(d) chg(ln) start(01jan2000) end(31dec2013) field(o c h l v)
gen day=_n
tsset day
save NYA.dta, replace

local latex_file="mean_reversion"
mfd_tex, saving("`latex_file'") custom("window,symbol,return,p-value,revert")

forval bb=20(10)100 {
    use NYA.dta, clear
	
	* Moving average
	* Distance of price from moving average
	foreach aa in `tickers' {
		di "`bb': `aa'"
		qui {
			mfd_ta_MA close_`aa', period(`bb') ma_type(sma)

			gen diff_`aa'=close_`aa'-sma`bb'_`aa' if (close_`aa'>sma`bb'_`aa')
			replace diff_`aa'=sma`bb'_`aa'-close_`aa' if (close_`aa'0) & (ln_diff_`aa'!=.)

			summ revert_`aa'
			local revert: di %6.4fc r(mean)
	
			summ ln_diff_`aa'		
			local ln_diff: di %6.4fc r(mean)
			local standard_error = `r(sd)'/sqrt(`r(N)')
			local degrees_of_freedom = `r(N)'-1
			local mean_ = `r(mean)'
			sigstars T, df(`degrees_of_freedom') coef(`mean_') se(`standard_error')	
			if ("`r(stars)'"=="NS") local ln_diff_stars="   "
			if ("`r(stars)'"!="NS") local ln_diff_stars="`r(stars)'"

			mfd_tex, saving("`latex_file'") custom("`bb',`aa',`ln_diff',`ln_diff_stars',`revert'")
		}
	}
}

insheet using "`latex_file'.tex", clear comma
tabstat revert, stat(mean) by(window)
*

Multicompany (NYA) and multi SMA window to LaTeX (downloading prices one by one): 

*
clear all
cd "/Users/mfd/Desktop"

mfd_dm_components, symbol(^NYA)
levelsof Symbol, local(tickers)

local latex_file="mean_reversion"
mfd_tex, saving("`latex_file'") custom("window,symbol,return,p-value,revert")

foreach aa in `tickers' {
	mfd_dm_prices `aa', freq(d) chg(ln) start(01jan2000) end(31dec2013) field(o c h l v)
	gen day=_n
	tsset day
	forval bb=20(10)100 {
		di "`bb': `aa'"
		qui {
			mfd_ta_MA close_`aa', period(`bb') ma_type(sma)

			gen diff_`aa'=close_`aa'-sma`bb'_`aa' if (close_`aa'>sma`bb'_`aa')
			replace diff_`aa'=sma`bb'_`aa'-close_`aa' if (close_`aa'0) & (ln_diff_`aa'!=.)

			summ revert_`aa'
			local revert: di %6.4fc r(mean)
	
			summ ln_diff_`aa'		
			local ln_diff: di %6.4fc r(mean)
			local standard_error = `r(sd)'/sqrt(`r(N)')
			local degrees_of_freedom = `r(N)'-1
			local mean_ = `r(mean)'
			sigstars T, df(`degrees_of_freedom') coef(`mean_') se(`standard_error')	
			if ("`r(stars)'"=="NS") local ln_diff_stars="   "
			if ("`r(stars)'"!="NS") local ln_diff_stars="`r(stars)'"

			mfd_tex, saving("`latex_file'") custom("`bb',`aa',`ln_diff',`ln_diff_stars',`revert'")
		}
	}
}

insheet using "`latex_file'.tex", clear comma
tabstat revert, stat(mean) by(window)
*

Multicompany (NYA) and multi SMA window as an update to components.dta: 

*
clear all
cd "/Users/mfd/Desktop"

* Variables
use components.dta, clear
keep Symbol
gen name=""
gen exchange=""
gen mcap=""
gen obs=.
gen first=.
gen last=.
format first last %td
gen mean_ln=.
gen max_ln=.
gen min_ln=.
gen sd_ln=.
forval aa=20(10)100 {
	gen ln_diff_`aa'=.
	gen ln_diff_stars_`aa'=""
	gen revert_proportion_`aa'=.
}
save components.dta, replace

use components.dta, clear
levelsof Symbol, local(tickers)

* Descriptives and Mean reversion
qui {
	local counter=0
	foreach bb in `tickers' {
		use components.dta, clear
		summ obs if Symbol=="`bb'"
		if (r(N)==0) {
			mfd_dm_prices `bb', freq(d) chg(ln) field(c) start(01jan2000)
			if (_N>200) {
				noi: di "`bb'"
				gen day=_n
				tsset day
				save temp.dta, replace
		
				* Descriptives
				use temp.dta, clear
				ds close*
				local aa=substr(r(varlist),(strpos(r(varlist),"_")+1),.)
				summ date if ln_`aa'!=.
				local first=r(min)
				local last=r(max)
				summ ln_`aa'
				local obs=r(N)
				local mean_ln=r(mean)
				local max_ln=r(max)
				local min_ln=r(min)
				local sd_ln=r(sd)
				mfd_dm_keystats `bb', field(n x j1)
				local co_name=Name[1]
				local co_exc=Exchange[1]
				local co_mcap=Market_Capitalization[1]
				use components.dta, clear
				replace name="`co_name'" if Symbol=="`bb'"
				replace exchange="`co_exc'" if Symbol=="`bb'"
				replace mcap="`co_mcap'" if Symbol=="`bb'"
				replace first=`first' if Symbol=="`bb'"
				replace last=`last' if Symbol=="`bb'"
				replace obs=`obs' if Symbol=="`bb'"
				replace mean=`mean_ln' if Symbol=="`bb'"
				replace max=`max_ln' if Symbol=="`bb'"
				replace min=`min_ln' if Symbol=="`bb'"
				replace sd=`sd_ln' if Symbol=="`bb'"
				save components.dta, replace
						
		
				* Mean reversion
				forval cc=20(10)100 {
					no: di "`cc': `bb'"
					use temp.dta, clear
					ds close*
					local aa=substr(r(varlist),(strpos(r(varlist),"_")+1),.)
					mfd_ta_MA close_`aa', period(`cc') ma_type(sma)

					gen diff_`aa'=close_`aa'-sma`cc'_`aa' if (close_`aa'>sma`cc'_`aa')
					replace diff_`aa'=sma`cc'_`aa'-close_`aa' if (close_`aa'0) & (ln_diff_`aa'!=.)

					summ revert_`aa'
					local revert: di %6.4fc r(mean)

					summ ln_diff_`aa'		
					local ln_diff: di %6.4fc r(mean)
					local standard_error = `r(sd)'/sqrt(`r(N)')
					local degrees_of_freedom = `r(N)'-1
					local mean_ = `r(mean)'
					sigstars T, df(`degrees_of_freedom') coef(`mean_') se(`standard_error')	
					if ("`r(stars)'"=="NS") local ln_diff_stars="   "
					if ("`r(stars)'"!="NS") local ln_diff_stars="`r(stars)'"

					use components.dta, clear
					replace ln_diff_`cc'=`ln_diff' if Symbol=="`bb'"
					replace ln_diff_stars_`cc'="`ln_diff_stars'" if Symbol=="`bb'"
					replace revert_proportion_`cc'=`revert' if Symbol=="`bb'"
					save components.dta, replace
				}
				local counter=`counter'+1
				if (`counter'==10) continue, break	
			}
		}		
	}
}
*

Financial ratios

Category : Uncategorized

Financial ratios

Cash ratio

*
clear all
cd "/users/mfd/desktop"

use "http://researchforprofit.com/data_private/140117_statement_annual_yahoo.dta", clear
keep symbol name sector industry date cash_and_cash_equivalents total_assets
bysort symbol: egen max_date=max(date)
keep if date==max_date
drop max_date
gen cash_ratio=cash_and_cash_equivalents / total_assets * 100
drop if cash_ratio==.

replace sector = subinstr(sector,"&","&",.)
replace industry = subinstr(industry,"&","&",.)
save temp.dta, replace

use temp.dta, clear
tabstat cash_ratio, stat(min max mean count) by(sector) format(%5.2fc)
collapse (min) Min=cash_ratio (max) Max=cash_ratio (mean) Mean=cash_ratio (count) N=cash_ratio, by(sector)
format %5.2fc Min Max Mean
mfd_tex_list Min Max Mean N, saving("140117_cash_ratio_by_sector") todo(table_begin table_rows table_end) label("cash_ratio_by_sector") caption({\bf Cash ratio by sector}. Most recent financial statements are used as of January 17, 2014. Financial statements are obtained from Yahoo! Finance. Figures are percents: 22.38 is 22.38%.)

use temp.dta, clear
tabstat cash_ratio, stat(min max mean count) by(industry) format(%5.2fc) labelwidth(70) save
collapse (min) Min=cash_ratio (max) Max=cash_ratio (mean) Mean=cash_ratio (count) N=cash_ratio, by(industry)
format %5.2fc Min Max Mean
mfd_tex_list Min Max Mean N, saving("140117_cash_ratio_by_industry") todo(table_begin table_rows table_end) label("cash_ratio_by_industry") caption({\bf Cash ratio by industry}. Most recent financial statements are used as of January 17, 2014. Financial statements are obtained from Yahoo! Finance. Figures are percents: 22.38 is 22.38%.)

erase temp.dta
*

Screen Shot 2014-01-17 at 8.38.57 PM

Current ratio

*
clear all
cd "/users/mfd/desktop"

use "http://researchforprofit.com/data_private/140117_statement_annual_yahoo.dta", clear
keep symbol name sector industry date total_current_assets total_current_liabilities
bysort symbol: egen max_date=max(date)
keep if date==max_date
drop max_date
gen current_ratio=total_current_assets/total_current_liabilities
drop if current_ratio==.

replace sector = subinstr(sector,"&","&",.)
replace industry = subinstr(industry,"&","&",.)
save temp.dta, replace

use temp.dta, clear
tabstat current_ratio, stat(min max mean count) by(sector) format(%5.2fc)
collapse (min) Min=current_ratio (max) Max=current_ratio (mean) Mean=current_ratio (count) N=current_ratio, by(sector)
format %5.2fc Min Max Mean
mfd_tex_list Min Max Mean N, saving("140117_current_ratio_by_sector") todo(table_begin table_rows table_end) label("current_ratio_by_sector") caption({\bf Current ratio by sector}. Most recent financial statements are used as of January 17, 2014. Financial statements are obtained from Yahoo! Finance.)

use temp.dta, clear
tabstat current_ratio, stat(min max mean count) by(industry) format(%5.2fc)
collapse (min) Min=current_ratio (max) Max=current_ratio (mean) Mean=current_ratio (count) N=current_ratio, by(industry)
format %5.2fc Min Max Mean
mfd_tex_list Min Max Mean N, saving("140117_current_ratio_by_industry") todo(table_begin table_rows table_end) label("current_ratio_by_industry") caption({\bf Current ratio by industry}. Most recent financial statements are used as of January 17, 2014. Financial statements are obtained from Yahoo! Finance.)

erase temp.dta
*

Screen Shot 2014-01-17 at 10.50.46 PM

Quick ratio

 

Debt to assets ratio

*
clear all
cd "/users/mfd/desktop"

use "http://researchforprofit.com/data_private/140117_statement_annual_yahoo.dta", clear
keep symbol name sector industry date total_liabilities total_assets
bysort symbol: egen max_date=max(date)
keep if date==max_date
drop max_date
gen debt_ratio=total_liabilities/total_assets*100
drop if debt_ratio==.

replace sector = subinstr(sector,"&","&",.)
replace industry = subinstr(industry,"&","&",.)
save temp.dta, replace

use temp.dta, clear
tabstat debt_ratio, stat(min max mean count) by(sector) format(%5.2fc)
collapse (min) Min=debt_ratio (max) Max=debt_ratio (mean) Mean=debt_ratio (count) N=debt_ratio, by(sector)
format %5.2fc Min Max Mean
mfd_tex_list Min Max Mean N, saving("140117_debt_ratio_by_sector") todo(table_begin table_rows table_end) label("debt_ratio_by_sector") caption({\bf Debt ratio by sector}. Most recent financial statements are used as of January 17, 2014. Financial statements are obtained from Yahoo! Finance. Figures are percents: 22.38 is 22.38%.)

use temp.dta, clear
tabstat debt_ratio, stat(min max mean count) by(industry) format(%5.2fc)
collapse (min) Min=debt_ratio (max) Max=debt_ratio (mean) Mean=debt_ratio (count) N=debt_ratio, by(industry)
format %5.2fc Min Max Mean
mfd_tex_list Min Max Mean N, saving("140117_debt_ratio_by_industry") todo(table_begin table_rows table_end) label("debt_ratio_by_industry") caption({\bf Debt ratio by industry}. Most recent financial statements are used as of January 17, 2014. Financial statements are obtained from Yahoo! Finance. Figures are percents: 22.38 is 22.38%.)

erase temp.dta
*

Screen Shot 2014-01-17 at 10.56.03 PM

Gross margin
ROE
ROA
Sales to assets
EPS
PE


Dividends

Category : Uncategorized

Dividends

What are the characteristics for companies that pay dividends?
Is there a correlation between price volatility and dividend yield?
Is there a correlation between PE and dividend yield?
Is there a correlation between market capitalization and dividend yield?

Price performance before and after dividend change.
Beta before and after dividend change.
Volume before and after dividend change.
Volume leading to dividend payment.

Dividend yield

*
clear all 
cd "/Users/mfd/Desktop" 

use "http://researchforprofit.com/data_private/140118_dividends.dta", clear
levelsof Symbol, local(tickers)

gen close=.

foreach aa of local tickers {
	di "`aa'"
	qui: {
		sort date
		fetchyahooquotes `aa', freq(d) field(c) merge
		drop if dividends==.
		replace close=close_`aa' if Symbol=="`aa'"
		drop *`aa'
	}
}
*
*
sort Symbol date
gen annualized_dividends=dividends
forval aa=1/12 {
	replace annualized_dividends = annualized_dividends + dividends[_n-`aa'] if (Symbol==Symbol[_n-`aa']) & ((date-date[_n-`aa'])<360)
}

* Dividend yield at the time of dividend payment===============================
gen dividend_yield=annualized_dividends/close*100
format %5.2fc dividend_yield
save temp.dta, replace
*
*
clear
fetchyahooquotes `tickers', freq(d) field(c) start(01jan2014)
drop adj*
keep if _n==_N
xpose, clear varname
drop if _n==1
replace _varname=subinstr(_varname,"close_","",.)
rename _varname Symbol	
rename v1 current_price
sort Symbol
save temp2.dta, replace

use temp.dta, clear
sort Symbol
merge Symbol using temp2.dta
drop _merge
sort Symbol date
collapse (lastnm) date dividends close annualized_dividends dividend_yield current_price, by(Symbol)

* Current dividend yield=======================================================
gen dividend_yield_current=annualized_dividends/current_price*100
format %5.2fc dividend_yield_current
gsort -dividend_yield_current
save 140118_dividend_yields.dta, replace
erase temp.dta 
erase temp2.dta

outsheet Symbol annualized_dividends current_price dividend_yield_current using "140118_SP500_dividend_yields.csv", replace
*

140118_SP500_dividend_yields
 

*
clear all 
cd "/Users/mfd/Desktop" 

use "http://researchforprofit.com/data_private/140118_dividends.dta", clear
sort Symbol date
gen annualized_dividends=dividends
forval aa=1/12 {
	replace annualized_dividends = annualized_dividends + dividends[_n-`aa'] if (Symbol==Symbol[_n-`aa']) & ((date-date[_n-`aa'])<360)
}
sort Symbol date
save temp.dta, replace

use "http://researchforprofit.com/data_private/140118_eps.dta", clear
sort Symbol date
gen annualized_eps=eps
forval aa=1/12 {
	replace annualized_eps = annualized_eps + eps[_n-`aa'] if (Symbol==Symbol[_n-`aa']) & ((date-date[_n-`aa'])<360)
}
sort Symbol date
merge Symbol date using temp.dta
erase temp.dta
drop _merge
sort Symbol date
replace dividends=dividends[_n+1] if (Symbol==Symbol[_n+1]) & (dividends==.)
replace annualized_dividends=annualized_dividends[_n+1] if (Symbol==Symbol[_n+1]) & (annualized_dividends==.)
drop if eps==.
drop if dividends==.
gen payout_ratio=annualized_dividends/annualized_eps*100
format %5.2fc payout_ratio
format %6.2fc annualized*

* keep if year(date)>2012
collapse (lastnm) date annualized* payout_ratio, by(Symbol)

* Companies that have negative annualized EPS and still pay dividends
gsort +payout_ratio
outsheet if (annualized_eps<0) using "140118_SP500_dividend_negative_eps.csv", replace

* Companies that have positive annualized EPS that is lower than dividends
gsort -payout_ratio
outsheet if (annualized_eps>0) & (annualized_eps0) & (annualized_eps>annualized_dividends) using "140118_SP500_eps_higher_than_dividend.csv", replace
*

140118_SP500_dividend_negative_eps
140118_SP500_eps_lower_than_dividend
140118_SP500_eps_higher_than_dividend
 

Performance into dividends and after dividends (actual dividend payment date)

*
clear all
cd "/users/mfd/desktop"

* Merge price and dividend data==========================================================
use "http://researchforprofit.com/data_private/140118_dividends.dta", clear
sort Symbol date
save temp
use "http://researchforprofit.com/data_private/140120_daily_prices.dta", clear
sort Symbol date
merge Symbol date using temp.dta
erase temp.dta
drop _merge
order Symbol date open high low close adjclose volume dividends
sort Symbol date
save data.dta, replace
* =======================================================================================

by Symbol: gen day=_n
encode Symbol, generate(ticker)
xtset ticker day

gen return=ln(close/L.close)

* Returns leading to the dividend payment================================================
gen return_sum_L1=L1.return
label variable return_sum_L1 "Total return for past 1 day"
forval aa=2/30 {
	local bb=`aa'-1
	gen return_sum_L`aa'=return_sum_L`bb'+L`aa'.return
	label variable return_sum_L`aa' "Total return for past `aa' days"
}
*========================================================================================

* Returns after the dividend payment=====================================================
gen return_sum_F1=F1.return
label variable return_sum_F1 "Total return for future 1 day"
forval aa=2/30 {
	local bb=`aa'-1
	gen return_sum_F`aa'=return_sum_F`bb'+F`aa'.return
	label variable return_sum_F`aa' "Total return for future `aa' days"
}
*========================================================================================

save data1.dta, replace

keep if dividend!=.
save data2.dta, replace

use data2.dta, clear
gen year=year(date)
collapse (mean) return_sum_L* return_sum_F*, by(year)
forval aa=1/30 {
	replace return_sum_L`aa'=return_sum_L`aa'*100
	replace return_sum_F`aa'=return_sum_F`aa'*100
}
format %5.2fc return*

tabstat return_sum_L5 return_sum_F5 return_sum_L10 return_sum_F10 return_sum_L15 return_sum_L15 return_sum_L20 return_sum_F20, stat(mean)
*

 


Comparing portfolios: Finding your company

Category : Archieve

Comparing portfolios: Finding your company

Please make sure you have the most updated mfd_dm and mfd_ta packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
net from "http://www.researchforprofit.com/stata/mfd_ta"
* click on the blue mfd_dm package and then "click here to install" 
*
*
clear all
cd "/Users/mfd/Desktop"
use "http://researchforprofit.com/data_public/FIN400_2014_prices.dta", clear

collapse (sum) ln*
xpose, clear varname format
gen symbol = substr(_varname,4,.)
sort v1

browse if symbol == "AAPL"
*

P/E Ratio

Category : Uncategorized

P/E Ratio

Please make sure you have the most updated mfd_dm, mfd_ta and mfd_tex packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
net from "http://www.researchforprofit.com/stata/mfd_ta"
net from "http://www.researchforprofit.com/stata/mfd_tex"
* click on the blue mfd_dm package and then "click here to install" 
*

Price to earnings (P/E) ratio is: How much the market is paying for each dollar of earnings. It is calculated by dividing market price of the stock by earnings per share. However, the price is paid once to purchase the stock and earnings are realized every year. Thus, the P/E ratio is based on investor expectations of future earnings.

If we have a P/E ratio of comparable firms or of the overall market, then we may be able to calculate an expect price for a company of our choice.

If the market is willing to pay $10 for each dollar of earnings of a comparable firm and if we expect our company’s annual earnings to be $5 then the expected price for our company would be $10 X $5 = $50.

*
mfd_dm_components, symbol(^DJA)
levelsof Symbol, local(tickers)
mfd_dm_keystats `tickers', field(n s l1 e r)
drop if Price_to_Earnings==.
egen average_PE=mean(Price_to_Earnings)
gen price_PE_multiple= Earnings_per_Share * average_PE
list
*

Screen Shot 2013-12-25 at 5.53.13 PM

Last\_T{\thicksim}e is last traded price. Earnin{\thicksim}e is earnings per share. Price\_{\thicksim}s is the P/E ratio. averag{\thicksim}E is the average P/E ratio for these companies. price\_{\thicksim}e is the price calculated using the P/E multiple.

Please note that this table is for illustration only. The earnings per share used in P/E multiple calculation is the expected earnings per share. The earnings per share that we used above is the earnings per share that is already announced. Also, we simply took the Dow Jones composite index companies. These companies are not necessarily comparable to each other. Proper P/E multiple calculate should be between comparable companies. The example above is to show you how to calculate and use P/E multiple.


Earnings surprises

Category : Uncategorized

Earnings surprises

Please make sure you have the most updated mfd_dm, mfd_ta and mfd_tex packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
net from "http://www.researchforprofit.com/stata/mfd_ta"
net from "http://www.researchforprofit.com/stata/mfd_tex"
* click on the blue mfd_dm package and then "click here to install" 
*

Yahoo! Finance provides a nice list of earnings surprises. mfd_dm downloads these daily lists as a Stata data file.

*
clear all
cd "/users/mfd/desktop"

forval month=1/12 {
	forval day=1/31 {
		local tarih: di %tdCCYYNNDD date("2014-`month'-`day'","YMD")
		di "`tarih'"
		capture: mfd_dm_ea_surprises, date(`tarih')
		if (_N>0) {
			capture: append using 140205_earnings_surprises.dta
			save 140205_earnings_surprises.dta, replace
		}
	}
}
*

 

Sample data:
Screen Shot 2014-02-05 at 2.39.13 PM

 


Analyst recommendations

Category : Uncategorized

Analyst recommendations

Please make sure you have the most updated mfd_dm and mfd_ta packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
* click on the blue mfd_dm package and then "click here to install" 
*

Investorwand.com provides a nice list of analyst recommendations. mfd_dm downloads these lists as a Stata data file.

*
clear all
cd "/users/mfd/desktop"
mfd_dm_analyst2, list
*

Sample data for the list of available symbols
Screen Shot 2014-09-28 at 1.35.19 PM
 

*
clear all
cd "/users/mfd/desktop"
mfd_dm_analyst2, ticker(TSLA)
*

Sample data for the analysts recommendations for TSLA
Screen Shot 2014-09-28 at 1.35.43 PM

*
clear all
cd "/users/mfd/desktop"

use "http://researchforprofit.com/data_public/investorwand.dta", clear
levelsof symbol, local(tickers)

foreach aa of local tickers {
	di "`aa'"
	capture: mfd_dm_analyst2, ticker("`aa'")
	if (_rc==0) {
		gen symbol="`aa'"
		capture: append using all_analysts_recommendations.dta
		save all_analysts_recommendations.dta, replace
	}
}
sort symbol signal_date research_firm
save all_analysts_recommendations.dta, replace
*

Economic events

Category : Uncategorized

Economic events

Please make sure you have the most updated mfd_dm and mfd_ta packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
net from "http://www.researchforprofit.com/stata/mfd_ta"
* click on the blue mfd_dm package and then "click here to install" 
*

Economic data is released everyday. This code downloads the data and puts them on a chart.

*
clear all
cd "/Users/mfd/Desktop"

erase economic_events.dta
forval aa=1/41 {
	di "`aa'"
	if (`aa'<10) mfd_dm_events, week (20130`aa')
	if (`aa'>=10) mfd_dm_events, week (2013`aa')
	capture: append using economic_events.dta
	save economic_events.dta, replace
}

replace date=date+",2013"
gen date_=date(date,"MDY",2050)
format %td date_
drop date
rename date_ date
sort date

split time, parse(:) gen(temp)
split temp2, gen(Temp)

rename temp1 hour
destring hour, replace

rename Temp1 minute
destring minute, replace

replace hour=hour+12 if Temp2=="PM"

drop time temp2 Temp2
order date hour minute
sort date hour minute

keep if statistic=="Initial Claims" | statistic=="Unemployment Rate" | statistic=="Mich Sentiment"

drop if strlen(actual)<4
keep for actual date statistic

replace actual=subinstr(actual,"K","",.)
replace actual=subinstr(actual,"%","",.)

destring actual, replace force

mfd_dm_prices DIA, freq(d) start(01jan2013) merge

gen actual_IC=adjclose_DIA if actual!=. & statistic=="Initial Claims"
gen actual_UR=adjclose_DIA if actual!=. & statistic=="Unemployment Rate"
gen actual_MS=adjclose_DIA if actual!=. & statistic=="Mich Sentiment"

twoway (line adjclose_DIA date) (scatter actual_IC date if statistic=="Initial Claims", mlabel(actual)), scale(0.75) 
twoway (line adjclose_DIA date) (scatter actual_UR date if statistic=="Unemployment Rate", mlabel(actual)), scale(0.75)
twoway (line adjclose_DIA date) (scatter actual_MS date if statistic=="Mich Sentiment", mlabel(actual)), scale(0.75)
*

events_MS

 


End-of-day prices

Category : Uncategorized

End-of-day prices

Please make sure you have the most updated mfd_dm and mfd_ta packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
net from "http://www.researchforprofit.com/stata/mfd_ta"
* click on the blue mfd_dm package and then "click here to install" 
*

Stata code for downloading all available historic prices for Dow Jones Composite Index components:

Each company is downloaded and merged. Thus, this is a “wide” dataset: 

*
* Download historic prices for all NYSE listed stocks
clear all
cd "/Users/mfd/Desktop"

* Get the components of NYA Index
mfd_dm_components, symbol(^NYA)
drop if Symbol==""
sort Symbol
save NYA_components.dta, replace

* Get the prices of all NYA Index components as of 130906
use NYA_components.dta, clear
levelsof Symbol, local(tickers)
mfd_dm_prices `tickers', freq(d) chg(ln) field(o c h l v)
gen day=_n
tsset day
save NYA_historic_prices.dta, replace
*

This code updates the historic price data for the given day: 

*
clear all
cd "/Users/mfd/Desktop"

local tarih="130802"
local tarih_start=date("`tarih'","YMD", 2050)-150
local end: display %td date("`tarih'","YMD", 2050)
local start: display %td `tarih_start'

use components.dta, clear
levelsof Symbol, local(tickers)
mfd_dm_prices `tickers', freq(d) start("`end'") end("`end'")
append using historic_prices.dta
save historic_prices.dta, replace
*

Each company is downloaded and appended. Thus, this is a “long” dataset: 

*
* Download historic prices for all NYSE listed stocks
clear all
cd "/Users/mfd/Desktop"

mfd_dm_components, symbol(^NYA)
drop if Symbol==""
levelsof Symbol, local(tickers)

local counter=0
foreach aa of local tickers {
    capture: mfd_dm_prices `aa', freq(d)
    if (_rc==0) & (_N>10) {
        di "`aa'"
		rename adj* adjclose
		gen symbol="`aa'"
		if (`counter'==0) {
			qui: save NYA_historic_prices.dta, replace
			local counter=1
		}
		else {
			qui: append using NYA_historic_prices.dta
			qui: save NYA_historic_prices.dta, replace			
		}
	}

}
*

Historic EPS, PE and revenues

Category : Uncategorized

Historic EPS, PE and revenues

Please make sure you have the most updated mfd_dm, mfd_ta and mfd_tex packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
net from "http://www.researchforprofit.com/stata/mfd_ta"
net from "http://www.researchforprofit.com/stata/mfd_tex"
* click on the blue mfd_dm package and then "click here to install" 
*

Historical earnings per share (EPS), revenues and price to earnings ratio (PE).

Data WITHOUT “annual” and “pe” options:

*
clear all
cd "/Users/mfd/Desktop"

mfd_dm_EPS AAPL GOOG MSFT IBM, item(eps)

mfd_dm_EPS AAPL GOOG MSFT IBM, item(eps) annual pe


mfd_dm_EPS AAPL GOOG MSFT IBM, item(revenue) annual pe
*

Screen Shot 2013-10-12 at 4.18.40 PM

Data WITH “annual” and “pe” options:

Screen Shot 2013-12-28 at 1.28.18 PM

Historic EPS for all Dow Jones 30 companies: 

*
clear all
cd "/Users/mfd/Desktop"

* Get the components of DJI Index
mfd_dm_components, symbol(^DJI)
drop if Symbol==""
sort Symbol
levelsof Symbol, local(tickers)

mfd_dm_EPS `tickers', item(eps) annual pe

keep if _n==_N
keep PE_*_annual
xpose, clear varname
sort v1
*

Historic PE, multiple companies:

*
clear all
cd "/Users/mfd/Desktop"

mfd_dm_EPS AAPL GOOG MSFT IBM, item(eps) annual pe

tsline PE_IBM_annual PE_MSFT_annual PE_GOOG_annual PE_AAPL_annual if year(date)>2010, ylabel(#20, angle(horizontal) axis(1)) xlabel(#20, angle(vertical)) scale(.70) legend(on cols(5)) 
*

PE_all

EPS and PE along with Apple events on a chart: 

*
clear all
cd "/Users/mfd/Desktop"

mfd_dm_EPS AAPL, item(eps) annual pe

gen iPhone=.
foreach aa in "29jun2007" "11jul2008" "19jun2009" "24jun2010" "14oct2011" "21sep2012" {
    replace iPhone=eps_AAPL_annual if date==date("`aa'","DMY")
}
gen iPad=.
* 3apr2010 is a weekend. Changed it to 5apr2010
foreach aa in "5apr2010" "11mar2011" "16mar2012" "2nov2012" {
	replace iPad=eps_AAPL_annual if date==date("`aa'","DMY")
}

twoway (line eps_AAPL_annual date) (scatter iPhone date, mcolor(red)) (scatter iPad date, mcolor(black)) if year(date)>2006, ylabel(#50, angle(horizontal) axis(1)) xlabel(#50, angle(vertical)) scale(.5) legend(on cols(5)) 
graph export "eps_aapl.png", replace
*

eps_aapl_annual

EPS (quarterly annualized) increase after each iPhone release: 

*
clear all
cd "/Users/mfd/Desktop"

mfd_dm_EPS AAPL, item(eps) annual pe

gen iPhone=.
foreach aa in "29jun2007" "11jul2008" "19jun2009" "24jun2010" "14oct2011" "21sep2012" {
    replace iPhone=eps_AAPL_annual if date==date("`aa'","DMY")
}
gen iPad=.
* 3apr2010 is a weekend. Changed it to 5apr2010
foreach aa in "5apr2010" "11mar2011" "16mar2012" "2nov2012" {
    replace iPad=eps_AAPL_annual if date==date("`aa'","DMY")
}

sort date
gen temp=1 if ((eps_AAPL!=eps_AAPL[_n-1]) | (iPhone!=.)) & (eps_AAPL[_n-1]!=.)
keep if temp==1

gen temp3=.
replace temp3=0 if iPhone!=.
replace temp3=1 if date-date[_n-1]>15 & iPhone[_n-1]!=.
replace temp3=1 if temp3[_n-1]==. & temp3[_n-2]==0
keep if temp3!=.

gen eps_ret=(eps_AAPL_annual[_n+1]-eps_AAPL_annual)/eps_AAPL_annual if (temp3==0)
keep date eps_AAPL_annual eps_ret
order date eps_AAPL_annual eps_ret
*

Screen Shot 2013-09-08 at 11.27.44 PM

 


Intraday prices

Category : Uncategorized

Intraday prices

Please make sure you have the most updated mfd_dm, mfd_ta and mfd_tex packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
net from "http://www.researchforprofit.com/stata/mfd_ta"
net from "http://www.researchforprofit.com/stata/mfd_tex"
* click on the blue mfd_dm package and then "click here to install" 
*

This post is rather technical. It basically explains the steps of downloading intraday prices from different sources.

NASDAQ

Nasdaq provides real-time prices tick-by-tick throughout the day. Prices are available for pre- and port-market hours as well. This is a free and very valuable service.

*
clear all
cd "/Users/mfd/Desktop"
local tarih="130906"
local symbol="AAPL"

* Intraday
mfd_dm_intraday `symbol', time(1)
forval aa=2/13 {
	di "`aa'"
	mfd_dm_intraday `symbol', time(`aa') append
}
gsort time -page -sequence
gen market="intraday"
save `tarih'_`symbol'_intra.dta, replace

* Pre-market hours
capture: mfd_dm_preday `symbol', time(1)
forval aa=2/11 {
	di "`aa'"
	capture: mfd_dm_preday `symbol', time(`aa') append
}
gsort time -page -sequence
gen market="preday"
save `tarih'_`symbol'_pre.dta, replace

* Post-market hours
capture: mfd_dm_postday `symbol', time(1)
forval aa=2/8 {
	di "`aa'"
	capture: mfd_dm_postday `symbol', time(`aa') append
}
gsort time -page -sequence
gen market="postday"
save `tarih'_`symbol'_post.dta, replace

use `tarih'_`symbol'_intra.dta, clear
append using `tarih'_`symbol'_pre.dta
append using `tarih'_`symbol'_post.dta
gen trading_period=-1 if market=="preday"
replace trading_period=0 if market=="intraday"
replace trading_period=1 if market=="postday"
gsort trading_period time -page -sequence
save `tarih'_`symbol'.dta, replace

gen seq=_n
tsset seq
tsline nls_price
*

Yahoo! Finance

* Reference: http://www.quantshare.com/sa-426-6-ways-to-download-free-intraday-and-tick-data-for-the-us-stock-market
* Format : http://chartapi.finance.yahoo.com/instrument/1.0/[TICKER]/chartdata;type=quote;range=1d/csv
* Example: http://chartapi.finance.yahoo.com/instrument/1.0/GOOG/chartdata;type=quote;range=1d/csv

*
clear all
cd "/users/mfd/desktop"

mata:
	string file_get_contents (string scalar raw)
	{
		fh = fopen(raw, "r")
		raw=""
		while ((line=fget(fh))!=J(0,0,"")) {
			raw=raw+char(13)+line
		}
		fclose(fh)
		return (raw)
	}

	void get_data (string scalar symbol)
	{	
		icerik = file_get_contents("http://chartapi.finance.yahoo.com/instrument/1.0/" + symbol + "/chartdata;type=quote;range=1d/csv")
		satir = tokens(icerik, char(13))			
		sutun=satir'
		st_addvar("str244", "myvar")
		st_addobs(rows(sutun))
		st_sstore(.,"myvar",sutun)
	}
end
*
*
local ticker="AAPL"
mata: get_data("`ticker'")

drop if strlen(myvar)<5
capture: split myvar, parse(",") 
rename myvar1 date
rename myvar2 close
rename myvar3 high
rename myvar4 low
rename myvar5 open
rename myvar6 volume

destring date close high low open volume, replace force

* Timestamp
replace date = (date * 1000) + (msofhours(24)*3653) - msofhours(5)
drop myvar*
gen date2 = dofc(date)
format %tc date
format %td date2

drop if date==.
*

Netfonds.no.

* Reference: http://www.quantshare.com/sa-426-6-ways-to-download-free-intraday-and-tick-data-for-the-us-stock-market
* Example for tick data:
* http://hopey.netfonds.no/tradedump.php?date=20120423&paper=AAPL.O&csv_format=txt

*
clear all
cd "/users/mfd/desktop"
insheet using "http://hopey.netfonds.no/tradedump.php?date=20140114&paper=AAPL.O&csv_format=txt"

* Get date and time
capture: split time, parse("T") 
	* date
	gen date=date(time1,"YMD")
	format %td date
	order date

	* time
	gen hour=substr(time2,1,2)
	destring hour, replace force
	gen minute=substr(time2,3,2)
	destring minute, replace force
	gen second=substr(time2,5,2)
	destring second, replace force

keep date hour minute second price quantity
order date hour minute second price quantity
*
*
clear all
cd "/users/mfd/desktop"

* To get bid/ask price and volume data:
insheet using "http://hopey.netfonds.no/posdump.php?date=20140114&paper=AAPL.O&csv_format=txt"

* Get date and time
capture: split time, parse("T") 
	* date
	gen date=date(time1,"YMD")
	format %td date
	order date

	* time
	gen hour=substr(time2,1,2)
	destring hour, replace force
	gen minute=substr(time2,3,2)
	destring minute, replace force
	gen second=substr(time2,5,2)
	destring second, replace force

drop time*
order date hour minute second
*

PE by MCap

Category : Uncategorized

PE by MCap

Please make sure you have the most updated mfd_dm, mfd_ta and mfd_tex packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
net from "http://www.researchforprofit.com/stata/mfd_ta"
net from "http://www.researchforprofit.com/stata/mfd_tex"
* click on the blue mfd_dm package and then "click here to install" 
*

PE by market capitalization.

*
clear all
cd "/Users/mfd/Desktop"

* Get the components of S&P500 Index
mfd_dm_components_cnn sp500
drop if Symbol==""
sort Symbol
levelsof Symbol, local(tickers)

mfd_dm_EPS `tickers', item(eps) annual pe price
sort date
save data_eps.dta, replace

mfd_dm_EPS `tickers', item(revenue) annual price
sort date
save data_revenue.dta, replace

mfd_dm_yahooapi `tickers', field(s j2)
gen symbol=substr(v1,1,strpos(v1,",")-1)
gen shares=trim(substr(v1,strpos(v1,",")+1,.))
replace shares=subinstr(shares,",","",.)
destring shares, replace force
drop v1
sort symbol
save data_shares.dta, replace

use data_eps.dta, clear
merge date using data_revenue.dta
drop _merge
sort date
gen day=_n
tsset day
save data_sp500.dta, replace

* PE and revenues
clear all
cd "/Users/mfd/Desktop"
use data_sp500.dta, clear
keep date PE_*_annual revenue_*_annual adj*
keep if _n==_N
xpose, clear varname
drop if _n==1

gen PE_annual=v1 if (strpos(_varname,"PE")==1)
gen revenue_annual=v1 if (strpos(_varname,"revenue")==1)
gen price=v1 if (strpos(_varname,"adjclose")==1)

replace _varname=subinstr(_varname,"PE_","",.)
replace _varname=subinstr(_varname,"revenue_","",.)
replace _varname=subinstr(_varname,"_annual","",.)
replace _varname=subinstr(_varname,"adjclose_","",.)
drop v1
rename _varname symbol
sort symbol
replace PE_annual=PE_annual[_n-1] if (PE_annual==.) & (symbol==symbol[_n-1])
replace PE_annual=PE_annual[_n-2] if (PE_annual==.) & (symbol==symbol[_n-2])
replace PE_annual=PE_annual[_n+1] if (PE_annual==.) & (symbol==symbol[_n+1])
replace PE_annual=PE_annual[_n+2] if (PE_annual==.) & (symbol==symbol[_n+2])

replace revenue_annual=revenue_annual[_n-1] if (revenue_annual==.) & (symbol==symbol[_n-1])
replace revenue_annual=revenue_annual[_n-2] if (revenue_annual==.) & (symbol==symbol[_n-2])
replace revenue_annual=revenue_annual[_n+1] if (revenue_annual==.) & (symbol==symbol[_n+1])
replace revenue_annual=revenue_annual[_n+2] if (revenue_annual==.) & (symbol==symbol[_n+2])

replace price=price[_n-1] if (price==.) & (symbol==symbol[_n-1])
replace price=price[_n-2] if (price==.) & (symbol==symbol[_n-2])
replace price=price[_n+1] if (price==.) & (symbol==symbol[_n+1])
replace price=price[_n+2] if (price==.) & (symbol==symbol[_n+2])

duplicates drop

sort symbol
merge symbol using data_shares
drop _merge

drop if PE_annual==.
drop if revenue_annual==.

drop if PE_annual>100
drop if PE_annual<-100

replace revenue_annual=revenue_annual/1000000000
twoway (scatter PE_annual revenue_annual if symbol!="AAPL", mlabel(symbol)) (scatter PE_annual revenue_annual if symbol=="AAPL", mlabel(symbol)), ylabel(#30, angle(horizontal)) xlabel(#50, angle(vertical)) scale(.7) legend(off)

gen mcap=shares*price/1000000000
twoway (scatter PE_annual mcap if symbol!="AAPL", mlabel(symbol)) (scatter PE_annual mcap if symbol=="AAPL", mlabel(symbol)), ylabel(#30, angle(horizontal)) xlabel(#50, angle(vertical)) scale(.7) legend(off)
*

sp500_aapl_mcap


PE by Revenue

Category : Uncategorized

PE by Revenue

Please make sure you have the most updated mfd_dm, mfd_ta and mfd_tex packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
net from "http://www.researchforprofit.com/stata/mfd_ta"
net from "http://www.researchforprofit.com/stata/mfd_tex"
* click on the blue mfd_dm package and then "click here to install" 
*
*
clear all
cd "/Users/mfd/Desktop"

* Get the components of S&P500 Index
mfd_dm_components_cnn sp500
drop if Symbol==""
sort Symbol
levelsof Symbol, local(tickers)

mfd_dm_EPS `tickers', item(eps) annual pe price
sort date
save data_eps.dta, replace

mfd_dm_EPS `tickers', item(revenue) annual price
sort date
save data_revenue.dta, replace

use data_eps.dta, clear
merge date using data_revenue.dta
drop _merge
sort date
gen day=_n
tsset day
save data_dji.dta, replace

* PE and revenues
clear all
cd "/Users/mfd/Desktop"
use data_dji.dta, clear
keep date PE_*_annual revenue_*_annual
keep if _n==_N
xpose, clear varname
drop if _n==1

gen PE_annual=v1 if strpos(_varname,"PE")
gen revenue_annual=v1 if strpos(_varname,"revenue")

replace _varname=subinstr(_varname,"PE_","",.)
replace _varname=subinstr(_varname,"revenue_","",.)
replace _varname=subinstr(_varname,"_annual","",.)
drop v1
rename _varname symbol
sort symbol
replace PE_annual=PE_annual[_n-1] if (PE_annual==.) & (symbol==symbol[_n-1])
replace PE_annual=PE_annual[_n+1] if (PE_annual==.) & (symbol==symbol[_n+1])

replace revenue_annual=revenue_annual[_n-1] if (revenue_annual==.) & (symbol==symbol[_n-1])
replace revenue_annual=revenue_annual[_n+1] if (revenue_annual==.) & (symbol==symbol[_n+1])
duplicates drop

drop if PE_annual==.
drop if revenue_annual==.

drop if PE_annual>100
drop if PE_annual<-100

replace revenue_annual=revenue_annual/1000000000
twoway (scatter PE_annual revenue_annual if symbol!="AAPL", mlabel(symbol)) (scatter PE_annual revenue_annual if symbol=="AAPL", mlabel(symbol)), ylabel(#30, angle(horizontal)) xlabel(#50, angle(vertical)) scale(.7) legend(off)
*

pe_revenue


Picking a low debt ratio stock

Category : Uncategorized

Picking a low debt ratio stock

Please make sure you have the most updated mfd_dm, mfd_ta and mfd_tex packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
net from "http://www.researchforprofit.com/stata/mfd_ta"
net from "http://www.researchforprofit.com/stata/mfd_tex"
* click on the blue mfd_dm package and then "click here to install" 
*

If you need to pick a stock that has low debt ratio, then you need to download stocks’ financial data and sort them based on their debt ratios.

*
* How to obtain Liability/Assets
clear all
cd "/Users/mfd/Desktop"

* Get the list of all DJI component companies (^DJI)
* Get the list of all DJA component companies (^DJA)
* Get the list of all NYSE component companies (^NYA)
* Get the list of all NASDAQ component companies (^IXIC)
mfd_dm_components, symbol(^DJI)
levelsof Symbol, local(tickers)

local counter=1
foreach aa of local tickers {
	di "BS `counter': `aa'"
	clear
	qui: capture: mfd_dm_statements `aa', freq(a) st(BS) type(wider)
	if (_rc==0) { 	
		qui: if (`counter'!=1) append using BS.dta
		qui: save BS.dta, replace
		local counter = `counter'+1	
	}
}

gen liabities_to_assets = total_liabilities/total_assets
keep symbol name date liabities_to_assets
keep if (year(date)==2012)
sort liabities_to_assets
*

Screen Shot 2013-12-28 at 2.02.01 PM


Picking a low PE stock

Category : Uncategorized

Picking a low PE stock

Please make sure you have the most updated mfd_dm, mfd_ta and mfd_tex packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
net from "http://www.researchforprofit.com/stata/mfd_ta"
net from "http://www.researchforprofit.com/stata/mfd_tex"
* click on the blue mfd_dm package and then "click here to install" 
*

If you wanted to pick a stock with a low PE, then you need to download PEs and sort them.

*
* How to pick lowest PE
clear all
cd "/Users/mfd/Desktop"

* Get the list of all DJI component companies (^DJI)
* Get the list of all DJA component companies (^DJA)
* Get the list of all NYSE component companies (^NYA)
* Get the list of all NASDAQ component companies (^IXIC)
mfd_dm_components, symbol(^DJI)
levelsof Symbol, local(tickers)

mfd_dm_EPS `tickers', item(eps) annual pe

keep date PE_*_annual
keep if (month(date)==12) & (year(date)==2012)
keep if _n==_N
drop date
xpose, clear varname
sort v1
browse
*

Screen Shot 2013-12-28 at 1.51.55 PM


SEC 13F-HR

Category : Uncategorized

SEC 13F-HR

Please make sure you have the most updated mfd_dm, mfd_ta and mfd_tex packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
net from "http://www.researchforprofit.com/stata/mfd_ta"
net from "http://www.researchforprofit.com/stata/mfd_tex"
* click on the blue mfd_dm package and then "click here to install" 
*

Download the list of 13F-HR filers

SEC provides the 13F-HR forms. This code downloads the list of all 13F-HR filers for the 2013.

*
clear all
cd "/users/mfd/desktop"
forval aa=2/4 {
	mfd_dm_SEC_idx 2013/QTR`aa'/form.idx
	keep if trim(filing_type)=="13F-HR"
	keep company_name CIK
	capture: append using 13FHR_filers.dta
	save 13FHR_filers.dta, replace
}
compress
duplicates drop
replace company_name=lower(company_name)
sort company_name
save 13FHR_filers.dta, replace
*

Screen Shot 2013-11-17 at 4.37.04 PM

 

Download SEC form 13F-HR

It can be important to know institutional investor portfolios. For instance, I wanted to know which institutional investor increased/decreased its AAPL holdings.

*
clear all
cd "/users/mfd/desktop"

forval aa=1/10 {
	qui: mfd_dm_SEC_filings 0001110806, filing ("13")
	qui: mfd_dm_SEC_xml `aa'
	qui: capture: append using 13FHR.dta
	qui: save 13FHR.dta, replace
	di "`aa'"
}
sort nameOfIssuer_ filing_date
twoway (line shrsOrPrnAmt_sshPrnamt filing_date) if nameOfIssuer_=="APPLE INC"
*

Screen Shot 2013-11-16 at 12.36.19 AM

 

13fhr


SEC archive file

Category : Uncategorized

SEC archive file

Please make sure you have the most updated mfd_dm, mfd_ta and mfd_tex packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
net from "http://www.researchforprofit.com/stata/mfd_ta"
net from "http://www.researchforprofit.com/stata/mfd_tex"
* click on the blue mfd_dm package and then "click here to install" 
*

SEC Edgar provides periodic archive files that includes all filed forms.

*
clear all
cd "/users/mfd/desktop"

mfd_dm_SEC_idx 2013/QTR3/form.idx
*

Screen Shot 2013-11-17 at 12.24.35 AM


SEC common shares outstanding

Category : Uncategorized

SEC common shares outstanding

Please make sure you have the most updated mfd_dm, mfd_ta and mfd_tex packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
net from "http://www.researchforprofit.com/stata/mfd_ta"
net from "http://www.researchforprofit.com/stata/mfd_tex"
* click on the blue mfd_dm package and then "click here to install" 
*

In this post, we will be downloading historic common shares outstanding for Apple Inc. SEC provides financial reports of public companies through Edgar On-line. fetchedgarxml downloads financial reports such as 10-K or 10-Q. You can even download form 4.

In this example, we will focus on historic common shares outstanding for Apple Inc. for the past 11 quarters.

*

clear all
cd "/Users/mfd/Desktop"

forval aa=1/11 {
	di "`aa'"
	qui: {
		mfd_dm_SEC_filings AAPL, filing("10-Q")
		mfd_dm_SEC_xml `aa'
		keep if item=="CommonStockSharesOutstanding"
		if (`aa'!=1) append using CommonStockSharesOutstanding.dta
		save CommonStockSharesOutstanding.dta, replace
	}
}
gen date=date(substr(contextRef,strpos(contextRef,"_20")+1,8),"YMD")
drop item unitRef decimals contextRef id
format date %td
sort date
duplicates drop date, force

gen change=amount-amount[_n-1]
format change %18.2fc
save AAPL.dta, replace
*

AAPL shares

Net Income

*
clear all
cd "/Users/mfd/Desktop"

local search_item="netincomeloss"
local file="140118_netincome.dta"

foreach bb in "AAPL" "GOOG" "MSFT" "AMZN" "XOM" "AA" {
	forval aa=1/12 {
		di "`bb': `aa'"
		qui: {
			mfd_dm_SEC_filings `bb', filing("10-Q")
			capture: mfd_dm_SEC_xml `aa'
			if (_rc==0) {
				if (_N>0) {
					keep if lower(item)=="`search_item'"
					gen Symbol="`bb'"
					capture: append using `file'
					save `file', replace
				}
			}
		}
	}
	forval aa=1/5 {
		di "`bb': `aa'"
		qui: {
			mfd_dm_SEC_filings `bb', filing("10-K")
			capture: mfd_dm_SEC_xml `aa'
			if (_rc==0) {
				if (_N>0) {
					keep if lower(item)=="`search_item'"
					gen Symbol="`bb'"
					capture: append using `file'
					save `file', replace
				}
			}
		}
	}
}

use "140118_netincome.dta", clear
replace contextRef=subinstr(contextRef,"--","-",.)
replace contextRef=subinstr(contextRef,"--","-",.)
replace contextRef=subinstr(contextRef,"--","-",.)

capture: split contextRef, parse("_") 
drop if contextRef7!=""

gen date=date(contextRef5,"YMD")
format %td date
drop if date==.

capture: split contextRef2, parse("-") 
drop item contextRef unitRef decimals id contextRef1 contextRef2 contextRef3 contextRef5 contextRef6 contextRef7 contextRef21 contextRef22

order Symbol date contextRef4 contextRef23 amount
duplicates drop
destring contextRef4, replace force
sort Symbol date contextRef4

rename contextRef4 period
rename contextRef23 filing
gen filing_type=substr(filing,1,1)
gen filing_order=substr(filing,2,.)
drop filing
destring filing_order, replace force
capture: drop contextRef*
*

Splits calendar

Category : Uncategorized

Splits calendar

Please make sure you have the most updated mfd_dm, mfd_ta and mfd_tex packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
net from "http://www.researchforprofit.com/stata/mfd_ta"
net from "http://www.researchforprofit.com/stata/mfd_tex"
* click on the blue mfd_dm package and then "click here to install" 
*

Yahoo! Finance provides a nice list of splits. mfd_dm downloads these daily lists as a Stata data file.

*

clear all
cd "/users/mfd/desktop"

forval year=2000/2014 {
	forval month=1/12 {
		di "`month'/`year'"
		capture: mfd_dm_splits_calendar, year(`year') month(`month')
		if (_N>0) {
			capture: append using 140205_splits_calendar.dta
			save 140205_splits_calendar.dta, replace
		}
	}
}
*

Sample data:

Screen Shot 2014-02-05 at 4.51.02 PM

 


Cumulative result after an event

Category : Uncategorized

Cumulative result after an event

Please make sure you have the most updated mfd_dm and mfd_ta packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
net from "http://www.researchforprofit.com/stata/mfd_ta"
* click on the blue mfd_dm package and then "click here to install" 
*

In order for the Stata code on this post to work, you need to have our data mining package (mfd_dm) and technical analysis package (mfd_ta) installed on your Stata. You can find the mfd_dm package here. You can find the mfd_ta package here.

We need to calculate cumulative returns after an event/condition. I’ve used this with back-test of Bollinger bands before.

*

clear all
cd "/Users/mfd/Desktop"

mfd_dm_prices AAPL, freq(d) chg(ln) start(01jan2010) field(c)
gen day=_n
tsset day
gen year=year(date)

mfd_ta_bollinger close_AAPL

gen condition_AAPL=1 if (L.close_AAPL<=L.lower_band)
mfd_stat_cret AAPL, ret_type(l_cc) period(1) current newvar(profit_AAPL) condition(condition_AAPL) clear

tabstat profit_AAPL, stat(mean sum min max) by(year)

*

Screen Shot 2013-09-15 at 10.31.15 PM