Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 3

mila.com  
Artyom Trishkin:
We need to look deeper...

Deeper, where is that?

All attempts, with the new indicator, lead to a hang-up.

And with the indicator, which is unchanged, there's a momentary hiccup.

What could be the solution?

Artyom Trishkin  
mila.com:

Deeper, where is that?

All attempts, with the new indicator, result in a hang-up.

And with the unchanged indicator, there is a momentary hiccup.

What could be the solution?

In your indicator - instead of using in it the data of other custom indicators, in particular - search for fractals of any dimension, just make a function for searching such fractals and work with them.
mila.com  
Artyom Trishkin:
Just make functions to find such fractals and work with them.

For you, it's a simple thing to do.)

but for me, it's an impossible task.

A function like this?


bool isDnFractal(int bar,int max,const double &low[])
  {
//---
   for(int i=1; i<=max; i++) {
      if(i<=leftSide && low[bar]>low[bar-i])    return(false);
      if(i<=rightSide && low[bar]>=low[bar+i])  return(false);
      }
//---
   return(true);
  }

It's a lower fractal.

How to use it?

Artyom Trishkin  
mila.com:

For you, it's easier to do.)

But for me, it's an impossible task.

How to implant the function from that indicator into mine?


bool isDnFractal(int bar,int max,const double &low[])
  {
//---
   for(int i=1; i<=max; i++) {
      if(i<=leftSide && low[bar]>low[bar-i])    return(false);
      if(i<=rightSide && low[bar]>=low[bar+i])  return(false);
      }
//---
   return(true);
  }

Well, you need it to return the price of a fractal on the required bar. Here, I made a simple indicator. It has two functions that you can take out of it and use in yours - I specially organized them as functions - with checks for invalid values.

//+------------------------------------------------------------------+
//|                                             iFreeNumFractals.mq4 |
//|              Copyright 2016, Artem A. Trishkin, Skype artmedia70 |
//|                       https://login.mql5.com/ru/users/artmedia70 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Artem A. Trishkin, Skype artmedia70"
#property link      "https://login.mql5.com/ru/users/artmedia70"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot UpperFractal
#property indicator_label1  "UpperFractal"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot LowerFractal
#property indicator_label2  "LowerFractal"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrSteelBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters
input          int      LeftNum=2;     // Количество баров слева
int leftNum;     // Количество баров слева
input          int      RightNum=2;    // Количество баров справа
int rightNum;    // Количество баров справа
//--- indicator buffers
double         BufferUpperFractal[];
double         BufferLowerFractal[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,BufferUpperFractal);
   SetIndexBuffer(1,BufferLowerFractal);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW,159);
   PlotIndexSetInteger(1,PLOT_ARROW,159);
   SetIndexArrow(0,217);
   SetIndexArrow(1,218);
//---
   leftNum=(LeftNum<1?1:LeftNum);
   rightNum=(RightNum<1?1:RightNum);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//---
   if(rates_total<leftNum+rightNum) return(0);
   int limit=rates_total-prev_calculated;
   if(limit>0) {
      ArrayInitialize(BufferUpperFractal,0.0);
      ArrayInitialize(BufferUpperFractal,0.0);
      limit=rates_total-leftNum-1;
      }
   //---
   for(int i=limit; i>rightNum; i--) {
      if(GetFreeUpperFractal(i,limit,high,leftNum,rightNum)>0) BufferUpperFractal[i]=high[i];
      if(GetFreeLowerFractal(i,limit,low ,leftNum,rightNum)>0) BufferLowerFractal[i]=low[i];
      }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+----------------------------------------------------------------------------+
double GetFreeLowerFractal(int shift,const int limit,const double &low[],int left_dimension=2,int right_dimension=2) {
   if(left_dimension<1)  left_dimension=1;
   if(right_dimension<1) right_dimension=1;
   if(shift-right_dimension<1 || shift+left_dimension>limit-1) return(-1);
   for(int i=shift; i>shift-right_dimension; i--) if(low[i]>low[i-1]) return(-1);
   for(int i=shift; i<shift+left_dimension; i++)  if(low[i]>low[i+1]) return(-1);
   return(low[shift]);
}
//+----------------------------------------------------------------------------+
double GetFreeUpperFractal(int shift,const int limit,const double &high[],int left_dimension=2,int right_dimension=2) {
   if(left_dimension<1)  left_dimension=1;
   if(right_dimension<1) right_dimension=1;
   if(shift-right_dimension<1 || shift+left_dimension>limit-1) return(-1);
   for(int i=shift; i>=shift-right_dimension; i--) if(high[i]<high[i-1]) return(-1);
   for(int i=shift; i<=shift+left_dimension; i++)  if(high[i]<high[i+1]) return(-1);
   return(high[shift]);
}
//+----------------------------------------------------------------------------+
Artyom Trishkin  

To completely detach the functions for getting arbitrary fractals from the indicator, we should not pass by reference arrays high[] and low[] and limit value into them.

Since our code is very close to MQL5, we will have to refuse High[], Low[], iHigh() and iLow() functions. This is how it will look in this indicator:

//+------------------------------------------------------------------+
//|                                             iFreeNumFractals.mq4 |
//|              Copyright 2016, Artem A. Trishkin, Skype artmedia70 |
//|                       https://login.mql5.com/ru/users/artmedia70 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Artem A. Trishkin, Skype artmedia70"
#property link      "https://login.mql5.com/ru/users/artmedia70"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot UpperFractal
#property indicator_label1  "UpperFractal"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot LowerFractal
#property indicator_label2  "LowerFractal"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrSteelBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters
input          int      LeftNum=2;     // Количество баров слева
int leftNum;     // Количество баров слева
input          int      RightNum=2;    // Количество баров справа
int rightNum;    // Количество баров справа
//--- indicator buffers
double         BufferUpperFractal[];
double         BufferLowerFractal[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,BufferUpperFractal);
   SetIndexBuffer(1,BufferLowerFractal);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   // PlotIndexSetInteger(0,PLOT_ARROW,217);
   // PlotIndexSetInteger(1,PLOT_ARROW,218);
   SetIndexArrow(0,217);
   SetIndexArrow(1,218);
//---
   leftNum=(LeftNum<1?1:LeftNum);
   rightNum=(RightNum<1?1:RightNum);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//---
   if(rates_total<leftNum+rightNum) return(0);
   int limit=rates_total-prev_calculated;
   if(limit>0) {
      ArrayInitialize(BufferUpperFractal,0.0);
      ArrayInitialize(BufferUpperFractal,0.0);
      limit=rates_total-leftNum-1;
      }
   //---
   for(int i=limit; i>rightNum; i--) {
      if(GetFreeUpperFractal(Symbol(),PERIOD_CURRENT,i,leftNum,rightNum)>0) BufferUpperFractal[i]=high[i];
      if(GetFreeLowerFractal(Symbol(),PERIOD_CURRENT,i,leftNum,rightNum)>0) BufferLowerFractal[i]=low[i];
      }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+----------------------------------------------------------------------------+
double GetFreeLowerFractal(const string symbol_name,ENUM_TIMEFRAMES timeframe,int shift,int left_dimension=2,int right_dimension=2) {
   int bars=Bars(symbol_name,timeframe);
   if(left_dimension<1)  left_dimension=1;
   if(right_dimension<1) right_dimension=1;
   if(shift-right_dimension<1 || shift+left_dimension>bars-1) return(-1);
   for(int i=shift; i>shift-right_dimension; i--) if(GetPriceLow(symbol_name,timeframe,i)>GetPriceLow(symbol_name,timeframe,i-1)) return(-1);
   for(int i=shift; i<shift+left_dimension; i++)  if(GetPriceLow(symbol_name,timeframe,i)>GetPriceLow(symbol_name,timeframe,i+1)) return(-1);
   return(GetPriceLow(symbol_name,timeframe,shift));
}
//+----------------------------------------------------------------------------+
double GetFreeUpperFractal(const string symbol_name,ENUM_TIMEFRAMES timeframe,int shift,int left_dimension=2,int right_dimension=2) {
   int bars=Bars(symbol_name,timeframe);
   if(left_dimension<1)  left_dimension=1;
   if(right_dimension<1) right_dimension=1;
   if(shift-right_dimension<1 || shift+left_dimension>bars-1) return(-1);
   for(int i=shift; i>=shift-right_dimension; i--) if(GetPriceHigh(symbol_name,timeframe,i)<GetPriceHigh(symbol_name,timeframe,i-1)) return(-1);
   for(int i=shift; i<=shift+left_dimension; i++)  if(GetPriceHigh(symbol_name,timeframe,i)<GetPriceHigh(symbol_name,timeframe,i+1)) return(-1);
   return(GetPriceHigh(symbol_name,timeframe,shift));
}
//+----------------------------------------------------------------------------+
double GetPriceHigh(const string symbol_name, ENUM_TIMEFRAMES timeframe, int shift){
   double array[1];
   if(CopyHigh(symbol_name,timeframe,shift,1,array)==1) return(array[0]);
   return(-1);
}
//+----------------------------------------------------------------------------+
double GetPriceLow(const string symbol_name, ENUM_TIMEFRAMES timeframe, int shift){
   double array[1];
   if(CopyLow(symbol_name,timeframe,shift,1,array)==1) return(array[0]);
   return(-1);
}
//+----------------------------------------------------------------------------+
However, we should also check for -1 from functions GetPriceHigh() and GetPriceLow()
strongflex  
Hi, advise how to make Expert Advisor using 15 minute time frame to check the value every 20 minutes, say at 9-20, 9-40 followed by crossover RSI and if the level is crossed in twenty minutes, checked the price change. This is what I need to fix:
if (Hour()==9 && (Minute() == 20) && (RSI>70))
Price2==Bid;
     {
      if (Hour()==9 && (Minute() == 40) && (Bid<Price2))
  
         {
          ticket=OrderSend(Symbol(),OP_SELL, Lts, Bid, SP,0,0, NULL, Magic, 0, Blue);
          return(0);
         }
     }

