Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (TipmyPip, AndrewAMD), 911 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
peak() is baffled by flat-topped peaks. also affects crossover/u #444443
08/11/14 07:16
08/11/14 07:16
Joined: Sep 2013
Posts: 504
California
G
GPEngine Offline OP
User
GPEngine  Offline OP
User
G

Joined: Sep 2013
Posts: 504
California
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.
Re: peaks() is baffled by flat-topped peaks [Re: GPEngine] #444444
08/11/14 07:20
08/11/14 07:20
Joined: Sep 2013
Posts: 504
California
G
GPEngine Offline OP
User
GPEngine  Offline OP
User
G

Joined: Sep 2013
Posts: 504
California
This behavior can be mitigated with the following hack. The cost of the hack is that it introduces non-determinism.
Code:
vars Highs = series(priceHigh() + random() * 0.000001);
  vars Lows = series(priceLow() + random() * 0.000001);


Last edited by GPEngine; 08/11/14 07:20.
Re: peaks() is baffled by flat-topped peaks [Re: GPEngine] #444508
08/13/14 03:26
08/13/14 03:26
Joined: Sep 2013
Posts: 504
California
G
GPEngine Offline OP
User
GPEngine  Offline OP
User
G

Joined: Sep 2013
Posts: 504
California
Your implementation of crossOver/Under is also naive. In the following example, the Blue line has clearly crossed over the Red line at bar 67, where Blue=2 and Red=1. But crossOver(Blue, Red) is consistently zero throughout.
Code:
function run() {
  set(PLOTNOW);
  MaxBars = 250;
  asset("");
  PlotHeight1 = 0;
  PlotHeight2 = 256;

  vars Red = series(2 - (int)(genSine(120, 120) * 3));
  plot("Red", Red, LINE+NEW, RED);
  vars Blue = series((int)(genSine(120, 120) * 3))+3;
  plot("Blue", Blue, LINE, BLUE);

  plot("Cross", crossOver(Blue, Red), LINE+NEW, RED);  // always 0.
  printf("\n%d %f %f", Bar, Red[0], Blue[0]);
}


Remove the (int) conversions in Red and Blue's definition, making the crossovers is instant, 1 bar wide, and crossOver works correctly.

See attachments.

Attached Files
crossover_edge_case.png (103 downloads)
Last edited by GPEngine; 08/13/14 03:38.
Re: peaks() is baffled by flat-topped peaks [Re: GPEngine] #444510
08/13/14 05:21
08/13/14 05:21
Joined: Sep 2013
Posts: 504
California
G
GPEngine Offline OP
User
GPEngine  Offline OP
User
G

Joined: Sep 2013
Posts: 504
California
In my attachments I had the red and blue lines swapped, which could be confusing. But the basic idea holds. Crossover fails to detect report true when a line is below, then equal to, then above, another line even though it is clear from the graph that the lines have crossed.

Attached Files
crossover_edge_case.png (98 downloads)
Re: peaks() is baffled by flat-topped peaks [Re: GPEngine] #444519
08/13/14 11:31
08/13/14 11:31
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
Yes, a flat line is not supposed to produce a signal. I'll add this to the remarks. BTW you can find all peak and crossover algorithms in the manual - no experiments necessary.

Re: peaks() is baffled by flat-topped peaks [Re: jcl] #444528
08/13/14 15:00
08/13/14 15:00
Joined: Sep 2013
Posts: 504
California
G
GPEngine Offline OP
User
GPEngine  Offline OP
User
G

Joined: Sep 2013
Posts: 504
California
Thanks.
I realize that flat lines are not supposed to produce a signal. Perhaps I should not have used the word "flat" in my description. Since what I really mean is a series which rises, stays at the same level for 2 bars, then falls. A casual observer would look at the local minimum shown in peak_bug_real_GBPJPY.png and say, "that's a peak." peak() misses it.

Likewise in the chart above Red and Blue have trades places and have clearly "crossed over" yet crossOver is unaware.

Thanks for pointing out algorithms in the manual. Silly me.

Re: peaks() is baffled by flat-topped peaks [Re: GPEngine] #444531
08/13/14 15:24
08/13/14 15:24
Joined: Jun 2013
Posts: 1,609
D
DdlV Offline
Serious User
DdlV  Offline
Serious User
D

Joined: Jun 2013
Posts: 1,609
Hi jcl. I seem to recall this has come up before, and I don't remember seeing the "why"? For example, a flat peak is a kind of "broad peak" (to steal verbiage from optimize laugh ) - why isn't that still a peak? Similarly for valley and the cross's. Is there some statistical or other reason why not?

Thanks.

Re: peaks() is baffled by flat-topped peaks [Re: DdlV] #444536
08/13/14 16:38
08/13/14 16:38
Joined: Sep 2013
Posts: 504
California
G
GPEngine Offline OP
User
GPEngine  Offline OP
User
G

Joined: Sep 2013
Posts: 504
California
The crossOver/Under bug is likely to come up when you're dealing with discrete variables. For example, a newcomer might expect the following to work:
Code:
function run() {
  StartDate = 20140715;
  EndDate = 20140716;
  BarPeriod = 15;
  asset("EUR/USD");
  // This won't do what you want. Don't do this!
  if (crossOver(series(hour()), 12.0)) {
    printf("\n%d Hour 13 struck!", Bar);
  }
}


Someone unfamiliar with this bug might expect that this strategy would print a line (or perform some action) once per day upon encountering the first bar in hour 13. But, it doesn't. The condition is never true because hour() is discrete. It spends some time (4 bars) at 12 before advancing to 13.

There are other discrete built-ins. NumRiseFall, MinMaxIndex, etc.

Last edited by GPEngine; 08/13/14 16:38.
Re: peaks() is baffled by flat-topped peaks [Re: GPEngine] #444562
08/14/14 08:43
08/14/14 08:43
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
I think the "why" is because this is simply an often needed function - triggering a signal on a peak or crossover of a particular bar. If you want to also trigger signals on the begin or end of a flat line or plateau, just use a similar function with >= instead of >.

Re: peaks() is baffled by flat-topped peaks [Re: jcl] #444580
08/14/14 12:22
08/14/14 12:22
Joined: Jun 2013
Posts: 1,609
D
DdlV Offline
Serious User
DdlV  Offline
Serious User
D

Joined: Jun 2013
Posts: 1,609
Thanks.

Page 1 of 2 1 2

Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1