Relative Strength Index (RSI)

Category : Uncategorized

Relative Strength Index (RSI)

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" 
*

Stata code for backtesting RSI for DJA components: 

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

mfd_dm_components, symbol(^DJA) page(2)
drop if Symbol==""
levelsof Symbol, local(tickers)

local counter=1
foreach aa of local tickers {
	di "`counter': `aa'"
	clear
	capture: mfd_dm_prices `aa', freq(d) field(o c h l)
	gen day=_n
	tsset day
	mfd_ta_RSI adjclose_`aa'
	mfd_ta_RSI adjclose_`aa', backtest("20-80")
	capture: summ date
	if (_rc==0) { 
		if (`counter'!=1) append using RSI_backtest.dta
		save RSI_backtest.dta, replace
		local counter = `counter'+1	
	}
}

sort symbol date

save RSI_backtest.dta, replace

* all companies by year
clear all
cd "/Users/mfd/Desktop"
local counter=1
foreach aa in "L_profit" "L_profit_1p" "H_profit" "H_profit_1p" {
	di "`counter': `aa'"
	qui: {
		use RSI_backtest.dta, clear
		gen year=year(date)
		collapse (mean) `aa'_mean=`aa' (semean) `aa'_semean=`aa' (count) `aa'_N=`aa', by(year)
		gen `aa'_t=`aa'_mean/`aa'_semean
		gen `aa'_pval=2*ttail(`aa'_N,abs(`aa'_t)) 
		gen `aa'_star=""
		replace `aa'_star="*" if `aa'_pval<=0.10
		replace `aa'_star="**" if `aa'_pval<=0.05
		replace `aa'_star="***" if `aa'_pval<=0.01		
		keep year `aa'_mean `aa'_star `aa'_semean
		order year `aa'_mean `aa'_star `aa'_semean
		sort year
		if (`counter'!=1) {
			merge year using stats.dta
			drop _merge
			sort year
		}
		save stats.dta, replace
		local counter = `counter'+1		
	}
}

* by symbol year
clear all
cd "/Users/mfd/Desktop"
local counter=1
foreach aa in "L_profit" "L_profit_1p" "H_profit" "H_profit_1p" {
	di "`counter': `aa'"
	qui: {
		use RSI_backtest.dta, clear
		gen year=year(date)
		collapse (mean) `aa'_mean=`aa' (semean) `aa'_semean=`aa' (count) `aa'_N=`aa', by(symbol year)
		gen `aa'_t=`aa'_mean/`aa'_semean
		gen `aa'_pval=2*ttail(`aa'_N,abs(`aa'_t)) 
		gen `aa'_star=""
		replace `aa'_star="*" if `aa'_pval<=0.10
		replace `aa'_star="**" if `aa'_pval<=0.05
		replace `aa'_star="***" if `aa'_pval<=0.01		
		keep symbol year `aa'_mean `aa'_star `aa'_semean
		order symbol year `aa'_mean `aa'_star `aa'_semean
		sort symbol year
		if (`counter'!=1) {
			merge symbol year using stats.dta
			drop _merge
			sort symbol year
		}
		save stats.dta, replace
		local counter = `counter'+1		
	}
}
*