% draw line from Low to High for i=1:l
line([i i],[L(i) H(i)]) end
% draw red body (down day) n=find(d<0); if ~isempty(n) for i=1:length(n)
x=[n(i)-w n(i)-w n(i)+w n(i)+w n(i)-w]; y=[O(n(i)) C(n(i)) C(n(i)) O(n(i)) O(n(i))]; patch(x,y,'g') end end
% draw blue body(up day) n=find(d>=0); if ~isempty(n) for i=1:length(n)
x=[n(i)-w n(i)-w n(i)+w n(i)+w n(i)-w]; y=[O(n(i)) C(n(i)) C(n(i)) O(n(i)) O(n(i))]; patch(x,y,'r') end end hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [RSV]=rsv(high,low,close,m) % row stochatic value
% high = highest price in the trading holizon % low = lowest price in the trading holizon % close = close price in the trading holizon
% m = length of highest prices holizon you selected
Tfault set up:
% RSV:=(CLOSE-LLV(LOW,9))/(HHV(HIGH,9)-LLV(LOW,9))*100;
if nargin < 5 m=9; end
[hi ,hj] = size(high); [li ,lj] = size(low); [ci ,cj] = size(close);
if hi~=li || hi~=ci || li~=ci
error(' inputed price columns must be equal') end
if hj>1 || lj>1 ||cj>1
error( ' input prices matrix must be column') end
LLV = llv(low,m); HHV = hhv(high,m);
RSV = ((close -LLV )./(HHV -LLV ))*100;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [LLV]=llv(a,n) %
% LLV = the minest data in the last m trading days % data = low stock price in the trading day % prepare
[vi , vj] = size (a); LLV = zeros(vi,vj);
if length(n)==1 for j=1:vi if j>=n
LLV(j,:) = min(a(j-n+1:j,:)); else
LLV(j,:)=min(a(1:j,:)); end end end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [HHV]=hhv(a,n) [vi , vj] = size (a); HHV = zeros(vi,vj);
if length(n)==1 for j=1:vi if j>=n
HHV(j,:) = max(a(j-n+1:j,:)); else
HHV(j,:)=max(a(1:j,:)); end
end end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [out1] = exceedence_corr(X,Y,qc,indic)
% Computes the 'exceedence correlations' discussed in % Ang and Chen, Journal of Financial Economics, 2002. %
% Corr[X,Y|X>v,Y>v] if v>0 and X % Testing the significance of the differences using the test of Hong, Tu and Zhou (2003). % % INPUTS: X, a Tx1 vector of data % Y, a Tx1 vector of data % qc, a (k/2)x1 vector of quantiles or standard deviations to estimate the correl. must be increasing, and only the % \ % indic, a scalar, equals 1 if using quantiles to determine cut-offs, =0 if using standard deviations % % OUTPUT: out1, a (k+1)x1 vector of the measure at each quantile % Andrew Patton if nargin<4 indic=1; % using quantiles to determine cut-offs end if nargin<3 qc=0.5; % just looking at above or below median returns end k = size(qc,1); if indic==1 % firstly: ripping through to make sure I have enough data in each tail qc2 = qc; for jj=1:length(qc); temp1 = find( (X<=quantile(X,1-qc(jj))).*(Y<=quantile(Y,1-qc(jj))) ); temp2 = find( (X>=quantile(X,qc(jj))).*(Y>=quantile(Y,qc(jj))) ); if ((length(temp1)<3)+(length(temp2)<3))>0 % then not enough data to compute a correlation in at least one tail qc2 = setdiff(qc2,qc(jj)); end end % now I am sure that each cut-off has enough data qc2 = sortrows(unique([qc2;1-qc2])); % sorting and making sure that 0 does not appear twice if isempty(find(qc==0.5))==0 % then q=0.5 is a cut-off qc1 = [qc2(1:(end+1)/2);qc2((end+1)/2:end)]; else qc1 = qc2; end q = qc1(end/2+1:end); k = length(q); for jj = 1:k; temp1 = find( (X<=quantile(X,1-qc(jj))).*(Y<=quantile(Y,1-qc(jj))) ); temp2 = find( (X>=quantile(X,qc(jj))).*(Y>=quantile(Y,qc(jj))) ); out1(jj,1) = corrcoef12(X(temp1),Y(temp1)); out1(end+1-jj,1) = corrcoef12(X(temp2),Y(temp2)); end end if indic==0 X = (X-mean(X))./std(X); Y = (Y-mean(Y))./std(Y); % firstly: ripping through to make sure I have enough data in each tail qc2 = qc; for jj=1:length(qc); temp1 = find( (X<=-qc(jj)).*(Y<=-qc(jj)) ); temp2 = find( (X>=qc(jj)).*(Y>=qc(jj)) ); if ((length(temp1)<3)+(length(temp2)<3))>0 % then not enough data in at least one tail qc2 = setdiff(qc2,qc(jj)); end end % now I am sure that each cut-off has enough data qc2 = sortrows(unique([qc2;-qc2])); % sorting and making sure that 0 does not appear twice if isempty(find(qc==0))==0 % then c=0 is a cut-off qc1 = [qc2(1:(end+1)/2);qc2((end+1)/2:end)]; else qc1 = qc2; end c = qc1(end/2+1:end); k = length(c); for jj = 1:k; temp1 = find( (X<=-c(jj)).*(Y<=-c(jj)) ); out1(jj,1) = corrcoef12(X(temp1),Y(temp1)); temp2 = find( (X>=c(jj)).*(Y>=c(jj)) ); out1(end+1-jj,1) = corrcoef12(X(temp2),Y(temp2)); end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function xy = corrcoef12(x,y) % CORRCOEF(X) is a matrix of correlation coefficients formed % from array X whose each row is an observation, and each % column is a variable. % CORRCOEF(X,Y), where X and Y are column vectors is the same as % CORRCOEF([X Y]). % % If C is the covariance matrix, C = COV(X), then CORRCOEF(X) is % the matrix whose (i,j)'th element is % % C(i,j)/SQRT(C(i,i)*C(j,j)). switch nargin case 1 c = cov(x); case 2 c = cov(x,y); otherwise error('Not enough input arguments.'); end d = diag(c); xy = c./sqrt(d*d'); xy = xy(1,2); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%