Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1501

 

Sergey Voytsekhovsky #:

... I tried to write myself on the basis of Euclid's algorithm, but I got confused. In general, experience is lacking ...

... The algorithm itself is not complicated. It is simple to calculate on a sheet of paper or a calculator, but I can't translate it into MQL5 language!

Based on my practice of communication on the Forum, I can tell you that if you show the code you have already written, then you will be answered faster and they will tell you where the error is in the code.

Regards, Vladimir.

 
ulong gcd(ulong a, ulong b) {
   ulong c;
   if(a < b) {
      c = a;
      a = b;
      b = c;
   }

   while(b > 0) {
      c = a % b;
      a = b;
      b = c;
   }

   return a;
}

ulong ArrayGcd(ulong &a[]) {
   int n = ArraySize(a);
   if(n == 1) {
      return 0;
   }
   ulong c = gcd(a[0], a[1]);

   for(int i = 2; i < n; i++) {
      c = gcd(a[i], c);
      if(c == 1) {
         break;
      }
   }
   return c;
}

int OnInit() {
   PrintFormat("gcd(35, 140) = %d", gcd(35, 140));
   PrintFormat("gcd(35, 147) = %d", gcd(35, 147));
   PrintFormat("gcd(35, 149) = %d", gcd(35, 149));
   
   ulong a1[] = {35, 140, 28, 7};
   ulong a2[] = {35, 140, 28, 6};
   ulong a3[] = {32, 140, 28, 16};
   PrintFormat("gcd(35, 140, 28, 7) = %d", ArrayGcd(a1));
   PrintFormat("gcd(35, 140, 28, 6) = %d", ArrayGcd(a2));
   PrintFormat("gcd(32, 140, 28, 16) = %d", ArrayGcd(a3));
   return(INIT_SUCCEEDED);
}
 
MrBrooklin #:

Based on my practice of communicating on the Forum, I can tell you that if you show the code you have already written at once, they will answer you faster and tell you where the error is in the code.

Regards, Vladimir.

Ok, thank you very much for your opinion. Let's try it. A brief background for understanding. I plan to use the function in an indicator based on Renko, superimposed on the main chart. There is a need to determine the current size of bricks. ZigZag is built on the basis of Renko chart, it has, among other things, two arrays - one with tops and one with bottoms. Now I need to determine the GreatestCommonDivider of the distances between them to adjust the size of the bricks automatically.

At the moment I've sketched out this kind of krakozabra and that's it, I'm stumped, I can't think of what to do next.

//+------------------------------------------------------------------+
//| Вычисляем значение НОД(НаибольшийОбщийДелитель)=размера кирпичика|
//+------------------------------------------------------------------+
int CalculateBrickSize(int &size_brick, double &Arr_PeakBuff[], double &Arr_BottomBuff[])
  {
   int    size_zigbuf = 0;
   double peak        = 0.0;
   double bottom      = 0.0;

   int zigLag   = 0.0;
   int Arr_ZigLag[];

   if(!ArrayGetAsSeries(Arr_PeakBuff))
      ArraySetAsSeries(Arr_PeakBuff,   true);
   if(!ArrayGetAsSeries(Arr_BottomBuff))
      ArraySetAsSeries(Arr_BottomBuff, true);
   ArrayInitialize(Arr_ZigLag, 0.00);

   size_zigbuf = MathMin(ArraySize(Arr_PeakBuff), ArraySize(Arr_BottomBuff));
   size_zigbuf = MathMin(size_zigbuf, ChartVisibleBars()) - 1;

   if(size_zigbuf > 0)
      ArrayResize(Arr_ZigLag, size_zigbuf);
   for(int i = 0; i < size_zigbuf; i++)
     {
      if(Arr_PeakBuff[i] != EMPTY_VALUE)
         peak = NormalizeDouble(Arr_PeakBuff[i], _Digits);
      if(Arr_BottomBuff[i] != EMPTY_VALUE)
         bottom = NormalizeDouble(Arr_BottomBuff[i], _Digits);
      if(peak == 0 || bottom == 0)
        {
         zigLag = 1;
        }
      else
         zigLag = (int)DoubleToString(MathAbs(peak - bottom)*100000, 0);
      Arr_ZigLag[i] = zigLag;
     }
//---
   int delimoe = 0;
   int delitel = 1;
   int ostatok = 1;
   ArraySort(Arr_ZigLag);

   for(int i = ArraySize(Arr_ZigLag)-1; i > 0; i--)
     {
      if(Arr_ZigLag[i] != Arr_ZigLag[i-1])
        {
         if(delimoe == 0)
            delimoe = MathMax(Arr_ZigLag[i], Arr_ZigLag[i-1]);
         if(delitel == 1)
            delitel = MathMin(Arr_ZigLag[i], Arr_ZigLag[i-1]);
         ostatok = (int)MathMod(delimoe, delitel);
         if(ostatok == 0)
           {
            delimoe = delitel;
            delitel = 1;
            ostatok = 1;
            continue;
           }
         else
            while(ostatok != 0)
              {
               ostatok = (int)MathMod(delimoe, delitel);
               delimoe = delitel;
               delitel = ostatok;
              }
        }
     }
   //Print("delimoe = ", delimoe);
   //Print("delitel = ", delitel);
   //Print("ostatok = ", ostatok);
   return(size_brick);
  }
//+------------------------------------------------------------------+
 
This is of course just a draft, do not judge strictly. I think I went somewhere wrong, it's all kind of messy, I need to find a simpler and clearer solution, but I can't get it into my head yet.....
 
Sergey Voytsekhovsky #:
This is of course just a draft, do not judge strictly. I think I've gone somewhere wrong, it's all kind of messy, I need to find a simpler and clearer solution, but I can't think of it yet....
Sergey, form an array of numbers for which you want to find the NOD and pass it to the ArrayGcd() function from my previous post.

But I'm not sure that you will be able to use NOD in this case. It looks like you want to calculate it for numbers that don't have any rigid relations between them, ensuring the presence of a non-trivial common divisor.
Then with high probability you will always get NOD = 1.
 
Yuriy Bykov #:
Sergey, form an array of numbers for which you want to find the NOD and pass it to the ArrayGcd() function from my previous post.

Thanks, I saw your message after I had sent my last one, now I'm trying to adapt it.

 
Sergey Voytsekhovsky #:

Thanks, I saw your message after I had sent my last one, now I'm trying to adapt it.

You can replace ulong with int everywhere in the code, but then you have to be sure that all the numbers in the array are positive.
 
Yuriy Bykov #:
Sergey, form an array of numbers for which you want to find the NOD and pass it to the ArrayGcd() function from my previous post.

But I'm not sure that you will be able to use the NOD in this case. It looks like you want to calculate it for numbers that don't have any rigid relations between them, providing the presence of a non-trivial common divisor.
Then with high probability you will always get NOD = 1.

Perhaps you need to check, and that's only after coding the algorithm. Thanks for your feedback.

 

Hi all, can you tell me if it is possible to import a function from one EA to another?

For example, there is an EA that opens orders, and there is an EA that monitors the state of the account, where the parameters drawdown, etc. are specified,

I need that when the drawdown of the second EA is reached, the first EA does not open orders....

How to do it for example?

 
psihodelit import a function from one EA to another?

For example, there is an EA that opens orders, and there is an EA that monitors the state of the account, where the parameters drawdown, etc. are specified,

I need that when the drawdown of the second advisor is reached, the first advisor does not open orders....

How to do it for example?

If the first EA opens positions with a certain medzhik, then you can sort these positions in it or in another EA and calculate their total profit loss. And the second Expert Advisor you have counts this profit-loss, so it can not be a drawdown, it does not open positions, it monitors the state of positions with the desired medzhik. And in general, positions, after they are opened, are on their own, and among them you usually need to find the right ones)))))