Checking for price retrace for the next "N" Bars.

 

Hello. I have been trying multiple ways to do this and read a lot of topics but still cannot get it working. 

So I have been trying to make EA that checks for 3 different signals and opens an order once all 3 of them are the same. I run into a problem with the last signal, which is supposed to check for price retrace for the next "N" (external) Bars. 

Here is the code:

extern int bar_count = 3;
void start()
{
   static string Sig_1 = "BUY", Sig_2 = "BUY", Sig_3, Action;
   static int counter;
   static bool DoCycle;
   DoCycle = false;           // Do not do cycle if next conditions are not met
   // ... here would be recalculation for signals Sig_1 and Sig_2 
   // If this Bar and both signals Sig_1 and Sig_2 are equal
   if ( Fun_New_Bar() && Sig_1 != "HOLD" && Sig_1 == Sig_2)   
      {  
       counter = 0;           // Then reset bar counter to 0
       DoCycle = true;        // And assign true value to cycle execution 
       Action = "HOLD";       // And reset Action value
      }
   // Cycle
   if(DoCycle)
   {
      while(!IsStopped())
      {
         // Count how many bars have been passed
         counter = Bar_Counter(counter);  
         // Check the signal value
         Sig_3 = CheckBackTrace();     
         // Print the values to check results
         Print("counter = ", counter, "; Sig_3 = ", Sig_3); 
         // Break cycle if specified number of bars have been passed
         if(counter >= bar_count) 
            {
             Print("Cycle terminated because ", bar_count, " bars passed"); 
             break;
            }
         // Break cycle if Signals match   
         if(Sig_3 == Sig_2) 
            {
             Print("Cycle terminated because ", Sig_3, " signal detected"); 
             Action = Sig_3;
             break;
            }
      }
   // Here would be buy or sell orders accordingly to Action variable
   }
   Print("We are now outside the cycle and returning to start");
}
// -------------------------------------
// New Bar function
// -------------------------------------
bool Fun_New_Bar()
{
   static datetime New_Time = 0;
   bool New_Bar = false;
   if (New_Time!= Time[0])
      {
         New_Time = Time[0];
         New_Bar = true;
      }
   return(New_Bar);
}
// -------------------------------------
// Bar counter function
// -------------------------------------
int Bar_Counter(int a)
{
   static datetime Time0 = 0;
   if (Time0 != Time[0])
      {
         Time0 = Time[0];
         a++;
      }
   return(a);
}
// -------------------------------------
// Price and MA5 backtrace function
// -------------------------------------
string CheckBackTrace()
{
   // Some moving average value
   double FMA_Current = iMA(NULL, 0, 250, 0, 0, PRICE_CLOSE, 0);
   string Sig_back = "HOLD";
   // And some criteria for signal
   if ((Ask - FMA_Current) <= (2 * MarketInfo(Symbol(), MODE_TICKSIZE))) Sig_back = "BUY";
   if ((FMA_Current - Bid) <= (2 * MarketInfo(Symbol(), MODE_TICKSIZE))) Sig_back = "SELL";
   return(Sig_back);
}

The question is - why is my Bar_Counter() function not increasing the value of "counter" variable when it is inside the cycle. It works fine while it is outside of the cycle but in the code above it keeps returning only value 1 and does not increase it. Am I missing something about cycles here?

How could I work around this problem? Or is there a way to write it without such a cycle which would work similar for "N" bars on each tick without returning to start unless time is exceeded or conditions met?

 
Kristians Simanis:

Hello. I have been trying multiple ways to do this and read a lot of topics but still cannot get it working. 

So I have been trying to make EA that checks for 3 different signals and opens an order once all 3 of them are the same. I run into a problem with the last signal, which is supposed to check for price retrace for the next "N" (external) Bars. 

Here is the code:

The question is - why is my Bar_Counter() function not increasing the value of "counter" variable when it is inside the cycle. It works fine while it is outside of the cycle but in the code above it keeps returning only value 1 and does not increase it. Am I missing something about cycles here?

How could I work around this problem? Or is there a way to write it without such a cycle which would work similar for "N" bars on each tick without returning to start unless time is exceeded or conditions met?

I apologize but your code is spaghetti code. I didn't examine it much but I can write some advices you should follow.
  • Stop using obsolete function start() and use OnTick() or OnTimer() instead
  • A while loop inside if condition. In this case you can write the condition into while condition. See below
  • Your counter is increased only once in the while loop because then the condition Time0!=Time[0] is always false
  • Maybe you should avoid using break in a loop. And write the condition into while()
while(DoCycle && !IsStopped())

or

while(DoCycle && counter >= bar_count && Sig_3 == Sig_2 && !IsStopped())
 
Petr Nosek:
I apologize but your code is spaghetti code. I didn't examine it much but I can write some advices you should follow.
  • Stop using obsolete function start() and use OnTick() or OnTimer() instead
  • A while loop inside if condition. In this case you can write the condition into while condition. See below
  • Your counter is increased only once in the while loop because then the condition Time0!=Time[0] is always false
  • Maybe you should avoid using break in a loop. And write the condition into while()

Sorry about that. Probably should have mentioned that I am new at this. 

  • Changed start() to OnTick();
  • Changed the loop as you mentioned (below);
  • Added RefreshRates() at the beginning of the loop and now counter increases as it was intended;
  • I am now using 1 break in the loop in case if the Signals match because then I would like to assign that signal value to Action variable which will be used for OrderSend() and get out of the loop. 

Here is the updated version of loop:

while(DoCycle && !IsStopped() && counter <= bar_count)
      {
         RefreshRates();
         // Count how many bars have been passed
         counter = Bar_Counter(counter);  
         // Check the signal value
         Sig_3 = CheckBackTrace();     
         // Print the values to check results
         Print("counter = ", counter, "; Sig_3 = ", Sig_3); 
         // Break cycle if Signals match   
         if(Sig_3 == Sig_2) 
            {
             Print("Cycle terminated because ", Sig_3, " signal detected"); 
             Action = Sig_3;
             break;
            }
      }

1 more question arises though: how could I make this loop recalculate on each tick instead of continuous recalculations (see below)?



2018.10.11 16:09:03.083 Test_Cycles_2018_10_09 EURUSD,M1: counter = 2; Sig_3 = SELL
2018.10.11 16:09:03.083 Test_Cycles_2018_10_09 EURUSD,M1: counter = 2; Sig_3 = SELL
2018.10.11 16:09:03.083 Test_Cycles_2018_10_09 EURUSD,M1: counter = 2; Sig_3 = SELL
2018.10.11 16:09:03.083 Test_Cycles_2018_10_09 EURUSD,M1: counter = 2; Sig_3 = SELL
2018.10.11 16:09:03.083 Test_Cycles_2018_10_09 EURUSD,M1: counter = 2; Sig_3 = SELL
2018.10.11 16:09:03.083 Test_Cycles_2018_10_09 EURUSD,M1: counter = 2; Sig_3 = SELL
2018.10.11 16:09:02.842 Test_Cycles_2018_10_09 EURUSD,M1: counter = 1; Sig_3 = SELL
2018.10.11 16:09:02.842 Test_Cycles_2018_10_09 EURUSD,M1: counter = 1; Sig_3 = SELL
2018.10.11 16:09:02.842 Test_Cycles_2018_10_09 EURUSD,M1: counter = 1; Sig_3 = SELL

And thank you for the advice.

 

Kristians Simanis:

1 more question arises though: how could I make this loop recalculate on each tick instead of continuous recalculations (see below)?



What do you mean by this? This loop (if it is inside the OnTick function) is recalculated on each tick.

The best way to achieve your goal is using bool function for each signal (in your case three signals=>three functions) and then use them into OnTick function. You have to decide if you want check the signals every tick or just at new bar event.

 
Petr Nosek:

What do you mean by this? This loop (if it is inside the OnTick function) is recalculated on each tick.

The best way to achieve your goal is using bool function for each signal (in your case three signals=>three functions) and then use them into OnTick function. You have to decide if you want check the signals every tick or just at new bar event.

I specifically added Print(...) statement inside loop to see how often it executes and as I have shown in previous response, it executes multiple times per millisecond, which is not OnTick().

Maybe I am going at this the wrong way but since I want to calculate Sig_1 once per bar and Sig_2 once per tick and at the moment they both match I want to check for Sig_3 for the next "N" bars.

I made the function run OnTick() instead of simple start() but it seems from the Print(...) results that it does not help and once it goes into loop it does not wait for new tick to recalculate but instead recalculates continuously (as soon as previous iteration is done, not considering whether or not new tick has occurred). 

 
Kristians Simanis:

I specifically added Print(...) statement inside loop to see how often it executes and as I have shown in previous response, it executes multiple times per millisecond, which is not OnTick().

Maybe I am going at this the wrong way but since I want to calculate Sig_1 once per bar and Sig_2 once per tick and at the moment they both match I want to check for Sig_3 for the next "N" bars.

I made the function run OnTick() instead of simple start() but it seems from the Print(...) results that it does not help and once it goes into loop it does not wait for new tick to recalculate but instead recalculates continuously (as soon as previous iteration is done, not considering whether or not new tick has occurred). 

The function OnTick is executed on every new tick (if it can). And the while loop (inside the OnTick) is also executed once on tick but the code inside the while loop is executed until the loop is ended.

For your purpose see the schema below. It could be something like this:

void OnTick()
  {
   if(IsNewBar()) resultSignal1=CheckSignal1(...);
   resultSignal2=CheckSignal2(...);
   if(resultSignal1==resultSignal2 && resultSignal1==CheckSignal3(...))
     {
      do something
     }
  }

bool IsNewBar()
  {
   ...
  }

string CheckSignal1(...)
  {
   ...
  }

string CheckSignal2(...)
  {
   ...
  }

string CheckSignal3(...)
  {
   ...
  }
Reason: