[ARCHIVE]Any rookie question, so as not to clutter up the forum. Professionals, don't pass it by. Can't go anywhere without you - 5. - page 318

 
gyfto:

Or simplify it further.

Here Δ is the difference meant, i.e.Close[i] is Open[i], and ^ is the sign of degree. const - constant in this expression, i.e.i_sizeOfSequentialCorrectionBar * pt, standard mathematical notation, I did not make anything up. 2*bVar-1 is similar to±1, bVar here is any bool-variable. And 2*bVar-1 expression takes values not 0 and 1, but -1 and +1.≥ is MQL4 >=, also a standard mathematical notation.step is a step, i.e. step equals 1 in cnt++, and step equals -cnt in cnt=0. What else was unclear from notations?

gyfto, thanks for the explanation. But there is a point. Anyway, we cannot solve the problem by changing the output digit of the expression. At the moment, where zeroing occurs, the open and close prices must be "swapped". I.e. as I see it, the universality cannot be achieved. This is what I've been puzzling over.

Vadim wrote some time ago that putting a function inside the loop slows down the code by an order of magnitude. I wonder, does it apply only to cases when a function calculates its values at each loop iteration or to any other case? For example, until I finished writing the Expert Advisor, I rewrote the function that I was trying to simplify using the following scheme:

//+-------------------------------------------------------------------------------------+
//| Расчитываем количество идущих один за одним баров одного признака                   |
//+-------------------------------------------------------------------------------------+
int LastCandlesType(int directionMA)
{
   int cntUp,                                                                            // Счётчик идущих друг за другом свечей с..
       cntDn;                                                                                // .. требуемыми признаками
       
   for (int i=i_AnyBarsToHistory; i>=1; i--)
   {
      if (directionMA == CROSS_UP)
      {
         if ((Open[i] - Close[i]) >= i_sizeOfSequentialCorrectionBar * pt)              // Если бар соответствует требуемым признакам..
             cntDn++;                                                                     // .. прибавим 1 к счётчику

         if ((Close[i] - Open[i]) >= i_sizeOfTrandBar * pt)                             // Если бар, не соответствует основному признаку..
             cntDn = 0;                                                                   // .. счётчик обнуляем

         if (cntDn == i_sequentBarsСount)                                                   // Если cnt баров в подряд медвежьи..
             return (REQUIRED_SEQUENTIAL_BEARS_GOT);                                         // .. Выходим из функции
      }

      if (directionMA == CROSS_DN)
      {
         if ((Close[i] - Open[i]) >= i_sizeOfSequentialCorrectionBar * pt)              // Если бар соответствует требуемым признакам..
             cntUp++;                                                                     // .. прибавим 1 к счётчику

         if ((Open[i] - Close[i]) >= i_sizeOfTrandBar * pt)                             // Если бар, не соответствует основному признаку..
             cntUp = 0;                                                                   // .. счётчик обнуляем
     
         if (cntUp == i_sequentBarsСount)                                                   // Если cnt баров в подряд бычьи..
             return (REQUIRED_SEQUENTIAL_BULLS_GOT);                                         // .. Выходим из функции
      }
   }
}

I have madecntUp andcntDn counters different, because it mayhave one value at once and then anotherone when calculation bars are used in a cycle.And the counter can sum up one value and continue summing up another one. And if the variable is the same, the number of bars of one characteristic will be added to the counter of bars of another characteristic.

This is where the advice is needed, maybe it is better to summarize in some other way?

The matter is that the Expert Advisor is not a big one yet. I've got the wagons and this function. I have a feeling that instead of long, the owl sells and vice versa. I'm trying to understand how it can happen. Maybe there is something wrong here?

 

Hello, I started to learn mql4 and wrote a script that locks losing orders. I cannot solve one problem: The script locks all orders with a loss of -10, but it keeps locking them as long as the price is below -10. How to make locking happen only once? I.e. if the loss reaches -10, the script has locked that order and leaves it there even if the loss keeps increasing.

extern double StopLoss=150;

extern double Profit=-10;

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

//----
void start()
{
double profit=Profit;
double Lots=0;
for(int i=0;i<OrdersTotal();i++)
{
if(!OrderSelect(i,SELECT_BY_POS))
continue;

if(OrderType()==OP_BUY && OrderProfit()<Profit*Point)
Lots+=OrderLots();
if(OrderType()==OP_SELL && OrderProfit()<Profit*Point)
Lots-=OrderLots();


}
if(Lots>0)
OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point,0,NULL,Red);
if(Lots<0)
OrderSend(Symbol(),OP_BUY,-(Lots),Ask,3,Bid-StopLoss*Point,0,NULL,Blue);
//----
return(0);
}

 

Good afternoon.

Please look through the code, I want to calculate the average time of a trade ( pending order trading)

//+------------------------------------------------------------------+
//| Расчет среднего времени сделки            |
//+------------------------------------------------------------------+
double Sredneevremyasdelky(){
datetime Tsd=0; // общее время сделок
datetime Tsvsd=0; // среднее время сделки 
int n=0;
int orders=HistoryTotal();  // history orders total

        for(  int i=orders-1;i>=0;i--)
                  {
                 if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) 
                   { 
                        Print("Ошибка в истории!"); 
                        break;
                   }
                if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) 
                  continue;
                  n++;
                 Tsd=Tsd+(OrderCloseTime()-OrderOpenTime());
                   }
            if (n>0) Tsvsd=Tsd/n/60;


            return(Tsvsd);
         }

From order triggering to closing.

 
Stells:

Good afternoon.

Please look at the code, I want to calculate the average time of a trade (pending order trading)

From order triggering to closing.


Why do you calculate the average time in the cycle? Maybe it would be better to do it after the cycle. And it would be a good idea to set the calculation period
 
Vinin:

Why do you calculate the average time in the cycle? It's an unnecessary operation. Maybe it would be better to do it after the cycle. And it would be nice to set the calculation period
You took it out of the loop. How do you set the calculation period?
 
Stells:
Took it out of the cycle. How do I do the calculation period?

External variables e.g.
 

I get it, the date needs to be brought out somehow

Which function is it?

 
Stells:

I get it, the date needs to be brought out somehow

Which function is it?


Why complicate things when there's an extern
 

hoz ,I'll move in as soon asI'm free.

I have this question about optimization. I'm dealing with EMA algorithm. As you know, it's recursion which actually saves time. Referring to the original code:

...

Transferred tohttps://www.mql5.com/ru/forum/144691

 
gyfto:

hoz ,I'll move in as soon asI'm free.

I have this question about optimization. I'm dealing with EMA algorithm. As you know, it's recursion which actually saves time. Referring to the original code:

I took Matcad specifically to deal with weights generated by EMA:

By the way, now it's clear to me why the indicators that use EMA (which I think should be called a power function rather than an expotential one) use period of 14. So, why do I need to take the last weight so high when I can just wrap a power function if the next weight is more important than the previous one? I don't even ask anymore why to use recursion when the final weights after the end of recursion can be derived by a formula (see F(n,x) and y(n,x)).


Maybe you should open your own branch. Why do you need a "Beginner's Branch"?