Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (VoroneTZ, AndrewAMD), 833 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Zig Zag function #448525
02/09/15 10:29
02/09/15 10:29
Joined: Apr 2014
Posts: 482
Sydney, Australia
B
boatman Offline OP
Senior Member
boatman  Offline OP
Senior Member
B

Joined: Apr 2014
Posts: 482
Sydney, Australia
I've been trying to create a zig zag function in Zorro to track the swing highs and swing lows of the price series. I know that this indicator doesn't have any predictive value, but I'd like to test whether entering a trade on a retracement of a recent swing has any merit.

To this end, I've been trying to convert the Metaquotes zig zag indicator across to Lite-C, but the code is unintelligble gibberish to me.

I was wondering if anyone could help me out with this, or at least point me in the right direction.

First, here is the MQL code that I'm trying to convert, followed by my somewhat poor first attempt.

I'd really like to convert the MQL version converted, but don't have a clue where to start. Any clues much appreciated!

Code:
//+------------------------------------------------------------------+
//|                                                       ZigZag.mq4 |
//|                   Copyright 2006-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "2006-2014, MetaQuotes Software Corp."
#property link      "http://www.mql4.com"
#property strict

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1  Red
//---- indicator parameters
input int InpDepth=12;     // Depth
input int InpDeviation=5;  // Deviation
input int InpBackstep=3;   // Backstep
//---- indicator buffers
double ExtZigzagBuffer[];
double ExtHighBuffer[];
double ExtLowBuffer[];
//--- globals
int    ExtLevel=3; // recounting's depth of extremums
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(InpBackstep>=InpDepth)
     {
      Print("Backstep cannot be greater or equal to Depth");
      return(INIT_FAILED);
     }
//--- 2 additional buffers
   IndicatorBuffers(3);
//---- drawing settings
   SetIndexStyle(0,DRAW_SECTION);
//---- indicator buffers
   SetIndexBuffer(0,ExtZigzagBuffer);
   SetIndexBuffer(1,ExtHighBuffer);
   SetIndexBuffer(2,ExtLowBuffer);
   SetIndexEmptyValue(0,0.0);
//---- indicator short name
   IndicatorShortName("ZigZag("+string(InpDepth)+","+string(InpDeviation)+","+string(InpBackstep)+")");
//---- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
   int    i,limit,counterZ,whatlookfor=0;
   int    back,pos,lasthighpos=0,lastlowpos=0;
   double extremum;
   double curlow=0.0,curhigh=0.0,lasthigh=0.0,lastlow=0.0;
//--- check for history and inputs
   if(rates_total<InpDepth || InpBackstep>=InpDepth)
      return(0);
//--- first calculations
   if(prev_calculated==0)
      limit=InitializeAll();
   else 
     {
      //--- find first extremum in the depth ExtLevel or 100 last bars
      i=counterZ=0;
      while(counterZ<ExtLevel && i<100)
        {
         if(ExtZigzagBuffer[i]!=0.0)
            counterZ++;
         i++;
        }
      //--- no extremum found - recounting all from begin
      if(counterZ==0)
         limit=InitializeAll();
      else
        {
         //--- set start position to found extremum position
         limit=i-1;
         //--- what kind of extremum?
         if(ExtLowBuffer[i]!=0.0) 
           {
            //--- low extremum
            curlow=ExtLowBuffer[i];
            //--- will look for the next high extremum
            whatlookfor=1;
           }
         else
           {
            //--- high extremum
            curhigh=ExtHighBuffer[i];
            //--- will look for the next low extremum
            whatlookfor=-1;
           }
         //--- clear the rest data
         for(i=limit-1; i>=0; i--)  
           {
            ExtZigzagBuffer[i]=0.0;  
            ExtLowBuffer[i]=0.0;
            ExtHighBuffer[i]=0.0;
           }
        }
     }
//--- main loop      
   for(i=limit; i>=0; i--)
     {
      //--- find lowest low in depth of bars
      extremum=low[iLowest(NULL,0,MODE_LOW,InpDepth,i)];
      //--- this lowest has been found previously
      if(extremum==lastlow)
         extremum=0.0;
      else 
        { 
         //--- new last low
         lastlow=extremum; 
         //--- discard extremum if current low is too high
         if(low[i]-extremum>InpDeviation*Point)
            extremum=0.0;
         else
           {
            //--- clear previous extremums in backstep bars
            for(back=1; back<=InpBackstep; back++)
              {
               pos=i+back;
               if(ExtLowBuffer[pos]!=0 && ExtLowBuffer[pos]>extremum)
                  ExtLowBuffer[pos]=0.0; 
              }
           }
        } 
      //--- found extremum is current low
      if(low[i]==extremum)
         ExtLowBuffer[i]=extremum;
      else
         ExtLowBuffer[i]=0.0;
      //--- find highest high in depth of bars
      extremum=high[iHighest(NULL,0,MODE_HIGH,InpDepth,i)];
      //--- this highest has been found previously
      if(extremum==lasthigh)
         extremum=0.0;
      else 
        {
         //--- new last high
         lasthigh=extremum;
         //--- discard extremum if current high is too low
         if(extremum-high[i]>InpDeviation*Point)
            extremum=0.0;
         else
           {
            //--- clear previous extremums in backstep bars
            for(back=1; back<=InpBackstep; back++)
              {
               pos=i+back;
               if(ExtHighBuffer[pos]!=0 && ExtHighBuffer[pos]<extremum)
                  ExtHighBuffer[pos]=0.0; 
              } 
           }
        }
      //--- found extremum is current high
      if(high[i]==extremum)
         ExtHighBuffer[i]=extremum;
      else
         ExtHighBuffer[i]=0.0;
     }
//--- final cutting 
   if(whatlookfor==0)
     {
      lastlow=0.0;
      lasthigh=0.0;  
     }
   else
     {
      lastlow=curlow;
      lasthigh=curhigh;
     }
   for(i=limit; i>=0; i--)
     {
      switch(whatlookfor)
        {
         case 0: // look for peak or lawn 
            if(lastlow==0.0 && lasthigh==0.0)
              {
               if(ExtHighBuffer[i]!=0.0)
                 {
                  lasthigh=High[i];
                  lasthighpos=i;
                  whatlookfor=-1;
                  ExtZigzagBuffer[i]=lasthigh;
                 }
               if(ExtLowBuffer[i]!=0.0)
                 {
                  lastlow=Low[i];
                  lastlowpos=i;
                  whatlookfor=1;
                  ExtZigzagBuffer[i]=lastlow;
                 }
              }
             break;  
         case 1: // look for peak
            if(ExtLowBuffer[i]!=0.0 && ExtLowBuffer[i]<lastlow && ExtHighBuffer[i]==0.0)
              {
               ExtZigzagBuffer[lastlowpos]=0.0;
               lastlowpos=i;
               lastlow=ExtLowBuffer[i];
               ExtZigzagBuffer[i]=lastlow;
              }
            if(ExtHighBuffer[i]!=0.0 && ExtLowBuffer[i]==0.0)
              {
               lasthigh=ExtHighBuffer[i];
               lasthighpos=i;
               ExtZigzagBuffer[i]=lasthigh;
               whatlookfor=-1;
              }   
            break;               
         case -1: // look for lawn
            if(ExtHighBuffer[i]!=0.0 && ExtHighBuffer[i]>lasthigh && ExtLowBuffer[i]==0.0)
              {
               ExtZigzagBuffer[lasthighpos]=0.0;
               lasthighpos=i;
               lasthigh=ExtHighBuffer[i];
               ExtZigzagBuffer[i]=lasthigh;
              }
            if(ExtLowBuffer[i]!=0.0 && ExtHighBuffer[i]==0.0)
              {
               lastlow=ExtLowBuffer[i];
               lastlowpos=i;
               ExtZigzagBuffer[i]=lastlow;
               whatlookfor=1;
              }   
            break;               
        }
     }
