Gamestudio Links
Zorro Links
Newest Posts
rMomentum always returns 0 when checking for peak/valley
by jcl
Today at 17:38
a little help here?
by 3run
Today at 16:38
How to set a pair trading order right
by jcl
Today at 11:46
Dual Momentum Algorithm - The way Zorro would have done it
by Hredot
Today at 05:21
HIgh FRequency Trading Script -- Highly Profitable
by Yosh
Yesterday at 19:27
Linear Regression in objective function
by jcl
11/22/17 15:40
MT4 vs Zorro
by pascalx
11/22/17 14:00
AUM Magazine
Latest Screens
RPG PARTY
Triton Wing now available on Steam
AckCon'17: Lotter vs The World
Triton Wing
Who's Online
14 registered (Lobo, Hredot, painkiller, 3run, alibaba, Kartoffel, FLD, RealSerious3D, Jerome8911, 1 invisible), 169 Guests and 4 Spiders online.
Key: Admin, Global Mod, Mod
Newest Members
ech87, Lobo, Andy60, AzzoCorp, franzz
17813 Registered Users
Topic Options
Rate This Topic
#448525 - 02/09/15 11:29 Zig Zag function
boatman Offline
Senior Member

Registered: 04/23/14
Posts: 480
Loc: 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);

}

_________________________
www.robotwealth.com

Top
#467507 - 08/07/17 16:08 Re: Zig Zag function [Re: boatman]
tahiri Offline
Guest

Registered: 08/01/17
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.

Top
#467511 - 08/08/17 07:29 Re: Zig Zag function [Re: tahiri]
Dalla Offline
Member

Registered: 02/24/17
Posts: 211
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

Top
#467606 - 08/16/17 13:03 Re: Zig Zag function [Re: Dalla]
tahiri Offline
Guest

Registered: 08/01/17
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]


Edited by tahiri (08/16/17 13:12)

Top
#467614 - 08/17/17 04:12 Re: Zig Zag function [Re: tahiri]
sdh309795gaas Online
Junior Member

Registered: 12/07/14
Posts: 81
I also wonder what's the purpose of this project. I'm using Zorros built in ZigZag indicator and find it quite useful.

Top
#468079 - 09/17/17 18:44 http://grailforex.com Monster Profit Forex Robot [Re: boatman]
1894 Offline
Guest

Registered: 10/26/16
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.


Attachments
SoeHoe.ID_monsterprofit.rar (21 downloads)


Top



Gamestudio download | chip programmers | Zorro platform | shop | resources | magazine | manual | support faq | bugs

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) 3dgamestudio.net