Problem with a loop on MQL4. Doesnt support more than 2 instructions inside.

 

I am making a simple script, looking for pivots.

My code runs just just fine untill a loop shows up.

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

//| Script program start function                                    |

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

void OnStart()
  {
int Vela_T=180; //testigo para r vela por vela testeando altos y bajo
int ths=0,tls=0;
bool A_B=0;
double PivotAlto[1000];//con i
double PivotBajo[1000];//con j
int PivotAltoVela[1000];//con i
int PivotBajoVela[1000];//con j
double PivotAltoPro=0,PivotBajoPro=0;
int PivotAltoVelaPro=0,PivotBajoVelaPro=0;
int i=0,j=0;

//////////determinar el primer  high

Print ("total de velas en gráfico=",Bars);
for (Vela_T=180;(PivotAltoPro==0);Vela_T=Vela_T-1)
{
   if (High[Vela_T]>High[Vela_T+2] && High[Vela_T]>High[Vela_T+1] && High[Vela_T]>High[Vela_T-1] && High[Vela_T]>High[Vela_T-2]) 
   {PivotAltoPro=High[Vela_T];PivotAltoVelaPro=Vela_T;
   Print ("PivotAltoPro=",PivotAltoPro," en la Vela ",Vela_T);
   break;}
}

//////////determinar el primer  low

for (Vela_T=180;PivotBajoPro==0;Vela_T=Vela_T-1)
{

   if (Low[Vela_T]<Low[Vela_T+2] && Low[Vela_T]<Low[Vela_T+1] && Low[Vela_T]<Low[Vela_T-1] && Low[Vela_T]<Low[Vela_T-2]) 
   {PivotBajoPro=Low[Vela_T];PivotBajoVelaPro=Vela_T;
   Print ("PivotBajoPro=",PivotBajoPro," en la Vela ",Vela_T);
   break;}
}

//Determinar el primer Pivot
if (PivotAltoVelaPro>PivotBajoVelaPro)
    {PivotAlto[i]=PivotAltoPro;PivotAltoVela[i]=PivotAltoVelaPro;Print("Primera Vela Pivot alto=",PivotAltoVela[i]," Primer Precio Pivot Alto=",PivotAlto[i]); i=i+1;}

else{PivotBajo[j]=PivotBajoPro;PivotBajoVela[j]=PivotBajoVelaPro;Print("Primera Vela Pivot bajo=",PivotBajoVela[j]," Primer Precio Pivot Bajo=",PivotBajo[j]);j=j+1;}

if (PivotAltoVelaPro>PivotBajoVelaPro) {Vela_T=PivotBajoVelaPro-1;A_B=0;} else {Vela_T=PivotAltoVelaPro-1;A_B=1;}
Print ("Vela_T=",Vela_T);


//Cut the code here and you will see the difference..


//chequear resto de velas hasta la vela 4 

while (Vela_T>4)
{
//Se evalúa el high y el low de la Vela Testigo (Vela_T)
  if (High[Vela_T]>High[Vela_T+2] && High[Vela_T]>High[Vela_T+1] && High[Vela_T]>High[Vela_T-1] && High[Vela_T]>High[Vela_T-2]) 
  {ths=Vela_T;}

  else
  {ths=0;}


  if (Low[Vela_T]<Low[Vela_T+2] && Low[Vela_T]<Low[Vela_T+1] && Low[Vela_T]<Low[Vela_T-1] && Low[Vela_T]<Low[Vela_T-2]) 
  {tls=Vela_T;}

  else
  {tls=0;}

   
//Si la Vela_T tuvo un high pivote se asigna el último PivotBajo[j]

  if (ths>0)
   {  if (A_B==0  &&  High[Vela_T]>PivotBajoPro)
     {PivotBajo[j]=PivotBajoPro;PivotBajoVela[j]=PivotBajoVelaPro;
       PivotAltoPro=High[Vela_T];PivotAltoVelaPro=Vela_T;A_B=1;
      Print ("PivotBajo[",j,"]=",PivotBajo[j],"PivotBajoVela[",j,"]=",PivotBajoVela[j]);
      j=j+1;
       }
    //si aparece otro pivot alto despues de un alto, se sigue subiendo el ñultimo high   

    else   
     if (A_B==1  && High[Vela_T]>PivotAltoPro)
    {PivotAltoPro=High[Vela_T];PivotAltoVelaPro=Vela_T;}

    }

      
//Si la vela _T tuvo un low pivotes se asigna el úlitmo PivotAlto[]

   if (tls>0)
   {  if (A_B==1  &&  Low[Vela_T]<PivotAltoPro)
      {PivotAlto[i]=PivotAltoPro;PivotAltoVela[j]=PivotAltoVelaPro;
      PivotBajoPro=Low[Vela_T];PivotBajoVelaPro=Vela_T;A_B=0;
      Print ("PivotAlto[",i,"]=",PivotAlto[i],"PivotAltoVela[",i,"]=",PivotAltoVela[i]);
     i=i+1;
     }

  //si aparece otro pivot bajo despues de un bajo, se sigue bajando el último low   

    else
     if (A_B==0  && Low[Vela_T]<PivotBajoPro)
      {PivotBajoPro=Low[Vela_T];PivotBajoVelaPro=Vela_T;}


   }

 
Vela_T=Vela_T-1;//se pasa a la proxima vela a evaluar
}


//at the end, there will be a couple of arrays PivotAlto[] y PivotBajo[]  and  PivotAltoVela[] y PivotBajoVela[] with bars shifts and prices on each pivot

}

The aim of the code is create arrays with the pivots values. 

The first and the second loops "for" runs with no problem (as is meant to be...)

But next loop make an erratic behavior.

The debugger runs perfect. But script does'nt work on the chart.

The massage on expert log is quite weird, it doesnt say "initialized..." (see images attached)


Tryng to fix it:

- I tried getlasterror every where in the code, Nothing. last error is always 0

- If I cut the  code just before the while loop, no problem. 

- I changed "while" for "for". Nothing. same error. 

- If I leave only one instruction inside the loop, no problem

- if I leave more than two instruction inside the loop, puff, doesnt work

- I changed computers, script name, shorten variables names. Nothing seems to work


Thanks for your help

Files:
 
Miguel Vera: The debugger runs perfect. But script does'nt work on the chart.

The massage on expert log is quite weird, it doesnt say "initialized..."

  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your (original) post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless.

  3. Why did you post your MT4 script question in the Root / MT5 Indicators section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  4. //chequear resto de velas hasta la vela 4 
    while (Vela_T>4)
    You never change the variable, so it's likely an infinite loop. Go back to the debugger.
 
whroeder1:
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your (original) post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless.

  3. Why did you post your MT4 script question in the Root / MT5 Indicators section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  4. You never change the variable, so it's likely an infinite loop. Go back to the debugger.

i really appreciate your time helping on this. I am very sorry for posting whitout knowing the rules accurately.

If I find the way to move my post or edit it, I will.

In the other hand, the variable Vela_T is modified at the end of all instructions of the while loop.

I'll take your advice anyway, and will re-check those lines.

If you wish, you can run the code in your PC and see if you get the same results.

Thanks a lot!!!

 

solved.

thanks

 
How do you see what's going on with that indentation style?