Download MetaTrader 5

A nice Fractals&Alligator setup suitable for manual trading with an indicator alert or full EA implementation

To add comments, please log in or register
kypa
517
kypa  
Hello!

I need an indicator-like code that makes some kind of an alert for a particular setup with Bill Williams' Fractals and Alligator.

I'll probably write (and publish) the code myself after a few weeks, but I have close to zero coding skills, barely understanding even the documentation.

You can make a fully automated expert out of it, although it's better for manual trading.

Code would be better for MetaTrader 5 (instead of the classic 4) because of the extra timeframes. It really depends on it. In fact it would be best if the indicator scans more than one timeframe and market simultaneously.

The setup is a variation of Bill Williams classic Fractal entry - price action taking out the first Fractal out of the Alligator, except that the middle of the move (defined by two Fractals itself) which does this is also beyond this first Fractal. The starting Fractal of this move needs to be partially within (or better) the Alligator (the middle of its middle bar should be within at least one of Alligator's lines), and this starting Fractal needs to be less extreme than the previous Fractal of same direction.

If all conditions are met we should enter for a nice trend. if all but one - no matter which one - we can enter for a scalp (trade for a move of a lesser scale, e.g. 1/4 timeframe)

The setup is confirmed by a strong bar in its direction - that is a bar with moderate length (1/4 to 1/2 of the breakout move for ex.) and volume and almost absent shadows.

Here is an example (although not the best one):

3m Gold Fractals Alligator 50% setup

Red price tag is the first Fractal outside Alligator, red line of the Fibonacci retracement shows the breakout move between its two Fractals, the starting one being partially within the Alligator, yellow line is the key level 50% of it, red rectangle shows the first Fractal as less extreme than the previous one, the red vertical line shows the confirmation bar.
honest_knave
Moderator
2342
honest_knave  
kypa:

I'll probably write (and publish) the code myself after a few weeks, but I have close to zero coding skills, barely understanding even the documentation.
Excellent, I look forward to seeing the progress. Good luck!
kypa
517
kypa  
honest_knave:
Excellent, I look forward to seeing the progress. Good luck!
Thanks, I look forward in hopes to see any progress of mine too. :D

For now - copy-paste from Documentation and CodeBase:

//+------------------------------------------------------------------+
//|                                               Absolute Chaos.mq5 |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots   5
#property indicator_chart_window
#property indicator_buffers 7
#property indicator_plots   7

#property indicator_label1  "FractalUp"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrDimGray

#property indicator_label2  "FractalDown"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrDimGray

#property indicator_label3  "Alligator Jaws"
#property indicator_type3   DRAW_LINE
#property indicator_color3  C'0,0,166'
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1

#property indicator_label4  "Alligator Teeth"
#property indicator_type4   DRAW_LINE
#property indicator_color4  C'222,0,0'
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1

#property indicator_label5  "Alligator Lips"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrGreen
#property indicator_style5  STYLE_SOLID
#property indicator_width5  1

#property indicator_label6  "Up Setup"
#property indicator_type6   DRAW_ARROW
#property indicator_color6  C'222,0,0'

#property indicator_label7  "Down Setup"
#property indicator_type7   DRAW_ARROW
#property indicator_color7  C'222,0,0'

enum Creation
  {
   Call_iFractals,
   Call_iAlligator,
   Call_IndicatorCreate
  };
  
Creation             type=Call_iFractals;
Creation             Call_iAlligator;
string               symbol=" ";
ENUM_TIMEFRAMES      period=PERIOD_CURRENT;

int                  jaw_period=13;
int                  jaw_shift=8;
int                  teeth_period=8;
int                  teeth_shift=5;
int                  lips_period=5;
int                  lips_shift=3;

ENUM_MA_METHOD       MA_method=MODE_SMMA;
ENUM_APPLIED_PRICE   applied_price=PRICE_MEDIAN;

double         FractalUpBuffer[];
double         FractalDownBuffer[];

double         JawsBuffer[];
double         TeethBuffer[];
double         LipsBuffer[];

double         UpSetupBuffer[];
double         DownSetupBuffer[];

int    fractalshandle;
int    alligatorhandle;

string name=symbol;

string short_name;

int    bars_calculated=0;

int OnInit()
  {  
  
   SetIndexBuffer(0,FractalUpBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,FractalDownBuffer,INDICATOR_DATA);
  
   SetIndexBuffer(2,JawsBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,TeethBuffer,INDICATOR_DATA);
   SetIndexBuffer(4,LipsBuffer,INDICATOR_DATA);
  
   SetIndexBuffer(5,UpSetupBuffer,INDICATOR_DATA);
   SetIndexBuffer(6,DownSetupBuffer,INDICATOR_DATA);

   PlotIndexSetInteger(0,PLOT_ARROW,217);
   PlotIndexSetInteger(1,PLOT_ARROW,218);
  
   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-10);
   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,10);
  
   PlotIndexSetInteger(2,PLOT_SHIFT,jaw_shift);
   PlotIndexSetInteger(3,PLOT_SHIFT,teeth_shift);
   PlotIndexSetInteger(4,PLOT_SHIFT,lips_shift);
  
   PlotIndexSetInteger(5,PLOT_ARROW,216);
   PlotIndexSetInteger(6,PLOT_ARROW,216);
  
   PlotIndexSetInteger(5,PLOT_ARROW_SHIFT,-25);
   PlotIndexSetInteger(6,PLOT_ARROW_SHIFT,25);
  
   PlotIndexSetDouble(5,PLOT_EMPTY_VALUE,0);
   PlotIndexSetDouble(6,PLOT_EMPTY_VALUE,0);

   name=symbol;

   StringTrimRight(name);
   StringTrimLeft(name);

   if(StringLen(name)==0)
     {
      name=_Symbol;
     }

   if(type==Call_iFractals)
      fractalshandle=iFractals(name,period);
   else
      fractalshandle=IndicatorCreate(name,period,IND_FRACTALS);
      
   if(fractalshandle==INVALID_HANDLE)
     {
      PrintFormat("Failed to create handle of the iFractals indicator for the symbol %s/%s, error code %d",
                  name,
                  EnumToString(period),
                  GetLastError());
      return(INIT_FAILED);
     }
    
   if(type==Call_iAlligator)
      alligatorhandle=iAlligator(name,period,jaw_period,jaw_shift,teeth_period,
                        teeth_shift,lips_period,lips_shift,MA_method,applied_price);
   else
     {
      MqlParam pars[8];
      pars[0].type=TYPE_INT;
      pars[0].integer_value=jaw_period;
      pars[1].type=TYPE_INT;
      pars[1].integer_value=jaw_shift;
      pars[2].type=TYPE_INT;
      pars[2].integer_value=teeth_period;
      pars[3].type=TYPE_INT;
      pars[3].integer_value=teeth_shift;
      pars[4].type=TYPE_INT;
      pars[4].integer_value=lips_period;
      pars[5].type=TYPE_INT;
      pars[5].integer_value=lips_shift;
      pars[6].type=TYPE_INT;
      pars[6].integer_value=MA_method;
      pars[7].type=TYPE_INT;
      pars[7].integer_value=applied_price;
      alligatorhandle=IndicatorCreate(name,period,IND_ALLIGATOR,8,pars);
     }

   if(alligatorhandle==INVALID_HANDLE)
     {
      PrintFormat("Failed to create handle of the iAlligator indicator for the symbol %s/%s, error code %d",
                  name,
                  EnumToString(period),
                  GetLastError());
      return(INIT_FAILED);
     }
    
   short_name=StringFormat("Absolute Chaos(%s/%s)",name,EnumToString(period));
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);

   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 values_to_copy;
   int setup_check;

   int calculated=BarsCalculated(fractalshandle);
   if(calculated<=0)
     {
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError());
      return(0);
     }
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)
     {
      if(calculated>rates_total)
         {
          setup_check=(jaw_shift+1);
          values_to_copy=rates_total;
         }
      else
         {
          setup_check=(jaw_shift+1);
          values_to_copy=calculated;
         }
     }
   else
     {
      setup_check=(rates_total-3);
      values_to_copy=(rates_total-prev_calculated)+1;
     }
  
   if(!FillFractalsArraysFromBuffers(FractalUpBuffer,FractalDownBuffer,fractalshandle,values_to_copy)) return(0);
    
   if(!FillAlligatorArraysFromBuffers(JawsBuffer,jaw_shift,TeethBuffer,teeth_shift,LipsBuffer,lips_shift,alligatorhandle,values_to_copy)) return(0);
      
   bars_calculated=calculated;  
  
   int s, r, t, u, v, w;
  
   for (s=setup_check; s<rates_total && !IsStopped(); s++)
      {
       UpSetupBuffer[s]=0;
       DownSetupBuffer[s]=0;
      }
      
   for (r=setup_check; r<rates_total && !IsStopped(); r++)
      {
       double BullPointOne = 0;
       double BullPointTwo = 0;
       double BullPointThree = 0;
      
       if(FractalUpBuffer[r]==high[r] && high[r]>MathMax(MathMax(LipsBuffer[r-lips_shift],TeethBuffer[r-teeth_shift]),MathMax(TeethBuffer[r-teeth_shift],JawsBuffer[r-jaw_shift])))
         {          
          u = jaw_shift;
          w = jaw_shift;
          for (t=r; t>u && !IsStopped(); t--)
            {
             if(FractalDownBuffer[t]==low[t])
               {
                u = t;
                if(0.5*(high[t]+low[t])>=MathMin(MathMin(LipsBuffer[t-lips_shift],TeethBuffer[t-teeth_shift]),MathMin(TeethBuffer[t-teeth_shift],JawsBuffer[t-jaw_shift])))
                  {
                   for (v=t; v>w && !IsStopped(); v--)
                     {
                      if(FractalUpBuffer[v]==high[v] && high[v]>=MathMax(MathMax(LipsBuffer[v-lips_shift],TeethBuffer[v-teeth_shift]),MathMax(TeethBuffer[v-teeth_shift],JawsBuffer[v-jaw_shift])))
                        {
                         BullPointOne = high[v];
                         BullPointTwo = low[t];
                         BullPointThree = high[r];
                         w = v;
                         if((BullPointOne>=BullPointTwo) && (0.5*(BullPointTwo+BullPointThree)>=MathMax(open[v],close[v]))) UpSetupBuffer[r]=high[r];
                        }
                     }
                  }
               }
            }
         }
      }
      
   int l, m, n, o, p;
  
   for (l=setup_check; l<rates_total && !IsStopped(); l++)
      {
       double BearPointOne = 0;
       double BearPointTwo = 0;
       double BearPointThree = 0;
        
       if(FractalDownBuffer[l]==low[l] && low[l]<MathMin(MathMin(LipsBuffer[l-lips_shift],TeethBuffer[l-teeth_shift]),MathMin(TeethBuffer[l-teeth_shift],JawsBuffer[l-jaw_shift])))
         {          
          m = jaw_shift;
          p = jaw_shift;
          for (n=l; n>m && !IsStopped(); n--)
            {
             if(FractalUpBuffer[n]==high[n])
               {
                m = n;
                if(0.5*(high[n]+low[n])<=MathMax(MathMax(LipsBuffer[n-lips_shift],TeethBuffer[n-teeth_shift]),MathMax(TeethBuffer[n-teeth_shift],JawsBuffer[n-jaw_shift])))
                  {
                   for (o=n; o>p && !IsStopped(); o--)
                     {
                      if(FractalDownBuffer[o]==low[o] && low[o]<=MathMin(MathMin(LipsBuffer[o-lips_shift],TeethBuffer[o-teeth_shift]),MathMin(TeethBuffer[o-teeth_shift],JawsBuffer[o-jaw_shift])))
                        {
                         BearPointOne = low[o];
                         BearPointTwo = high[n];
                         BearPointThree = low[l];
                         p = o;
                         if((BearPointOne<=BearPointTwo) && (0.5*(BearPointTwo+BearPointThree)<=MathMin(open[o],close[o]))) DownSetupBuffer[l]=low[l];
                        }
                     }
                  }
               }
            }
         }
      }
      
   return(rates_total);
  }
  
bool FillFractalsArraysFromBuffers(double &up_arrows[],
                           double &down_arrows[],
                           int fractals_ind_handle,
                           int fractals_amount
                           )
  {

   ResetLastError();

   if(CopyBuffer(fractals_ind_handle,0,0,fractals_amount,up_arrows)<0)
     {

      PrintFormat("Failed to copy data from the iFractals indicator to the FractalUpBuffer array, error code %d",
                  GetLastError());

      return(false);
     }

   if(CopyBuffer(fractals_ind_handle,1,0,fractals_amount,down_arrows)<0)
     {

      PrintFormat("Failed to copy data from the iFractals indicator to the FractalDownBuffer array, error code %d",
                  GetLastError());

      return(false);
     }

   return(true);
  }
  
bool FillAlligatorArraysFromBuffers(double &jaws_buffer[],
                           int j_shift,
                           double &teeth_buffer[],
                           int t_shift,
                           double &lips_buffer[],
                           int l_shift,
                           int alligator_ind_handle,
                           int alligator_amount
                           )
  {

   ResetLastError();

   if(CopyBuffer(alligator_ind_handle,0,-j_shift,alligator_amount,jaws_buffer)<0)
     {
      PrintFormat("Failed to copy data from the iAlligator indicator, error code %d",GetLastError());
      return(false);
     }

   if(CopyBuffer(alligator_ind_handle,1,-t_shift,alligator_amount,teeth_buffer)<0)
     {
      PrintFormat("Failed to copy data from the iAlligator indicator, error code %d",GetLastError());
      return(false);
     }

   if(CopyBuffer(alligator_ind_handle,2,-l_shift,alligator_amount,lips_buffer)<0)
     {
      PrintFormat("Failed to copy data from the iAlligator indicator, error code %d",GetLastError());
      return(false);
     }

   return(true);
  }  
  
  
void OnDeinit(const int reason)
  {
   Comment("");
  }
The demos of iFractals and iAlligator from Documentation merged in one indicator to build the arrays, all I wrote are the iteration functions, and even that borrows ideas from wlxFractals (from CodeBase).

It's not ready yet though, this just the beginning, the first version that actually puts some arrows on the chart. It doesn't abide by all conditions of the setup warning, no code for signal/confirmation bar yet.


P.S. It throws an "Array out of range" error sometimes.
Carl Schreiber
Moderator
7958
Carl Schreiber  
This message shows the line and the position as well!
kypa
517
kypa  
Carl Schreiber:
This message shows the line and the position as well!
The numbers at the end of the row? That makes sense actually.

I feel a bit retarded for not noticing it myself.
To add comments, please log in or register