image thumbnail
from Price/Volume chart by Foo
Plots the stock price with the volume as a horizontal bar histogram

pricevolume_chart(date_vect,close_day, volume_vect)
function [ ] = pricevolume_chart(date_vect,close_day, volume_vect)
%% pricevolume_chart
%   PRICEVOLUME_CHART(date_vect,close_day, volume_vect) 
%   Plots the price as a line and the volume at the closing price as a
%   horizontal bar chart. DATE_VECT is the time series dates in datenum
%   format. CLOSE_DAY is the closing price. VOLUME_VECT is the daily volume
%   
%   The function creates one bin for every dollar of price the stock trades
%   over and then allocates the traded volume in the time series in the
%   correct bin. The graph is a combination of a line graph for the stock
%   price and a horizontal bar graph for the volume histogram.
    
    %Create Bins
    % Round prices to nearest integer to calculate number of bins
    close_price_int = round(close_day); 
    % Calculate number of bins, max price in interval minus min price plus
    % one bin.
    noofbins = max(close_price_int)- min(close_price_int)+1; 
    % Create price scale for bins
    bins_price = (min(close_price_int)-1)+ cumsum(ones(noofbins,1)); 
    % Create bins matrix with price in first column and no of stocks in
    % 2nd. Initially set to zero
    bins_matrix = [bins_price, zeros(length(bins_price),1)];
    
    % Fill bins with volume data
    % Loop through the whole closing price vector
    for i=1:length(close_price_int)
        % Fit closing price element in the right bin
        for k=1:length(bins_price)
            if close_price_int(i) == bins_matrix(k,1)
                % Add the volume for that price and day in the right bin
                bins_matrix(k,2) = bins_matrix(k,2)+volume_vect(i,1);
            end
        end
    end
    
    % Plot Figure
    pv=figure;
    % use barh plot for the volume histogram
    h = barh(bins_matrix(:,1), bins_matrix(:,2),1, 'g');
    
    % Set properties for volume plot
    ax(1)=gca;
    set(ax(1), 'XAxisLocation', 'top', 'XColor',[0.8 0.8 0.8]);
    set(ax(1),'Ylim',[(bins_matrix(1,1)-3) (bins_matrix(end,1)+3)],...
        'Xlim', [0 max(bins_matrix(:,2))*7])
    
    % Set properties for price plot
    ax(2)=axes('Position',get(ax(1),'Position'),...
   'YAxisLocation','right',...
   'Ylim',[(bins_matrix(1,1)-3) (bins_matrix(end,1)+3)],...
   'Color','none',...
   'XColor','k','YColor','k');
    
    % Add price plot 
    h = [h; line(date_vect, close_day(:,1), 'Parent', ax(2))];
    title(['Price/Volume'])
    
    % Make a date x-axis
    xdata = [date_vect(1); date_vect(end)];
    set(gca,'Xlim', xdata);
    datetick('x','mm', 'keeplimits')
    % set grid
    grid on
    set(gca,'YMinorGrid','on')
    
    print(h)
    print(pv)
    
    %print ('-dpdf','pv_graph');
end

Contact us