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

 
Nerd Trader:
Why does isCrossing() not return 2??? The isCrossing() itself has a second if entry, but then there is no if entry in OnTick() when isCrossing() == 2. What a load of nonsense...

Both conditions fit...so you get the first result

 
MakarFX:

Both conditions fit...so you get the first result

Only fit when Bid == ma. Fixed <= to < (also for >=), but nothing changes.
 
Nerd Trader:
Why does isCrossing() not return 2??? The isCrossing() itself has a second if entry, but then there is no if entry in OnTick() when isCrossing() == 2. What a load of nonsense...

For some reason I don't think it returns 1 either.

This condition

if(g_barTime < iTime(NULL,g_timeFrame,0)// и дальше

Tells you that a new bar has been opened. On the first tick of the bar open == high == low == close and == Bid - hence the condition

 && high > ma && Bid <= ma)

or

 && low < ma && Bid >= ma)
cannot be fulfilled...

There was a question today about the condition if(i != i) These conditions are approximately the same.

 
OnTick() as it is now in my editor:
void OnTick()
  {
/*     if(isCrossing() == 1){          
      Print("enter to '1. if (isCrossing)'");
        if(shouldBars(1))         
          Print("angulation is ", shouldAngulation(1));            
    } */

    if(isCrossing() == 2){
      Print("enter to '2. if (isCrossing)'");
      if(shouldBars(2))
        Print("angulation is ", shouldAngulation(2));
        //if(shouldAngulation(2))
          //OpenSell();
    }

   
  }
If the first block is commented out, the second block is executed. I don't know where the error is.

P.S.
with the isCrossing() function left everything as it is.
 
Alexey Viktorov:

For some reason I don't think it returns 1 either.

This condition

tells about the opening of a new bar. On the first tick of the bar open == high == low == close and == Bid - hence, the condition

or

cannot be fulfilled...

There was a question today about the condition if(i != i) These conditions are about the same.

"1" returns, did code debugging in MetaEditor.

Further, even if everything is equal, it's only on the first tick.

 
Nerd Trader:

"1" returns, did code debugging in MetaEditor.

Further, even if everything is equal, it's only on the first tick.

Well, yes, I was wrong. The new bar will be only after the whole condition is fulfilled.

Try to replace high and low by open. Maybe it won't affect the strategy too much.

 
Nerd Trader:

"1" returns, did code debugging in MetaEditor.

Further, even if everything is equal, it is only on the first tick.

It works like this

//--- input parameters
input int                  g_maPeriod  = 14;
input int                  g_maShift   = 1;
input ENUM_APPLIED_PRICE   g_maPrice   = PRICE_CLOSE;  //Applied price
input ENUM_MA_METHOD       g_maMethod  = MODE_SMA;    //Method Ma
input ENUM_TIMEFRAMES      g_timeFrame = PERIOD_CURRENT;   //Timeframe for calculate
datetime g_barTime=0;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(g_barTime != iTime(NULL,g_timeFrame,0))
     {
      if(isCrossing()==1)
        {
         Print("enter to '1. if (isCrossing)'"); g_barTime = iTime(NULL,g_timeFrame,0);
        }
      if(isCrossing()==2)
        {
         Print("enter to '2. if (isCrossing)'"); g_barTime = iTime(NULL,g_timeFrame,0);
        }
     }
  }
//+------------------------------------------------------------------+
int isCrossing()
  {  
   int res=0;
   double low = iLow(Symbol(), g_timeFrame, 1);
   double high = iHigh(Symbol(), g_timeFrame, 1);
   double ma = iMA(_Symbol, g_timeFrame, g_maPeriod, g_maShift, g_maMethod, g_maPrice, 0);

   if(high > ma && Bid <= ma) res=1;
  
   if(low < ma && Bid >= ma) res=2;
   return (res);
  }
//+------------------------------------------------------------------+
2021.08.25 09:24:56.629 2021.08.20 23:50:00  Nerd Trader EURUSD,M5: enter to '1. if (isCrossing)'
2021.08.25 09:24:56.624 2021.08.20 23:45:00  Nerd Trader EURUSD,M5: enter to '2. if (isCrossing)'
2021.08.25 09:24:56.592 2021.08.20 21:35:00  Nerd Trader EURUSD,M5: enter to '2. if (isCrossing)'
2021.08.25 09:24:56.582 2021.08.20 21:05:00  Nerd Trader EURUSD,M5: enter to '1. if (isCrossing)'
2021.08.25 09:24:56.566 2021.08.20 20:20:00  Nerd Trader EURUSD,M5: enter to '1. if (isCrossing)'
2021.08.25 09:24:56.554 2021.08.20 18:50:00  Nerd Trader EURUSD,M5: enter to '2. if (isCrossing)'
2021.08.25 09:24:56.553 2021.08.20 18:45:00  Nerd Trader EURUSD,M5: enter to '2. if (isCrossing)'
2021.08.25 09:24:56.553 2021.08.20 18:40:00  Nerd Trader EURUSD,M5: enter to '1. if (isCrossing)'
 
MakarFX:

It works like this.


I did this and it all works, only messages from isCrossing() are duplicated, because the f-e is called twice for one tick.

void OnTick()
  {
   if(BarTime != iTime(NULL,0,0)){

     if(isCrossing == 1){
       ...
     }

     if(isCrossing == 2){
       ...
     }
     BarTime = iTime(NULL,0,0); 
  }

int isCrossing()
  {  
    ...

    if(high > ma && Bid <= ma){
      Print("Crossing down");
      return 1;
    } 
  
    if(low < ma && Bid >= ma){
      Print("Crossing up");
      return 2;
    }
   return 0;
  }


So I left it as it was, only now everything from isCrossing() is returned to variable. Nothing is duplicated and everything works. Thanks all :)

void OnTick()
  {
    int iCrossing = isCrossing();

    if(iCrossing == 1){
       ...
    }

    if(iCrossing == 2){
       ...
    }   
  }
 
MakarFX:
2021.08.25 09:24:56.629 2021.08.20 23:50:00  Nerd Trader EURUSD,M5: enter to '1. if (isCrossing)'
2021.08.25 09:24:56.624 2021.08.20 23:45:00  Nerd Trader EURUSD,M5: enter to '2. if (isCrossing)'
2021.08.25 09:24:56.592 2021.08.20 21:35:00  Nerd Trader EURUSD,M5: enter to '2. if (isCrossing)'
2021.08.25 09:24:56.582 2021.08.20 21:05:00  Nerd Trader EURUSD,M5: enter to '1. if (isCrossing)'
2021.08.25 09:24:56.566 2021.08.20 20:20:00  Nerd Trader EURUSD,M5: enter to '1. if (isCrossing)'
2021.08.25 09:24:56.554 2021.08.20 18:50:00  Nerd Trader EURUSD,M5: enter to '2. if (isCrossing)'
2021.08.25 09:24:56.553 2021.08.20 18:45:00  Nerd Trader EURUSD,M5: enter to '2. if (isCrossing)'
2021.08.25 09:24:56.553 2021.08.20 18:40:00  Nerd Trader EURUSD,M5: enter to '1. if (isCrossing)'
Saw now that you've added a log. Here you have duplicate messages too :)
 

can you please explain the difference between the functions?

for (int j = OrdersHistoryTotal()-1; j >= 0; j--)

{

if(OrderSelect(j, SELECT_BY_POS,MODE_HISTORY))

и

int i=OrdersHistoryTotal();

for(int pos=0; pos<i; pos++)

{

if(OrderSelect(pos, SELECT_BY_POS,MODE_HISTORY))