Showing posts with label Data Analysis. Show all posts
Showing posts with label Data Analysis. Show all posts

Tuesday, February 16, 2010

get_hisVolatility.m


function vol = get_hisVolatility(closes, N)
%
%   This script is to calculate the historical
%   volatility for close prices using N days sliding window
%   If N is not specified, a default of 20 days will be
%   used.
%
%   Copyright 2010 EdgeMe
%   EdgeMe, 12-Feb-2010
%
%
if nargin==0
    N=20;
end

    dataN=numel(closes);
    vol=[];
    if dataN<N
      warning ('Not enough data')
      return;
    end

    log_change(1)=0;
    for i=2:dataN
    log_change(i) = log(closes(i)/closes(i-1));
    end

    for i=N+1:dataN
    stdev = std(log_change(i-N+1:i));

    % Normalize to annual volatility
    vol(i)= stdev*sqrt(252);
    end

    %fillin the first N days using N+1 vol
    for i=1:N
        vol(i)=vol(N+1);
    end

end

Monday, February 15, 2010

ema.m


function dataout = ema(datain,period)
%
% This function is to get EMA of a given datain and a given period
%
%   Copyright 2010 EdgeMe
%   EdgeMe, 10-Feb-2010
%
%
    f = 2/(period+1);
    N = numel(datain);
    dataout = zeros(N,1);
    dataout(1) = datain(1);
    for i=2:N
    dataout(i) = f*(datain(i)-dataout(i-1)) + dataout(i-1);
    end

end

Saturday, February 13, 2010

gen_myList.m


%
% This script is to generate a watch list according to your magic formula criteria
%

% to get list of symbols we want to choose from
symbols=get_symbols('all_symbols.txt');

% initialize the result list
myList=[];

% now loop thru all symbols
for i=1:numel(symbols)

    % get data for each symbol
    [dates opens highs lows closes volumes]=get_symbol_data(symbols{i},2);

    % for this example, we will generate a list of stock that move up 2% for the day
    % so we need only 2 days of data; change the criteria of your own if you wish.

    % make sure we have data for the symbol
    if (~isempty(closes))
       % now let's find out if the stock meet our 2% up day
        if (100*(closes(2)-closes(1)))/closes(1) >= 2
        %ok, this one is up 2%
        % save into the list
          myList{end+1}=symbols{i};
        end
    end

end

% now we are here, myList should contain all the stock that met our criteria.
% we can save into a HTML file with a hyperlink to a charting website
if ~isempty(myList) %make sure we have at least one stock that met our criteria to save to a file
 save2html('myList.html',myList,'My list per my magic formula'); % generate a HTML file
 disp('Your magic stock list is generated in myList.html');
else
 disp('Sorry, No stock met your magic formula');
end