I discovered this behavior while trying to use peak() to identify local maxima of priceHigh()'s for range estimation. peak() is not capable of detecting flat-topped, square-shaped peaks consisting of 2 (or more) identical values. In the cases where a peak consists of two repeated values, peak() reports 0 (no peak) for both the first and second value. In other words it totally misses the presence of the peak. Proof
Code:
function run() {
  set(PLOTNOW);
  MaxBars = 250;
  asset("");
  PlotHeight1 = 0;
  PlotHeight2 = 256;

  vars Sine = series(genSine(4, 4) * 2);
  plot("Sine", Sine, LINE+NEW, BLUE);
  plot("Sinepeaks", peak(Sine), LINE+NEW, RED);

  vars Square = series(ceil(genSine(4, 4) * 2));
  plot("Square", Square, LINE+NEW, BLUE);
  plot("Squarepeaks", peak(Square), LINE+NEW, RED);  // always 0
}

And of course this kind of peak can happen in real price data. For example if you are trying to detect local maxima of Highs. The 60m+0 sampling of "GBP/JPY" on 20140715 has two highs with the same value. A peak-detector using peak() misses the peak at Bar 243 because Bar 244 has the same value (174.410004). See chart
Code:
function run() {
  StartDate = 20140715;
  EndDate = 20140716;
  BarPeriod = 60;
  asset("GBP/JPY");
  set(PLOTNOW);
  vars Highs = series(priceHigh());
  plot("Highs", Highs, LINE, BLUE);
  plot("Highspeaks", peak(Highs), LINE+NEW, RED);
  printf("\nBar %d %f", Bar, Highs[0]);
}


This can lead to missed signals.

Attached Files
peak_bug_GBPJPY.png (11 downloads)
Last edited by GPEngine; 08/13/14 03:18.