Can somebody PLEASE help me with this problem?

 

I have been trying to debug my first EA for about 5 days now without success. I make small progress every now and then, but the EA still does not work correctly!

There are two things that i cannot figure out:

  1. The counter "i" stays on zero although I initialize it with the value 4 and then reduce it to 1. But when I print the value of the counter it is always zero and the EA does not go through the loop 4 times as instructed by the "for" loop. It just goes through once with every tick and then goes further through the program. Is there something wrong with my code? Is the "for" loop in the wrong place?
  2. I have a variable inside the loop (nBullRevBars) that increases its value by one every time it goes through the loop. All that happens now is that when a tick comes through (and the condition is true) the value is increased by 1 - although it is still the same bar! So depending on how many ticks come through the value can end up as anything. I want it to be increased only ONCE the condition is true for EVERY BAR - not tick! It seems that the Close and Open prices for the bar changes every time a tick comes through! I though the code would work only on bars.

I am inserting the code of this algorithm below and desperately need your help! (I left in all the Print functions through which I was trying to find out what is happening)

I also give an extract from the log file to show the value of the counter "i" == 0.

(Some other interesting thing is that although Close and Open is the same value the EA still found Close < Open to be true!)

14:33:30 2011.05.16 02:28 Turning Point New EURUSD,M1: Start all over
14:33:30 2011.05.16 02:28 Turning Point New EURUSD,M1: nBarHighNo is 8
14:33:30 2011.05.16 02:28 Turning Point New EURUSD,M1: nBarLowNo is 5
14:33:30 2011.05.16 02:28 Turning Point New EURUSD,M1: Highest Level is 1.4082
14:33:30 2011.05.16 02:28 Turning Point New EURUSD,M1: Lowest Level is 1.4074
14:33:30 2011.05.16 02:28 Turning Point New EURUSD,M1: nBearRevBars is 1
14:33:30 2011.05.16 02:28 Turning Point New EURUSD,M1: ClosePrice is 1.4077
14:33:30 2011.05.16 02:28 Turning Point New EURUSD,M1: Counter is 0
14:33:30 2011.05.16 02:28 Turning Point New EURUSD,M1: OpenPrice is 1.4077
14:33:30 2011.05.16 02:28 Turning Point New EURUSD,M1: Start with next section

         double HighestLevel;
         int nBarHighNo;
         double LowestLevel;
         int nBarLowNo;
         double BarTime;
                
         while(nBarHighNo != 5 || nBarLowNo != 5)
         break;
            {           
               Print("Start all over ");
               nBarHighNo = iHighest(NULL,0,MODE_HIGH,10,1);
               HighestLevel = High[nBarHighNo];
               nBarLowNo = iLowest(NULL,0,MODE_LOW,10,1);
               LowestLevel = Low[nBarLowNo];
               Print("nBarHighNo is ",nBarHighNo);
               Print("nBarLowNo is ", nBarLowNo);
               Print("Highest Level is ", HighestLevel);
               Print("Lowest Level is ", LowestLevel);
            }
                 
         if (nBarHighNo == 5)
            {
               
              for (i = 4; i >= 1; i--)
              RefreshRates();
                  if (Close[i] < Open[i])

                        {
                           nBullRevBars = nBullRevBars + 1;
                           int LowBar = iLowest(NULL,0,MODE_LOW,4,1);
                           dSellPrice = Low[LowBar]- (PipsOpenTrans * UsePoint);
                           Print("Sell Price is ", dSellPrice);
                           Print("nBullRevBars is ", nBullRevBars);
                           Print("ClosePrice is ", Close[i]);
                           Print("Counter is ", i);
                           Print("OpenPrice is ", Open[i]);
                        } 
                    else 
                        {
                           nBullRevBars = 0;
                           dSellPrice = 0.0;
                           Print("nBullRevBars is ", nBullRevBars);
                        }
                }
           
           else if (nBarLowNo == 5)
            {
               for (i = 4; i >= 1; i--)
               RefreshRates();
                   if (Close[i] > Open[i])
                        {
                           nBearRevBars = nBearRevBars + 1;
                           int HighBar = iHighest(NULL,0,MODE_LOW,4,1);
                           dBuyPrice = High[HighBar]+ (PipsOpenTrans * UsePoint);
                           Print("nBearRevBars is ", nBearRevBars);
                           Print("ClosePrice is ", Close[i]);
                           Print("Counter is ", i);
                           Print("OpenPrice is ", Open[i]);
                        } 
                   else 
                        {
                           nBearRevBars = 0;
                           dBuyPrice = 0.0;
                           Print("nBearRevBars is ", nBearRevBars);
                        }
               }
 
ernest02:


  1. The counter "i" stays on zero although I initialize it with the value 4 and then reduce it to 1. But when I print the value of the counter it is always zero and the EA does not go through the loop 4 times as instructed by the "for" loop. It just goes through once with every tick and then goes further through the program. Is there something wrong with my code? Is the "for" loop in the wrong place?


group your codes under for with these brackets { }

eg for (i = 4; i >= 1; i--)

{

all codes that belong to the for loop

}

 
ronaldosim:

group your codes under for with these brackets { }

eg for (i = 4; i >= 1; i--)

{

all codes that belong to the for loop

}



Thanks a lot Ronaldosim! Your assistance is GREATLY appreciated! The grouping has solved the problem with the counter!

Now all I still need is to solve the problem with the nBullRevBars which is now incremented 4 times for every tick! And i need it to be incremented once for every bar.

 

Close price is the price of the last recieved tick thats why it changes all the time during the formation of a bar, Open price is the first recieved tick of a bar so it doesnt change during formation of a bar

if you want something to happen only once during the formation of a bar you should use some code to detect if the bar is a new bar I do it like this

On global scope declare

