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?