//--- done
   return(rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int InitializeAll()
  {
   ArrayInitialize(ExtZigzagBuffer,0.0);
   ArrayInitialize(ExtHighBuffer,0.0);
   ArrayInitialize(ExtLowBuffer,0.0);
//--- first counting position
   return(Bars-InpDepth);
  }
//+------------------------------------------------------------------+



My attempt at something similar: I resorted to a completely different approach with my attempt, essentially looking for peaks and valleys using the Fractals indicator (in hindsight, I probably should have just used 'Peak" and 'Valley - would have been simplar!), and then setting those peaks and valleys to be legs of the zig zag if they represent a certain % price move.

Code:
#define NZ_FractalUP AlgoVar[0]
#define NZ_FractalDW AlgoVar[1]
function run()
{
	

	vars Price = series(price());
	
//=======================Fractals========================
	var FractalUP_current = FractalHigh(series(priceHigh()),7); //if no fractal on that bar, returns zero
	var FractalDW_current = FractalLow(series(priceLow()),7); //if no fractal on that bar, returns zero
	//Non-zero fractals only
	if (FractalUP_current != 0)
	NZ_FractalUP = FractalUP_current;
	if (FractalDW_current != 0)
	NZ_FractalDW = FractalDW_current;
	//To access fractals back history, create a series of the non-zero values:
	vars FractalUP = series(NZ_FractalUP);
	vars FractalDW = series(NZ_FractalDW);
		

//	plot("FUseries", FractalUP[0], DOT, GREEN); //plot the series of non-zero up fractals
//	plot("FDseries", FractalDW[0], DOT, RED); //plot the series of non-zero down fractals
	
	var x = 1; //% move to trigger new leg of zigzag 
	static var ZigZagUP;
	static var ZigZagDW;
	if (FractalUP[0] > (x/100)*ZigZagDW + ZigZagDW)
		ZigZagUP = FractalUP[0];
	if (FractalDW[0] < ZigZagUP - (x/100)*ZigZagUP)
		ZigZagDW = FractalDW[0];
		
	plot("ZigZagUP", ZigZagUP, MAIN|DOT, ORANGE);
	plot("ZigZagDW", ZigZagDW, MAIN|DOT, BLUE);

}


Re: Zig Zag function [Re: boatman] #467507
08/07/17 14:08
08/07/17 14:08
Joined: Aug 2017
Posts: 2
T
tahiri Offline
Guest
tahiri  Offline
Guest
T

Joined: Aug 2017
Posts: 2
Hello,
very nice code base of zigzag indicator, I tried this one above but I still
need the entry condition to test it, I tried many cases for entry and get different results, my request is should I enter based on fractal indicator, or zigzag indicator or valley/ peak.

Re: Zig Zag function [Re: tahiri] #467511
08/08/17 05:29
08/08/17 05:29
Joined: Feb 2017
Posts: 369
D
Dalla Offline
Senior Member
Dalla  Offline
Senior Member
D

Joined: Feb 2017
Posts: 369
Is there a difference to what you want to implement and the existing zig zag indicator?

http://www.zorro-trader.com/manual/en/ta.htm

Re: Zig Zag function [Re: Dalla] #467606
08/16/17 11:03
08/16/17 11:03
Joined: Aug 2017
Posts: 2
T
tahiri Offline
Guest
tahiri  Offline
Guest
T

Joined: Aug 2017
Posts: 2
Hello there,
I handle the above zigzag and I come out with 2 zigzag codes base, one for the uptrend and the other for downtrend and I trying to combine both codes to get one special code for zigzag, I got nice results testing each code by itself, you can see my screenshot below.

[img:center]https://lh3.googleusercontent.com/_qxT17...7quaJC2Yj4=s170[/img]

Last edited by tahiri; 08/16/17 11:12.
Re: Zig Zag function [Re: tahiri] #467614
08/17/17 02:12
08/17/17 02:12
Joined: Dec 2014
Posts: 204
Germany
Smon Offline
Member
Smon  Offline
Member

Joined: Dec 2014
Posts: 204
Germany
I also wonder what's the purpose of this project. I'm using Zorros built in ZigZag indicator and find it quite useful.

http://grailforex.com Monster Profit Forex Robot [Re: boatman] #468079
09/17/17 16:44
09/17/17 16:44
Joined: Oct 2016
Posts: 1
1
1894 Offline
Guest
1894  Offline
Guest
1

Joined: Oct 2016
Posts: 1
Hello,
I would like to reprogram this EA but with digital broadband filter.
Have you ideas how to make? I am a beginner.

Attached Files

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