datetime Time0;

then when you want to do something once during a bar do

if(Time0!=Time[0])
{Do stuff;
 Time0=Time[0];
}
 
  1. Why did you open a new posting when you already had one open?
  2. while(nBarHighNo != 5 || nBarLowNo != 5)
             break;
                {           
                   Print("Start all over ");
                   nBarHighNo = iHighest(NULL,0,MODE_HIGH,10,1);
                   HighestLevel = High[nBarHighNo];
                   nBarLowNo = iLowest(NULL,0,MODE_LOW,10,1);
                   LowestLevel = Low[nBarLowNo];
                   Print("nBarHighNo is ",nBarHighNo);
                   Print("nBarLowNo is ", nBarLowNo);
                   Print("Highest Level is ", HighestLevel);
                   Print("Lowest Level is ", LowestLevel);
                }
    equivalent code
    // while(nBarHighNo != 5 || nBarLowNo != 5)  break;
    // {           
    Print("Start all over ");
    nBarHighNo = iHighest(NULL,0,MODE_HIGH,10,1);
    HighestLevel = High[nBarHighNo];
    nBarLowNo = iLowest(NULL,0,MODE_LOW,10,1);
    LowestLevel = Low[nBarLowNo];
    Print("nBarHighNo is ",nBarHighNo);
    Print("nBarLowNo is ", nBarLowNo);
    Print("Highest Level is ", HighestLevel);
    Print("Lowest Level is ", LowestLevel);
    //            }
    There is no loop there and if there was it would be a infinite loop since there is no refreshRates nor sleeps
  3.            for (i = 4; i >= 1; i--)
                  RefreshRates();
                      if (Close[i] < Open[i])
    equivalent code
               for (i = 4; i >= 1; i--)  RefreshRates();
               if (Close[0] < Open[0])
    There is no loop there.
    for(...){ code }
 
WHRoeder:
  1. Why did you open a new posting when you already had one open?
  2. equivalent code There is no loop there and if there was it would be a infinite loop since there is no refreshRates nor sleeps
  3. equivalent code There is no loop there.
    for(...){ code }

I opened a new posting since I had changed the code a little and and had an additional new problem that I discovered. Also I did not receive any feedback for a couple of days so I thought people may be thinking my posting is either solved or old and they are concentrating on only the new postings.

Where I live there is nobody withing 100kms that can help me (that I am aware of) so I am so dependent on your help from this forum. I am therefore SO GRATEFUL for any assistance offered by people like you SDC, Ronaldosim, etc.

I call it a loop since it has to "loop" four (4) times through the code in brackets after the if statement while the condition is true.

I am not sure why you changed the Close[i] and Open[i] functions to Close[0] and Open[0]. Please explain if you don't mind.

(What I am trying to do is "loop" through the last four bars and see how many of their "Close" was lower than their "Open".)

I would also be very grateful if you could look at the problem that SDC is helping me to solve.

 
SDC:

Close price is the price of the last recieved tick thats why it changes all the time during the formation of a bar, Open price is the first recieved tick of a bar so it doesnt change during formation of a bar

if you want something to happen only once during the formation of a bar you should use some code to detect if the bar is a new bar I do it like this

On global scope declare

then when you want to do something once during a bar do


SDC thanks a million for that advice you gave about how to work with full bars.

I change the code to the example give below but it does not seem to give me the correct values. The values will go up to 80 or more where the maximum it is supposed to be is 4.

Is this code correct?

         if (nBarHighNo == 5)
            {
               
              for (i = 4; i >= 1; i--)
                 { 
                  RefreshRates();
                  if (Close[i] < Open[i] && BarTime != Time[i])

                        {
                           Print("BarTime1 is ", BarTime);
                           nBullRevBars = nBullRevBars + 1;
                           int LowBar = iLowest(NULL,0,MODE_LOW,4,1);
                           dSellPrice = Low[LowBar]- (PipsOpenTrans * UsePoint);
                           BarTime = Time[i];
                        }
 

Why not try:


if (nBarHighNo == 5)
            {
             nBullRevBars = 0;  
              for (i = 4; i >= 1; i--)
                 { 
                  RefreshRates();
                  if (Close[i] < Open[i] && BarTime != Time[i])

                        {
                           Print("BarTime1 is ", BarTime);
                           nBullRevBars = nBullRevBars + 1;
                           int LowBar = iLowest(NULL,0,MODE_LOW,4,1);
                           dSellPrice = Low[LowBar]- (PipsOpenTrans * UsePoint);
                           BarTime = Time[i];
                        }
 
SDC:

Close price is the price of the last recieved tick thats why it changes all the time during the formation of a bar, Open price is the first recieved tick of a bar so it doesnt change during formation of a bar

if you want something to happen only once during the formation of a bar you should use some code to detect if the bar is a new bar I do it like this

On global scope declare

then when you want to do something once during a bar do

What confuses me is that the only bar that has not been completed is the "0" (current) bar, where I can see why one has to deal with the ticks coming in. Why e.g. if i work on Close[1] do I still have to consider ticks coming in while the bar was formed in the past. We know which was the last tick before the code even starts up. I am not working with the current bar only historical bars. So if I reference Close[1] in my code it should know what the value of the last tick was without having to go through a tick by tick update.

Please clear this mystery up for me!

 
ernest02:

SDC thanks a million for that advice you gave about how to work with full bars.

I change the code to the example give below but it does not seem to give me the correct values. The values will go up to 80 or more where the maximum it is supposed to be is 4.

Is this code correct?



When I looked at my code again it tells me that BarTime will NEVER be equal to Time[i] and so has no real function.
 
rafaell:

Why not try:


Rafaell that was a BRILLIANT insight! It is still early days but it seemed to have SOLVED my problem! I am so HAPPY!
Reason: