clear all;

load('price.mat');

plot(price); grid on;
xlabel('bar nr. (30 sec)'); ylabel('$');

%% let's test a couple of standard filters

n = 25; % filter window length

% ema calculation
alpha = 0.8;
Bema = (alpha*ones(1,n)).^(1:n); % create weights
Bema = Bema/sum(Bema);                 % normalize
ema = filter(Bema,1,price);
ema(1:n) = nan;

%wma calculation
Bwma = n:-1:1;
Bwma = Bwma/sum(Bwma);
wma = filter(Bwma,A,price); wma(1:n)=nan;

%sma
Bsma = ones(1,n);
Bsma = Bsma/sum(Bsma);
sma = filter(Bsma,1,price); sma(1:n)=nan;

%predictive filter
Bpred = [2 -1];
pred = filter(Bpred,1,price); pred(1:n)=nan;


% hibrid filter
f = 0.3;
hybrid = f*pred+(1-f)*ema;

clf;
subplot(2,1,1);
hold on;
plot(price,'ko');
plot([ema,wma,sma,pred,hybrid],'linewidth',2);
title('price');
legend({'price','ema','wma','sma','predictive','hybrid'});

subplot(2,1,2);
plot([Bema;Bwma;Bsma]','x-');
title('Weights');
legend({'ema','wma','sma'});

%% frequency responses
figure(2);
Bpred = [2 -1];
freqz(Bpred,1);

