MTF FRACTAL MT4 TO MT5 CONVERSION

Jajaofopobo

Trader
Sep 6, 2021
64
7
24
I attempted to convert an MT4 multi-timeframe fractal indicator to MT5 but encountered difficulties due to the differences in syntax, particularly with the absence of barshift in MQL5. I'm not acoder, I would greatly appreciate any assistance in implementing the iFractals coding (copy buffer and all) into the code. Thank you.
Below is the intital mt4 file and my mt5 version I tried to do.
Post automatically merged:

MQL5:
for (int i = 0; i < Maxbar; i++)
{
    datetime fractalTime = iTime(_Symbol, Fractal_Timeframe, i);
    int barShift = iBarShift(_Symbol, Fractal_Timeframe, fractalTime);
    int fractalBarIndex = iFractals(_Symbol, Fractal_Timeframe);
 
    if (barShift < 3)
    {
        if (i < ArraySize(UpBuffer)) UpBuffer[i] = 0;
        if (i < ArraySize(DoBuffer)) DoBuffer[i] = 0;
        continue;
    }
        CopyBuffer(fractalBarIndex, 0, 0, dif, UpBuffer);
        CopyBuffer(fractalBarIndex, 1, 0, dif, DoBuffer);
 
 
        if (fractalBarIndex != EMPTY_VALUE && i < ArraySize(UpBuffer))
            UpBuffer[i] = UpBuffer[0]; // Change this according to your logic
        else if (i < ArraySize(UpBuffer))
            UpBuffer[i] = 0;
 
        if (fractalBarIndex != EMPTY_VALUE && i < ArraySize(DoBuffer))
            DoBuffer[i] = DoBuffer[0]; // Change this according to your logic
        else if (i < ArraySize(DoBuffer))
            DoBuffer[i] = 0;
 
 
}
 
// no idea anymore on how to go about the ifractals
 

Attachments

  • MTF_Fractal.mq4
    12.3 KB · Views: 2
  • MTF_FRACTAL.mq5
    16 KB · Views: 8

Jajaofopobo

Trader
Sep 6, 2021
64
7
24
MQL4:
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, c, dif;
double tempref = iHigh(_Symbol, Fractal_Timeframe, 1) +
                 iHigh(_Symbol, Fractal_Timeframe, 51) +
                 iHigh(_Symbol, Fractal_Timeframe, 101);
 
 
if (barc != Bars(_Symbol, Fractal_Timeframe) || prev_calculated < 0 || tempref != refchk)
{
    barc = Bars(_Symbol, Fractal_Timeframe);
    refchk = tempref;
 
}
else
    return(0);                                                         
OnDeinit(0);
 
if (PeriodSeconds(Fractal_Timeframe) <= PeriodSeconds()) Fractal_Timeframe1 = Period();
dif = PeriodSeconds(Fractal_Timeframe)/PeriodSeconds();
 
 
 
ArraySetAsSeries(UpBuffer, true);
ArraySetAsSeries(DoBuffer, true);
 
for (int i = 0; i < Maxbar; i++)
{
    datetime fractalTime = iTime(_Symbol, Fractal_Timeframe, i);
    int barShift = iBarShift(_Symbol, Fractal_Timeframe, fractalTime);
    int fractalBarIndex = iFractals(_Symbol, Fractal_Timeframe);
 
    if (barShift < 3)
    {
         UpBuffer[i] = 0;
         DoBuffer[i] = 0;
        continue;
    }
 
    CopyBuffer(fractalBarIndex, 0, barShift, 1, UpBuffer);
    CopyBuffer(fractalBarIndex, 1, barShift, 2, DoBuffer);
 
}
Am i on the right path?
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,735
1,398
144
Odesa
www.earnforex.com
*lack of barshift in the ifractals in mql5
You are mostly on the right path, but it looks like you need a better understanding of how one should work with indicators in MQL5.
  1. When you call iFractals (or any other indicator function) in MT5, you just create a handle for the indicator, you don't read any indicator values.
  2. Due to that, you need to call iFractals and create its handle inside OnInit.
  3. Then, you need to use CopyBuffer to copy the entire range actual values that you need to use in the current instance of OnCalculate.
  4. CopyBuffer puts those values in the buffer (array) of your choice. You need to check the number of returned elements each time you call CopyBuffer. If the number is less than the requested number, it means that the indicator data isn't ready yet. This has to be properly handled (usually, you just return from the handler and wait for the data to become ready).
  5. When you have a well-formed buffer with the indicator values, you can work with them as usual.
 
  • 🚀
Reactions: Jajaofopobo