I need help finding a coding walk-around for a problem.

 

Hello all! 

I'm working on a project and I have a question, the system I'm working on uses Tenkan-Sen crossover with price, the problem I'm facing is the following:

I want the system to take a trade every time the conditions are met (Bar opens below tenkan and closes above it) when the EA sends the order (buy), the next tick has the conditions too as the candle still has its open price below and the close above, if I use previous candle comparison to take the trade, the same thing will happen, as this situation relies on the "status" of the current bar.

I tough about making a function that sends the EA to sleep X amount of time depending on the time frame, but as Sleep function does not work on the Strategy Tester it will become useless as it would be impossible to optimize the EA.

I found this walk-around on a forum that allow me to run the code just once per bar:

//+------------------------------------------------------------------+
//|                                                       Demo-1.mq4 |
//|                                                    Luca Spinello |
//|                                https://mql4tradingautomation.com |
//+------------------------------------------------------------------+
#property copyright "Luca Spinello"
#property link      "https://mql4tradingautomation.com"
#property version   "1.00"
#property strict
datetime LastActiontime;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
//---
//Comparing LastActionTime with the current starting time for the candle
if(LastActiontime!=Time[0]){
//Code to execute once in the bar
Print("This code is executed only once in the bar started ",Time[0]);
LastActiontime=Time[0];
}
}
//+------------------------------------------------------------------+

(Full explaination of the code here: http://mql4tradingautomation.com/execute-action-once-per-bar-mql4/)

I'm using this walk-around, but I still want to figure out other ways to avoid opening massive ammount of trades when I have a condition similar to the one I'm facing now. If the EA would only take one trade at the time, it would be very easy, but as this is not that case, I can think on other situations where using this walkaround is just not enough, for example, if I would need some complex conditions to be met, like if X indicator above Y indicator and K indicator below L indicator then send an order when the price crosses a MA regardless if the close price of the current candle ends above or below it.

In that case, I would have to either stick a massive part of the code on one line or waste resourses verifying a lot of conditions and use this walk-around on the OrderSend() part.

Any ideas on how to get the same results but with a more flexible coding?

 
FernandoBorea: the next tick has the conditions too
You are looking at a signal. Act on a change of signal.
          MQL4 (in Strategy Tester) - double testing of entry conditions - Strategy Tester - Expert Advisors and Automated Trading - MQL5 programming forum
Reason: