help with indicator handler array

SpikesHardy
30

hi,

someone should kindly help me get the last 20 lower and upper fractal handles . i have sample code below which compiles but i 'm unable to get the indicator handles to and array that gives me the low or high of the nth candle on which the fractal form. thank you

void OnTick()
  {
//calc Ask & Bid
double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

string signal;


 

MqlRates PriceArray[];
 
ArraySetAsSeries(PriceArray,true);

 
int Data=CopyRates(_Symbol,_Period,0,3,PriceArray);

// creat fractals array
double UpperFractal[],LowerFractal[];
 


 
int  FRACTALS=iFractals(_Symbol,_Period);



 
ArraySetAsSeries(UpperFractal,true);
ArraySetAsSeries(LowerFractal,true);


 
CopyBuffer(FRACTALS,UPPER_LINE,1,3,UpperFractal);
CopyBuffer(FRACTALS,LOWER_LINE,1,3,LowerFractal);

}
Fractals - Bill Williams' Indicators - MetaTrader 5 Help
Fractals - Bill Williams' Indicators - MetaTrader 5 Help
  • www.metatrader5.com
All markets are characterized by the fact that on the most part the prices do not change too much, and only short periods of time (15–30 percent)...
Vladimir Karputov
Moderator
271381
SpikesHardy :

hi,

someone should kindly help me get the last 20 lower and upper fractal handles . i have sample code below which compiles but i 'm unable to get the indicator handles to and array that gives me the low or high of the nth candle on which the fractal form. thank you

1. You are making a gross mistake: you create an indicator handle at every tick! REMEMBER: The MQL5 style implies that the indicator handle is created ONCE and must be done in OnInit.

2. An example of how to work with Fractals:

Forum on trading, automated trading systems and testing trading strategies

How to start with MQL5

Vladimir Karputov, 2021.06.05 06:59

Yes, Fractals can contain '0.0' or 'EMPTY_VALUE'. Example:

Code: 'iFractals.mq5'

//+------------------------------------------------------------------+
//|                                                    iFractals.mq5 |
//|                              Copyright © 2021, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, Vladimir Karputov"
#property version   "1.000"
//--- input parameters
input int         InputBars=9;
//---
int      handle_iFractals;             // variable for storing the handle of the iFractals indicator
bool     m_init_error      = false;    // error on InInit
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- forced initialization of variables
   m_init_error            = false;    // error on InInit
//--- create handle of the indicator iFractals
   handle_iFractals=iFractals(Symbol(),Period());
//--- if the handle is not created
   if(handle_iFractals==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iFractals indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      m_init_error=true;
      return(INIT_SUCCEEDED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(m_init_error)
      return;
//---
   double upper[],lower[];
   ArraySetAsSeries(upper,true);
   ArraySetAsSeries(lower,true);
   int start_pos=0,count=6;
   if(!iGetArray(handle_iFractals,UPPER_LINE,start_pos,count,upper) || !iGetArray(handle_iFractals,LOWER_LINE,start_pos,count,lower))
      return;
//---
   string text="";
   for(int i=0; i<count; i++)
     {
      //---
      string text_upper="";
      if(upper[i]==0.0 || upper[i]==EMPTY_VALUE)
         text_upper="";
      else
         text_upper=DoubleToString(upper[i],Digits());
      //---
      string text_lower="";
      if(lower[i]==0.0 || lower[i]==EMPTY_VALUE)
         text_lower="";
      else
         text_lower=DoubleToString(lower[i],Digits());
      //---
      text=text+IntegerToString(i)+"#: "+"Upper "+text_upper+", Lower "+text_lower+"\n";
     }
   Comment(text);
  }
//+------------------------------------------------------------------+
//| Get value of buffers                                             |
//+------------------------------------------------------------------+
bool iGetArray(const int handle,const int buffer,const int start_pos,
               const int count,double &arr_buffer[])
  {
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);
      return(false);
     }
   ArrayFree(arr_buffer);
//--- reset error code
   ResetLastError();
//--- fill a part of the iBands array with values from the indicator buffer
   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
   if(copied!=count)
     {
      //--- if the copying fails, tell the error code
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",
                  __FILE__,__FUNCTION__,count,copied,GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(false);
     }
   return(result);
  }
//+------------------------------------------------------------------+


Result:

iFractals


(Follow the link - at the bottom of the message there is a code 'iFractals.mq5')

SpikesHardy
30
SpikesHardy  
Vladimir Karputov #:

1. You are making a gross mistake: you create an indicator handle at every tick! REMEMBER: The MQL5 style implies that the indicator handle is created ONCE and must be done in OnInit.

2. An example of how to work with Fractals:


(Follow the link - at the bottom of the message there is a code 'iFractals.mq5')

thank you very much sir
SpikesHardy
30
SpikesHardy  
Vladimir Karputov #:

1. You are making a gross mistake: you create an indicator handle at every tick! REMEMBER: The MQL5 style implies that the indicator handle is created ONCE and must be done in OnInit.

2. An example of how to work with Fractals:


(Follow the link - at the bottom of the message there is a code 'iFractals.mq5')

please i couldn't get what i wanted with the code above so i've tried to transfer my mql4 script code which compiles and works fine to mql5 with help from the first code you recommended. however the mql5 version compiles but doesn't work . kindly check both codes and correct the mql5 version for me. forgive me for your time.
 //CODE IN MQL4
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   double arrUp[20],arrDn[20],val=0;
   int count = 0;
   int i = 0;
   datetime foundUp = 0;
   datetime foundDn = 0;

   for(i=0; i<Bars; i++)
     {
      val = iFractals(NULL,0,MODE_UPPER,i);
      if(val!=0)
        {
         arrUp[count] = val;
         if(count==0)
            foundUp = Time[i];
         count++;
         if(count==20)
            break;
        }
     }

   count = 0;
   for(i=0; i<Bars; i++)
     {
      val = iFractals(NULL,0,MODE_LOWER,i);
      if(val!=0)
        {
         arrDn[count] = val;
         if(count==0)
            foundDn = Time[i];
         count++;
         if(count==20)
            break;
        }

     }
     Alert(arrDn[1]);
  }

//   how i've done it in mql5. it compiles but alert doesnt work

//+------------------------------------------------------------------+
//|                                                    iFractals.mq5 |
//|                              Copyright © 2021, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, Vladimir Karputov"
#property version   "1.000"
//--- input parameters
 
//---
int      handle_iFractals;             // variable for storing the handle of the iFractals indicator
bool     m_init_error      = false;    // error on InInit
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnStart()
  {
   datetime fup=0,fdn=0;
//--- forced initialization of variables
   m_init_error            = false;    // error on InInit
//--- create handle of the indicator iFractals
   handle_iFractals=iFractals(Symbol(),Period());
//--- if the handle is not created
   if(handle_iFractals==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iFractals indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      m_init_error=true;
      return(INIT_SUCCEEDED);
     }
//---
   return(INIT_SUCCEEDED);







//---
   double upper[],lower[];
   ArraySetAsSeries(upper,true);
   ArraySetAsSeries(lower,true);
   int start_pos=0,count=6, val=0;

//---

   for(int i=0; i<count; i++)
     {
      val=CopyBuffer(handle_iFractals,UPPER_LINE,start_pos,count,upper);
      if(val!=0)
        {
         upper[count]=val;
         if(count==0)
            fup= i;
         count++;
         if(count==20)
            break;
        }
     }

   for(int i=0; i<count; i++)
     {
      val=CopyBuffer(handle_iFractals,LOWER_LINE,start_pos,count,lower);

      if(val!=0)
        {
         lower[count]=val;
         if(count==0)
            fdn= i;
         count++;
         if(count==20)
            break;
        }
     }
   Alert(lower[4]);
  }


//+------------------------------------------------------------------+
//+------------------------------------------------------------------+


Files:
fract.PNG  30 kb
Vladimir Karputov
Moderator
271381
Here is a forum for MQL5. Please attach your MQL5 file (you can attach using the button Attach file)
SpikesHardy
30
SpikesHardy  
Vladimir Karputov #:
Here is a forum for MQL5. Please attach your MQL5 file (you can attach using the butto

Please i have attached files for both mql4 and mql5 . the mql4 versions works fine you can use that to better understand what i want to do. thank you

Files:
Vladimir Karputov
Moderator
271381
SpikesHardy # :

Please i have attached files for both mql4 and mql5 . the mql4 versions works fine you can use that to better understand what i want to do. thank you

Your MQL5 file won't compile. Please correct the errors.


Add: This should be an ADVISOR, not a SCRIPT.


Add add: example in  

help with indicator handler array
help with indicator handler array
  • 2021.09.15
  • www.mql5.com
hi, someone should kindly help me get the last 20 lower and upper fractal handles...
SpikesHardy
30
SpikesHardy  
Vladimir Karputov #:

Your MQL5 file won't compile. Please correct the errors.

this one compiled
Files:
Vladimir Karputov
Moderator
271381
Just tell me what exactly you need. Do you need to issue an Alert if a Fractal appears?
SpikesHardy
30
SpikesHardy  
Vladimir Karputov #:
Just tell me what exactly you need. Do you need to issue an Alert if a Fractal appears?
 Please not just the current fractal but based on my analysis, i may need the 4th lower fractal or the 10th upper fractal or any fractal with a range between the current and the 20th fractals. i want to be able to change the fractal index in the alert function to any number between 0 and 20 e.g  at anytime within the code.  ALL I NEED IS A CODE THAT WOULD COUNT THE LOWER AND UPPER FRACTALS BACKWARDS TO THE 20TH AND ASSIGN THEM TO AN ARRAY. i hope you'd understand my illustration .THANK YOU
Files: