Code covered by the BSD License  

Highlights from
Candlestick Quantification

image thumbnail
from Candlestick Quantification by ted teng
Function to quantify candlesticks.

createCandleStick(open,close,high,low)
function output = createCandleStick(open,close,high,low)
%%this function creates a binerized candle stick code, given the
%%open,close,high, low of a stock price.

%each coloumn of the output will a candlestick for one unit(e.g., day).

if nargin < 4
    display('create_candle_stick.m needs at least two parameters');
    return;
end

[sz1,sz2] = size(open);
output=zeros(sz1,6);
for ind = 1 : sz1

            %% color of candle stick (up or down)
            color = (close(ind)-open(ind));
            %white
            if color > 0
                    output(ind,1:2) = [ 1, 0 ];
            %black
            else if color < 0
                    output(ind,1:2) = [ 0, 1 ];
                 %or neutral... you may not need this.
                 else
                    output(ind,1:2) = [ 0, 0 ];
                 end
            end

            %% size of the body (strength of bull/bear)
            %small
            if (close(ind)-open(ind)) == 0,
                output(ind,3:4) = [ 0, 1 ];
            else
                length = abs(close(ind)-open(ind))/abs(high(ind)-low(ind));
                %large
                if length > 0.66
                        output(ind,3:4) = [ 1, 0 ];
                %small
                elseif length < 0.33
                        output(ind,3:4) = [ 0, 1 ];
                %medium
                else 
                        output(ind,3:4) = [ 1, 1 ];
                end
            end

            %% shift of the body relative to stick
            
            % middle
            if (close(ind)-open(ind)) == 0
                output(ind,5:6) = [ 0, 0 ];
            else                
                shift = abs(mean([close(ind),open(ind)]) - low(ind) )/abs(high(ind)-low(ind));
                % upper
                if shift > 0.66
                        output(ind,5:6) = [ 0, 1 ];
                % middle
                elseif shift >= 0.33 && shift <= 0.66
                        output(ind,5:6) = [ 1, 1 ];
                % lower
                elseif shift < 0.33
                        output(ind,5:6) = [ 1, 0 ];
                end
            end

end

Contact us