t_in = tsecs(pidx);
p_in = price(pidx);

q_in = qsecs(qidx);
b_in = bid(qidx);
a_in = ask(qidx);

[q_in ind] = unique(q_in);
b_in = b_in(ind);
a_in = a_in(ind);

plot(t_in,p_in); hold on;
plot(q_in,b_in,'r');

t = [];
p = [];

for i = 1:length(t_in)
    % Current value
    t0 = t_in(i);
    p0 = p_in(i);
    
    % Interpolate bid and ask
    bi = interp1(q_in,b_in,t_in(i));
    ai = interp1(q_in,a_in,t_in(i));
    
    if((bi<=p0)&&(p0<=ai)) % Definition of good trade
        t = [t; t0];
        p = [p; p0];
    end
end

grid on;
set(gca,'FontSize',16);
plot(t,p,'Linewidth',2);
xlabel('Time (s)');
ylabel('Price');
title('Stock #679');