I received this in an email and thought it would be useful as a forum topic:
Chris
I have been doing some work with MD_ Trough and MD _ Peak listed under System Scans. I have no idea how it works. Could you give me an idea of how they are constructed or how the scan works? Peak and Trough are self explanatory but would like some understanding of how they do it.
Thanks
Jim
How do MD_Peak and MD_Trough work?
Moderator: Chris White
-
- Posts: 212
- Joined: Mon Nov 29, 2010 9:21 pm
Re: How do MD_Peak and MD_Trough work?
Hi Jim,
The script for MD_Peak is as follows:
As you can see the code uses the ZIG formula (zigzag) to figure out peaks. Basically looking for where the zigzag line two days ago was lower than one day ago and the zigzag line one day ago was higher than today (definition of a peak).
The one parameter you can specify is ZPCT (Zig %) which controls the granularity of the ZIGZAG line. Bigger numbers require a bigger change in stock price before a turn is identified.
I added MD_PEAK(10) and MD_TROUGH(10) to a chart that already has MD_ZIGZAG(10) overlayed on top of the stock candles:
You can see that the peak and trough scripts are correctly picking out the peaks and troughs of the zigzag line.
However, please be aware that any script that uses zigzag does not make a valid backtesting script because as a chart develops day by day the previous peak or trough may be re-drawn depending on close price in relation to those peaks and troughs. You will find that backtesting zigzag produces amazing results because after a chart is fully drawn it's easy to see where the peaks and troughs were. It is not so easy to see where the next peak or trough will finally be placed though. Zigzag is very useful for picking out potential turning points and for measuring double/triple tops and bottoms.
The script for MD_Peak is as follows:
Code: Select all
ZZ:= ZIG(C, ZPCT);
turn1:= REF(ZZ, 2) < REF(ZZ, 1) & ( REF(ZZ, 1) > ZZ );
event: REF(turn1, -1);
The one parameter you can specify is ZPCT (Zig %) which controls the granularity of the ZIGZAG line. Bigger numbers require a bigger change in stock price before a turn is identified.
I added MD_PEAK(10) and MD_TROUGH(10) to a chart that already has MD_ZIGZAG(10) overlayed on top of the stock candles:
You can see that the peak and trough scripts are correctly picking out the peaks and troughs of the zigzag line.
However, please be aware that any script that uses zigzag does not make a valid backtesting script because as a chart develops day by day the previous peak or trough may be re-drawn depending on close price in relation to those peaks and troughs. You will find that backtesting zigzag produces amazing results because after a chart is fully drawn it's easy to see where the peaks and troughs were. It is not so easy to see where the next peak or trough will finally be placed though. Zigzag is very useful for picking out potential turning points and for measuring double/triple tops and bottoms.
Re: How do MD_Peak and MD_Trough work?
Here is another version which plots 3 different size peaks and troughs
Under the parameter tab
ZPct1 double 3 2 100
ZPct2 Double 6 3 100
ZPct3 Double 10 6 100
This formula is for peaks
ZZ1:= ZIG(C, ZPCT1);
ZZ2:= ZIG(C, ZPCT2);
ZZ3:= ZIG(C, ZPCT3);
turn1:= REF(ZZ1, 2) < REF(ZZ1, 1) & ( REF(ZZ1, 1) > ZZ1 );
turn2:= REF(ZZ2, 2) < REF(ZZ2, 1) & ( REF(ZZ2, 1) > ZZ2 );
turn3:= REF(ZZ3, 2) < REF(ZZ3, 1) & ( REF(ZZ3, 1) > ZZ3 );
event: If(REF(turn3, -1),3,If(Ref(turn2,-1),2,If(Ref(Turn1,-1),1,0)));
This formula is for troughs
ZZ1:= ZIG(C, ZPCT1);
ZZ2:= ZIG(C, ZPCT2);
ZZ3:= ZIG(C, ZPCT3);
turn1:= REF(ZZ1, 2) >REF(ZZ1, 1) & ( REF(ZZ1, 1)< ZZ1 );
turn2:= REF(ZZ2, 2) > REF(ZZ2, 1) & ( REF(ZZ2, 1) < ZZ2 );
turn3:= REF(ZZ3, 2) > REF(ZZ3, 1) & ( REF(ZZ3, 1) < ZZ3 );
event: If(REF(turn3, -1),3,If(Ref(turn2,-1),2,If(Ref(Turn1,-1),1,0)));
This formula is the ZIGZAG
Z1:ZIG(C, ZPCT1),color#80000000;
Z2:ZIG(C, ZPCT2),color#80000000;
Z3:ZIG(C, ZPCT3),color#80000000;
Under the parameter tab
ZPct1 double 3 2 100
ZPct2 Double 6 3 100
ZPct3 Double 10 6 100
This formula is for peaks
ZZ1:= ZIG(C, ZPCT1);
ZZ2:= ZIG(C, ZPCT2);
ZZ3:= ZIG(C, ZPCT3);
turn1:= REF(ZZ1, 2) < REF(ZZ1, 1) & ( REF(ZZ1, 1) > ZZ1 );
turn2:= REF(ZZ2, 2) < REF(ZZ2, 1) & ( REF(ZZ2, 1) > ZZ2 );
turn3:= REF(ZZ3, 2) < REF(ZZ3, 1) & ( REF(ZZ3, 1) > ZZ3 );
event: If(REF(turn3, -1),3,If(Ref(turn2,-1),2,If(Ref(Turn1,-1),1,0)));
This formula is for troughs
ZZ1:= ZIG(C, ZPCT1);
ZZ2:= ZIG(C, ZPCT2);
ZZ3:= ZIG(C, ZPCT3);
turn1:= REF(ZZ1, 2) >REF(ZZ1, 1) & ( REF(ZZ1, 1)< ZZ1 );
turn2:= REF(ZZ2, 2) > REF(ZZ2, 1) & ( REF(ZZ2, 1) < ZZ2 );
turn3:= REF(ZZ3, 2) > REF(ZZ3, 1) & ( REF(ZZ3, 1) < ZZ3 );
event: If(REF(turn3, -1),3,If(Ref(turn2,-1),2,If(Ref(Turn1,-1),1,0)));
This formula is the ZIGZAG
Z1:ZIG(C, ZPCT1),color#80000000;
Z2:ZIG(C, ZPCT2),color#80000000;
Z3:ZIG(C, ZPCT3),color#80000000;