Mac-Cherony
Mac-Cherony

Reputation: 133

Candlestick-plot in Matlab and Octave

I want to write a script for a candlestick-plot that I can run in Octave as well as in Matlab. So far I use the following code:

D=[ '15-Jul-2013'
'16-Jul-2013'
'17-Jul-2013'
'18-Jul-2013'
'19-Jul-2013'
'22-Jul-2013'
'23-Jul-2013'
'24-Jul-2013'];
O=[25.93 26.39 26.37 26.75 25.82 25.99 26.10 26.32];
H=[26.43 26.75 26.78 26.77 26.11 26.13 26.30 26.53];
L=[25.60 26.01 26.30 26.12 25.60 25.72 25.97 26.05];
C=[26.28 26.32 26.65 26.18 25.88 26.05 26.13 26.51];

datapoints=length(C);
hold on;
for i=1:datapoints
    plot([i i],[L(i) H(i)],'linewidth',2,'Color','k');
    if C(i)>O(i)
        plot([i i],[O(i) C(i)],'linewidth',5,'color','r');
    else
        plot([i i],[O(i) C(i)],'linewidth',5,'color','g');
    end
end

hold off;
grid on;

xlim([0 datapoints+1]);
y=get(gca,'ylim');
ymin=int16(y(1)-0.5);
ymax=int16(y(2)+0.5);
ylim([ymin ymax]);

XTick=zeros(1,length(datapoints));
j=1;
for i=1:1:datapoints
    XTick(j)=i;
    j=j+1;
end


set(gca,'XTick',XTick,'XTickLabel','')    
pos = get(gca,'Position');
set(gca,'Position',[pos(1), .15, pos(3) .75])

for i=1:length(XTick)
    hText = text(XTick(i), double(ymin), D(XTick(i),:));
    set(hText,'Rotation',45,'HorizontalAlignment','right','VerticalAlignment','top');
end 

The resulting plot looks quite nice in Matlab but looks very terrible in Octave. How can I make the plot looks nice in both programms?

Upvotes: 1

Views: 2695

Answers (2)

Mac-Cherony
Mac-Cherony

Reputation: 133

The solution here was to use the fill function instead of plot.

The following code leads to similar results in octave and matlab independent of the plotting interface:

D=[ '15-Jul-2013'
'16-Jul-2013'
'17-Jul-2013'
'18-Jul-2013'
'19-Jul-2013'
'22-Jul-2013'
'23-Jul-2013'
'24-Jul-2013'];
O=[25.93 26.39 26.37 26.75 25.82 25.99 26.10 26.32];
H=[26.43 26.75 26.78 26.77 26.11 26.13 26.30 26.53];
L=[25.60 26.01 26.30 26.12 25.60 25.72 25.97 26.05];
C=[26.28 26.32 26.65 26.18 25.88 26.05 26.13 26.51];

     colorDown = 'g'; 
     colorUp = 'r'; 
     colorLine = 'k';
     date = (1:length(O))';

% w = Width of body, change multiplier to draw body thicker or thinner
% the 'min' ensures no errors on weekends ('time gap Fri. Mon.' > wanted
% spacing)
w=.1*min([(date(2)-date(1)) (date(3)-date(2))]);
%%%%%%%%%%%Find up and down days%%%%%%%%%%%%%%%%%%%
d=C-O;
l=length(d);
hold on
%%%%%%%%draw line from Low to High%%%%%%%%%%%%%%%%%
for i=1:l
   line([date(i) date(i)],[L(i) H(i)],'Color',colorLine, 'linewidth',2)
end
%%%%%%%%%%draw white (or user defined) body (down day)%%%%%%%%%%%%%%%%%
n=find(d<0);
for i=1:length(n)
    x=[date(n(i))-w date(n(i))-w date(n(i))+w date(n(i))+w date(n(i))-w];
    y=[O(n(i)) C(n(i)) C(n(i)) O(n(i)) O(n(i))];
    fill(x,y,colorDown)
end
%%%%%%%%%%draw black (or user defined) body(up day)%%%%%%%%%%%%%%%%%%%
n=find(d>=0);
for i=1:length(n)
    x=[date(n(i))-w date(n(i))-w date(n(i))+w date(n(i))+w date(n(i))-w];
    y=[O(n(i)) C(n(i)) C(n(i)) O(n(i)) O(n(i))];
    fill(x,y,colorUp)
end

    datapoints=length(C);
    xlim([0 datapoints+1]);
    y=get(gca,'ylim');
    ymin=int16(y(1)-0.5);
    ymax=int16(y(2)+0.5);
    ylim([ymin ymax]);

    XTick=zeros(1,length(datapoints));
    j=1;
    for i=1:2:datapoints
        XTick(j)=i;
        j=j+1;
    end

        set(gca,'XTick',XTick,'XTickLabel','')    
    pos = get(gca,'Position');
    set(gca,'Position',[pos(1), .15, pos(3) .75])

    for i=1:length(XTick)
        hText = text(XTick(i), double(ymin), D(XTick(i),:));
        set(hText,'Rotation',45,'HorizontalAlignment','right','VerticalAlignment','top');
    end

    hold off

Upvotes: 2

am304
am304

Reputation: 13876

Here's what I get with Octave 3.6.2 (I have used screen captures to illustrate what it actually looks like on the screen):

  • Graphics toolkit qt

enter image description here

  • Graphics toolkit gnuplot

enter image description here

  • Graphics toolkit fltk

enter image description here

The bodies of the candles are vertical in all 3 cases.

Upvotes: 1

Related Questions