That is, let's say at 9-20 the RSI was crossed. I want my EA to remember the price at 9-20 and at 9-40 check the last 20 minutes relative to the price at 9-20. If it is going down, it will open a short. Thank you very much.
Artyom Trishkin  
strongflex:
Hi, advise how to make an EA using 15 minute time frame to check the value every 20 minutes, say at 9-20, 9-40 followed by crossover RSI and if the level is crossed in twenty minutes, check the price change. This is what I need to fix:
if (Hour()==9 && (Minute() == 20) && (RSI>70))
Price2==Bid;
     {
      if (Hour()==9 && (Minute() == 40) && (Bid<Price2))
  
         {
          ticket=OrderSend(Symbol(),OP_SELL, Lts, Bid, SP,0,0, NULL, Magic, 0, Blue);
          return(0);
         }
     }

That is, let's say at 9-20 the RSI was crossed. I want my EA to remember the price at 9-20 and at 9-40 check the last 20 minutes relative to the price at 9-20. If it is going down, it will open a short. Thank you very much.

:)

What if you run the Expert Advisor at 9.22?

And if there is a system or terminal failure? The price will be lost.

I.e., you have to look for what was 20 minutes ago when the time of this check comes. The time has come in which the minutes are greater than or equal to a multiple of twenty - check the state of RSI on the bar, which was 20 minutes ago. If it has the right crossover, then proceed as planned...

However, on M15 you cannot determine the exact time of crossing and the exact price, but you can look at the price on M1 - at least 15 times more accurate.

strongflex  
Artyom Trishkin:

:)

What if you run the EA at 9.22 ?

What if there is a system or terminal failure? The price will be lost.

I.e., you have to look for what was 20 minutes ago when the time of this check comes. The time has come in which the minutes are greater than or equal to a multiple of twenty - check the state of RSI on the bar, which was 20 minutes ago. If it has the desired crossing, then we proceed as planned...

However, on M15 you cannot determine the exact time of crossing and the exact price, but you can look at the price on M1 - at least 15 times more accurate.

OK, I guess I can't do it))) Guys, who can write this part of the code? I'll pay 1000 rubles
Alekseu Fedotov  
strongflex:
Okay, I understand that I can not cope with it)))) guys who can write this part of the code? I'll pay 1000 rubles
double rci20 = iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,20);//RCI 20 минут назад.
   Comment("RSI = ",rci20);

And where's the thousand?

(just kidding)

strongflex  
Alekseu Fedotov:
double rci20 = iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,20);//RCI 20 минут назад.
   Comment("RSI = ",rci20);

And where's the thousand?

(joking)

The RSI needs to be 15 minutes long. We need the EA to check it every 20 minutes from market opening (9-00, 9-20, 9-40 etc.) Let's say at 10-20 there is a cross from below to above the 70 level it remembers the price and at 10-40 it checks if the price is lower than at 10-20 it opens a short.