How to take the inflection points on ZIG-ZAG indicator  [SOLVED]

Questions about MultiCharts and user contributed studies.
lois77
Posts: 23
Joined: 05 Aug 2012
Has thanked: 7 times

How to take the inflection points on ZIG-ZAG indicator

Postby lois77 » 26 Aug 2012

Hi,

I would like some help from MS support, if possible, on how to take the inflection points of ZIG-ZAG indicator.
I need to code the last 3 inflection points in order to detect higher highs, lower highs, lower lows and higher lows.
I can't really figure out how to do it. It seems more complex than I thought.

Kind regards.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: How to take the inflection points on ZIG-ZAG indicator

Postby Henry MultiСharts » 28 Aug 2012

Hello lois77,

Coordinates for the last inflection point are kept in variables var2, var3, var1 in the following condition in the study code:

Code: Select all

if var6 then begin
var8 = TL_New( var2, var3, var1, var2[1], var3[1],var1[1] ) ;
var1 = Price
var2 = Date
var3 = Time

lois77
Posts: 23
Joined: 05 Aug 2012
Has thanked: 7 times

Re: How to take the inflection points on ZIG-ZAG indicator  [SOLVED]

Postby lois77 » 28 Aug 2012

Hi Henry,

Many thanks for the explanation.
I guess I need to code an array and store all the inflection points in order to reference the last three whenever I need them.

Again, thank you very much.

Kind regards.

lois77
Posts: 23
Joined: 05 Aug 2012
Has thanked: 7 times

Re: How to take the inflection points on ZIG-ZAG indicator

Postby lois77 » 02 Sep 2012

Hi Henry.

After trying everything, I really couldn't make it.
I don't know how to take only the inflection points. The best I could do was to replicate the zigzag trend lines in an indicator but with intermediate points in some cases between the inflection points. In these cases, it doesn't work correctly.

Kind regards.

lois77
Posts: 23
Joined: 05 Aug 2012
Has thanked: 7 times

Re: How to take the inflection points on ZIG-ZAG indicator

Postby lois77 » 03 Sep 2012

Hi again.

I made some progression.
Now I replicated the zigzag trend lines in an indicator only after the occurrence of the turning points, but I don't know why this code doesn't work when I try to use it as a function (without the plot code), and that is how I need it to work in order to implement a strategy on automated trading.
I picked the zigzag and modified it as following:

Code: Select all

inputs:
Price( Close ),
RetracePnts( 34 ),
LineColor( Yellow ),
LineWidth( 1 ) ;

variables:
var0( 0 ),
var1( Price ),
var2( Date ),
var3( Time ),
var4( 0 ),
var5( false ),
var6( false ),
var7( false ),
var8( 0 ) ;



var0 = SwingHigh( 1, Price, 1, 2 ) ;
if var0 <> -1 then
begin
condition1 = var4 <= 0 and var0 >= var1 + RetracePnts ;
if condition1 then
begin
var5 = true ;
var6 = true ;
var4 = 1 ;
end
else
begin
condition1 = var4 = 1 and var0 >= var1 ;
if condition1 then
begin
var5 = true ;
var7 = true ;
end;
end ;
end
else
begin
var0 = SwingLow( 1, Price, 1, 2 ) ;
if var0 <> -1 then
begin
condition1 = var4 >= 0 and var0 <= var1 - RetracePnts ;
if condition1 then
begin
var5 = true ;
var6 = true ;
var4 = -1 ;
end
else
begin
condition1 = var4 = -1 and var0 <= var1 ;
if condition1 then
begin
var5 = true;
var7 = true ;
end ;
end;
end ;
end ;

if var5 then

begin
var1 = var0 ;
var2 = Date[1] ;
var3 = Time[1] ;
var5 = false ;
end ;

if var6 then

begin
{var8 = TL_New( var2, var3, var1, var2[1], var3[1],
var1[1] ) ;}
value1 = Close[Findbar(var2[1], var3[1])] ;
plot1[Findbar(var2[1], var3[1])](value1) ;
{TL_SetExtLeft( var8, false ) ;
TL_SetExtRight( var8, false ) ;}
TL_SetSize( var8, LineWidth ) ;
TL_SetColor( var8, LineColor ) ;
var6 = false ;
{end
else if var7 then

begin
TL_SetEnd( var8, var2, var3, var1 ) ;
var7 = false ;}
end ;
I posted an image to see the replication of the zigzag in an indicator.
Some help would be nice on how to code this in a useful function.

Kind regards.
Attachments
MultiCharts1.png
Zigzag replication
(48.7 KiB) Downloaded 2318 times

SP
Posts: 465
Joined: 06 Feb 2006
Has thanked: 36 times
Been thanked: 286 times

Re: How to take the inflection points on ZIG-ZAG indicator

Postby SP » 03 Sep 2012

Function for ZigZag, you only need to change the RetracePct to RetracePoints for your needs.

_ZigZagV1

Code: Select all

{
Function: _ZigZagV1

Customized based on the the built-in ZigZag % indicator.

Returns the price of the most recent ZigZag high or low.

Also plots ( using trend lines ) the ZigZag itself ( which would match the
ZigZag % indicator ) as well as the most recent ZigZag price.

You should refer to the built-in ZigZag % indicator to understand how to
set most of the inputs. Newly added inputs are documented below.

}

inputs:
Price( Numeric ),
RetracePct( Numeric ),
ZigZagLineColor( Numeric ), // Color of ZigZag indicator
ZigZagPriceColor( Numeric ), // Color of most recent high/low ZigZag price returned by this function
LineWidth( Numeric ),
oReversingBar( NumericRef ), // The bar at which the most recent ZigZag high/low occurred
PlotTrendLine( TrueFalseSimple ), // Plot the ZigZag indicator using trend lines
PlotZigZagPrice( TrueFalseSimple ) ;// Plot the ZigZag price using trend lines

variables:
NewSwingPrice( 0 ),
SwingPrice( Price ),
SwingDate( Date ),
SwingTime( Time ),
SwingBar( 0 ),
TLDir( 0 ),
RetraceFctrUp( 1 + RetracePct * .01 ),
RetraceFctrDn( 1 - RetracePct * .01 ),
SaveSwing( false ),
AddTL( false ),
UpdateTL( false ),
TLRef( 0 ),
HoldZigZag( 0 ),
ZigZagTLID( 0 ) ;


// Most of this logic and code is identical to the built-in ZigZag % indicator.
// Comments show where changes have been made.
NewSwingPrice = SwingHigh( 1, Price, 1, 2 ) ;
if NewSwingPrice <> -1 then
begin
if TLDir <= 0 and NewSwingPrice >= SwingPrice * RetraceFctrUp then
begin
SaveSwing = true ;
AddTL = true ;
TLDir = 1 ;
end
else if TLDir = 1 and NewSwingPrice >= SwingPrice then
begin
SaveSwing = true ;
UpdateTL = true ;
end ;
end
else
begin
NewSwingPrice = SwingLow( 1, Price, 1, 2 ) ;
if NewSwingPrice <> -1 then
begin
if TLDir >= 0 and NewSwingPrice <= SwingPrice * RetraceFctrDn then
begin
SaveSwing = true ;
AddTL = true ;
TLDir = -1 ;
end
else if TLDir = -1 and NewSwingPrice <= SwingPrice then
begin
SaveSwing = true;
UpdateTL = true ;
end ;
end ;
end ;

if SaveSwing then
begin
SwingPrice = NewSwingPrice ;
SwingDate = Date[1] ;
SwingTime = Time[1] ;

// Add SwingBar to capture the bar the swing occurred on
SwingBar = CurrentBar - 1 ;

SaveSwing = false ;
end ;

if AddTL then
begin

// Customized to allow plotting of ZigZag indicator to be turned off
if PlotTrendLine then
TLRef = TL_New( SwingDate, SwingTime, SwingPrice, SwingDate[1],
SwingTime[1], SwingPrice[1] ) ;

// Return the historical bar which the ZigZag reversed on
oReversingBar = SwingBar[1] ;

// Capture and hold the most recent ZigZag price
HoldZigZag = SwingPrice[1] ;

// Customized to allow plotting of ZigZag indicator to be turned off
if PlotTrendLine then
begin

TL_SetExtLeft( TLRef, false ) ;
TL_SetExtRight( TLRef, false ) ;
TL_SetSize( TLRef, LineWidth ) ;
TL_SetColor( TLRef, ZigZagLineColor ) ;

end ;

AddTL = false ;

end

else if UpdateTL then
begin

// Customized to allow plotting of ZigZag indicator to be turned off
if PlotTrendLine then
TL_SetEnd( TLRef, SwingDate, SwingTime, SwingPrice ) ;

UpdateTL = false ;

end ;

// Added this logic to the built-in ZigZag % indicator to return the
// current ZigZag high/low price ( this can lag the current bar by a wide margin )
// and to plot the ZigZag price.

// Return the most recent ZigZag high or low price
_ZigZagV1 = HoldZigZag ;

// Plot the ZigZag price if we have at least 2 ZigZag price values
if PlotZigZagPrice and HoldZigZag[1] > 0 then
begin

ZigZagTLID = TL_New( Date[1], Time[1], HoldZigZag[1], Date, Time, HoldZigZag ) ;

Value2 = TL_SetColor( ZigZagTLID, ZigZagPriceColor ) ;
Value2 = TL_SetSize( ZigZagTLID, LineWidth ) ;

end ;

Example how to use it in a strategy. This strategy enters long when the price touches the most recent high ZigZag price, and goes short when the price touches the most recent low ZigZag price.

Code: Select all

inputs:

Price( Close ),
RetracePct( 5 ),
ZigZagLineColor( Yellow ),
ZigZagPriceColor( Magenta ),
LineWidth( 1 ),
PlotZigZag( true ),

PlotZigZagPrice( true ),

InvertEntries( false ), // Invert the entries from going long to short and vice-versa

PrintDebug( False ) ; // issue print statements to log file for debug

variables:
oZigZagBarOffset( 0 ),
ZigZagPrice( 0 ),
OldZigZagPrice( 0 ),
HighLow( 0 ),
MP( 0 ) ;

// Get the current market position and ZigZag price
MP = MarketPosition ;
ZigZagPrice = _ZigZagV1( Price, RetracePct, ZigZagLineColor, ZigZagPriceColor, LineWidth, oZigZagBarOffset, PlotZigZag, PlotZigZagPrice ) ;

// If the ZigZag price has changed since the last bar, capture the prior bar value,
// update whether the current ZigZag price is a high or low versus the prior ZigZag price,
// and exit an open positions.
If ZigZagPrice <> ZigZagPrice[1] then
begin

OldZigZagPrice = ZigZagPrice[1] ;

// Determine if the latest ZigZag is a high or low
if ZigZagPrice > OldZigZagPrice then
HighLow = 1
else
HighLow = -1 ;

// If we are in a position then exit it.
// Strategy Automation allows us to place exit orders
// for both a long and short and will only issue the
// appropriate exit order.
if MP <> 0 then
begin

Sell at next bar at market ;
BuyToCover at next bar at market ;

end ;

end ;

// If we have at least 2 ZigZag prices defined then ...
if HighLow <> 0 and OldZigZagPrice[1] > 0 then
begin

// If the most recent ZigZag price is a high
// then Buy when the price crosses above it
// and SellShort when the price crosses below the
// prior ZigZag, which would be a low
if HighLow > 0 then
begin

if InvertEntries = false then
begin

Buy at next bar at ZigZagPrice Stop ;
SellShort at next bar at OldZigZagPrice Stop ;

end

// Invert the entries: Switch long to short and stop to limit orders
else
begin

SellShort at next bar at ZigZagPrice Limit ;
buy at next bar at OldZigZagPrice Limit ;

end ;

end

// If the most recent ZigZag price is a low
// then SellShort when the price crosses below it
// and buy when the price crosses above the
// prior ZigZag, which would be a high

else
begin

If InvertEntries = false then
begin

SellShort at next bar at ZigZagPrice Stop ;
Buy at next bar at OldZigZagPrice Stop ;

end
else
begin

Buy at next bar at ZigZagPrice Limit ;
SellShort at next bar at OldZigZagPrice Limit ;

end ;

end ;

end ;

lois77
Posts: 23
Joined: 05 Aug 2012
Has thanked: 7 times

Re: How to take the inflection points on ZIG-ZAG indicator

Postby lois77 » 03 Sep 2012

Hi.

SP, thank you very much.
I would like to ask what should be the initial value for "oReversingBar".
I think it is "SwingPrice[1]", but I get error when compiling.

Kind regards.

SP
Posts: 465
Joined: 06 Feb 2006
Has thanked: 36 times
Been thanked: 286 times

Re: How to take the inflection points on ZIG-ZAG indicator

Postby SP » 03 Sep 2012

The o before a var like oReversingBar is the old TS mark for ref values. It should compile
with oReversingBar( NumericRef ) even it is not used in the strategy.


If you need more details of the swings like date,time and price you need to create a function
ZigZag2

Code: Select all

inputs:
Price( numericseries ),
RetraceMethod( numericsimple ), {1 = percent, 2 = number}
retrace( numericsimple ),
LineColor( numericsimple ),
LineWidth( numericsimple ),
PlotLine( truefalse ),
SwingDate0( numericref ),
SwingTime0( numericref ),
SwingPrice0(numericref ),
SwingDate1( numericref ),
SwingTime1( numericref ),
SwingPrice1(numericref ) ;

variables:
NewSwingPrice( 0 ),
SwingPrice( Price ), { used as a convenient 2-element array }
SwingDate( Date ), { used as a convenient 2-element array }
SwingTime( Time ), { used as a convenient 2-element array }
TLDir( 0 ), { TLDir = -1 implies prev TL dn, +1 implies prev TL up }
RetraceFctrUp( 1 + retrace * .01 ),
RetraceFctrDn( 1 - retrace * .01 ),
SaveSwing( false ),
AddTL( false ),
UpdateTL( false ),
TLRef( 0 ),
ZigZagTrend( 0 ) ;

{ Candidate swings are just-confirmed, 3-bar (Str=1), SwingHi's and SwingLo's }

NewSwingPrice = SwingHigh( 1, Price, 1, 2 ) ;
if NewSwingPrice <> -1 then
begin
if (RetraceMethod = 1 and TLDir <= 0 and NewSwingPrice >= SwingPrice * RetraceFctrUp )
or ( RetraceMethod = 2 and TLDir <= 0 and NewSwingPrice >= SwingPrice + retrace )
then
{ prepare to add new up TL }
begin
SaveSwing = true ;
AddTL = true ;
TLDir = 1 ;
end
else if TLDir = 1 and NewSwingPrice >= SwingPrice then
{ prepare to update prev up TL }
begin
SaveSwing = true ;
UpdateTL = true ;
end ;
end
else
begin
NewSwingPrice = SwingLow( 1, Price, 1, 2 ) ;
if NewSwingPrice <> -1 then
begin
if (RetraceMethod = 1 and TLDir >= 0 and NewSwingPrice <= SwingPrice * RetraceFctrDn)
or (RetraceMethod = 2 and TLDir >= 0 and NewSwingPrice <= SwingPrice - retrace )
then
{ prepare to add new dn TL }
begin
SaveSwing = true ;
AddTL = true ;
TLDir = -1 ;
end
else if TLDir = -1 and NewSwingPrice <= SwingPrice then
{ prepare to update prev dn TL }
begin
SaveSwing = true;
UpdateTL = true ;
end ;
end ;
end ;

if SaveSwing then
{ save new swing and reset SaveSwing }
begin
SwingPrice = NewSwingPrice ;
SwingDate = Date[1] ;
SwingTime = Time[1] ;
SaveSwing = false ;
end ;

if AddTL then
{ add new TL and reset AddTL }
begin
if Plotline then
begin
TLRef = TL_New( SwingDate, SwingTime, SwingPrice, SwingDate[1], SwingTime[1],
SwingPrice[1] ) ;
TL_SetExtLeft( TLRef, false ) ;
TL_SetExtRight( TLRef, false ) ;
TL_SetSize( TLRef, LineWidth ) ;
TL_SetColor( TLRef, LineColor ) ;
SwingDate0 = SwingDate ;
SwingTime0 = SwingTime ;
SwingPrice0 = SwingPrice ;
SwingDate1 = SwingDate[1] ;
SwingTime1 = SwingTime[1] ;
SwingPrice1 = SwingPrice[1] ;
end ;
AddTL = false ;
end
else if UpdateTL then
{ update prev TL and reset UpdateTL }
begin
if PlotLine then
begin
TL_SetEnd( TLRef, SwingDate, SwingTime, SwingPrice ) ;
SwingDate0 = SwingDate ;
SwingTime0 = SwingTime ;
SwingPrice0 = SwingPrice ;
end ;
UpdateTL = false ;
end ;
ZigZag2 = 1 ;


or you take a look at the attached indicator how to store the last swings into an array and show the point when the direction changed.
Attachments
ZigZag.pla
(138.19 KiB) Downloaded 719 times

lois77
Posts: 23
Joined: 05 Aug 2012
Has thanked: 7 times

Re: How to take the inflection points on ZIG-ZAG indicator

Postby lois77 » 03 Sep 2012

Hi.

Once again, thanks for your help.

Your function _ZigZagV1 was really of my interest as it returns the turning points, so I could code some conditions and apply it on a strategy.
I set up the inputs, changed the Pct to Pnts code, but I couldn't solve the "oReversingBar" issue. My programming level is very basic. I just don't know how to get it working.

Kind regards.

earmarques
Posts: 13
Joined: 07 Dec 2013
Has thanked: 1 time
Been thanked: 1 time

Re: How to take the inflection points on ZIG-ZAG indicator

Postby earmarques » 28 Dec 2013

Hi everyone!

Somebody knows how can I hide the dynamic TL of this indy?

Great indicator, thanks to StratMan!

User avatar
MAtricks
Posts: 789
Joined: 09 Apr 2012
Has thanked: 286 times
Been thanked: 288 times

Re: How to take the inflection points on ZIG-ZAG indicator

Postby MAtricks » 09 Jan 2014

Change: PlotZigZag( true ),
to: PlotZigZag( false),

gianluca71
Posts: 15
Joined: 22 Aug 2018
Has thanked: 2 times

Re: How to take the inflection points on ZIG-ZAG indicator

Postby gianluca71 » 19 Sep 2018

Hi all,
I need a big help from somebody to assign in different variable the zigzag points.
As you can see from code below, I'd like to assign the last 4 values of zigzag to and current price to 5 different variables.
I tried with the following code, but when I print the values (point_a, point_b, point_c, point_d, point_e) only point_c and point_d are correct.
Thanks a lot in advance, regards

Code: Select all

inputs:
Price( Close ),
RetracePnts( 34 ),
LineColor( Yellow ),
LineWidth( 1 ) ;

variables:
var0( 0 ),
var1( Price ),
var2( Date ),
var3( Time ),
var4( 0 ),
var5( false ),
var6( false ),
var7( false ),
var8( 0 ),
point_a( 0 ),
point_b( 0 ),
point_c( 0 ),
point_d( 0 ),
point_e( 0 );


var0 = SwingHigh( 1, Price, 1, 2 ) ;
if var0 <> -1 then
begin
condition1 = var4 <= 0 and var0 >= var1 + RetracePnts ;
if condition1 then
begin
var5 = true ;
var6 = true ;
var4 = 1 ;
end
else
begin
condition1 = var4 = 1 and var0 >= var1 ;
if condition1 then
begin
var5 = true ;
var7 = true ;
end;
end ;
end
else
begin
var0 = SwingLow( 1, Price, 1, 2 ) ;
if var0 <> -1 then
begin
condition1 = var4 >= 0 and var0 <= var1 - RetracePnts ;
if condition1 then
begin
var5 = true ;
var6 = true ;
var4 = -1 ;
end
else
begin
condition1 = var4 = -1 and var0 <= var1 ;
if condition1 then
begin
var5 = true;
var7 = true ;
end ;
end;
end ;
end ;

if var5 then

begin
var1 = var0 ;
var2 = Date[1] ;
var3 = Time[1] ;
var5 = false ;
end ;

if var6 then
// *********block for plot of zigzag*********
begin
value1 = Close[Findbar(var2[1], var3[1])] ;
plot1[Findbar(var2[1], var3[1])](value1) ;
var6 = false ;
// *********assignment of last 4 values*********
point_a = value1[3] ;
point_b = value1[2] ;
point_c = value1[1] ;
point_d = value1[0] ;
point_e = close ; // close of last bar (current price)

print(point_b); // output print
end ;


Return to “MultiCharts”