clear;
clc;

buf = [3691.55,3693.62,3690.58,3690.09,3698.64,3705.14,3698.34,3696.46,3696.55,3699.17,3691.19,3688.62,3691.87,3691.9,3692.22,3691.55,3688.42,3688.63,3689.51,3691.55,3696.45,3696.62,3695,3699.45,3699.25,3698.23,3695.96,3696.86,3697.71,3695.43,3695.21,3696.63,3697.15,3700.98,3703.99,3705.79,3706.47,3706.61,3708.69,3710.06,3713.6,3714.78,3717.36,3714.52,3715.96,3716.47,3723.32,3725.8,3722.93,3722.65,3722.72,3721.42,3721.44,3722.24,3724.22,3723.89,3724.27,3723.63,3723.04,3720.26,3717.1,3718.88,3714.89,3714.95,3715.42,3717.34,3719.23,3722.67,3721.21,3725.15,3724,3724.44,3726.69,3727.6,3728.91,3735.26,3740.07,3741.29,3743.85,3746.77,3742.77,3738.93,3740.11,3739.19,3735.78,3737.09,3735.72,3735.8,3737.77,3738.63,3734.23,3739.9,3739.38,3738.65,3740.16,3742.76,3739.74,3737.48,3736.71,3736.02,3733,3733.62,3733.48,3733.82,3733.38,3729.7,3728.39,3728.8,3727.19,3726.71,3731.67,3726.51,3725.45,3726.39,3724.43,3725.09,3723.41,3724.85,3724.89,3728.01,3726.63,3727.75,3729.27,3730.18,3728.37,3729.2,3729.77,3730.21,3725.46,3725.77,3725.59,3726.97,3727.28,3726.49,3733.81,3734.06,3735.59,3734.41,3735.78,3735.56,3733.8,3730.91,3731.71,3730.51,3731.84,3732.02,3732.01,3732.98,3729.16,3728.89,3728.41,3727.3,3726.39,3726.39,3726.53,3723.22,3723.04,3723.13,3726.09,3726.59,3727.02,3728.24,3729.12,3728.44,3729.44,3730.59,3731.19,3730.09,3730.7,3728.43,3732.02,3732.35,3732.95,3732.21,3732.95,3734.75,3727.73,3728.71,3728.32,3728.31,3727.34,3725.74,3722.04,3725.82,3726.01,3726.47,3727,3725.03,3727.27,3728.25,3727.26,3727.69,3727.11,3729.14,3730.31,3734.3,3735.34,3735.21,3735.39,3731.67,3731.65,3731.13,3728.33,3726.96,3728.47,3728.16,3728.43,3729.68,3729,3730.11,3729.46,3727.32,3730.69,3730.95,3729.76,3731.07,3730.41,3729.56,3729.96,3730.67,3731.61,3730.48,3732,3731.32,3731.39,3730.89,3729.62,3730.01,3727.11,3726.6,3725.99,3724.98,3727.69,3728.42,3727.26,3726.89,3724.5,3724.91,3723.96,3725.98,3725.81,3725.28,3724.91,3726.39,3727.26,3725.05,3725.71,3724.3,3725.19,3724.94,3724.5,3720.01,3719.64,3713.05,3714.1,3717.31,3715.24,3715.12,3714.39,3717.48,3716.82,3718.49,3713.54,3714.65,3717.4,3717.96,3715.55,3716.82,3716.2,3715.6,3714.52,3713.56,3713.58,3713.36,3710.61,3711.14,3712.4,3713.66,3714.55,3716.71,3716.71,3716.51,3717.25,3715.04,3718.35,3716.71,3718.94,3718.25,3721.53,3720.86,3721.15,3720.14,3718.12,3716.94,3714.59,3712.59,3709.08,3709.39,3708.21,3711.55];

%loop through and compute the moving average
fast_i = 1;
fast_val = 0;
fast_buf = zeros(40,1);
fast_moving = [];

start = false;

for j = 1:300,
  fast_val -= fast_buf(fast_i);
  fast_val += buf(j);
  fast_buf(fast_i++) = buf(j);
  if fast_i > 40
    fast_i = 1;
    start = true;
  endif

  if start
    fast_moving(j) = fast_val/40;
  else
    fast_moving(j) = 0;
  endif
endfor

%test for crossover
for j = 200:299,
  %do 2 points at a time
  a = fast_moving(j+1);
  b = fast_moving(j);
  c = a-b;
  disp(a);
  disp(b);
  disp(c);
  disp("but...3729.6 - 3729.4 = 0.2...not 0.1158");
  break;
endfor
