You need a way to detect a new candle. Here is part of something I made earlier this week. I am new to MQL5, so there is probably better ways to do this.
I used OnTick() to check for a new candle (not a for loop), but you should be able to get something to work with Bars().
// # of candles since the last trade int SinceLastTrade = 0; // Set up int to detect new candles int CurBar = 0; int PrevBar = 0; void OnTick() { //Get the Current Bar # CurBar = Bars(_Symbol,_Period); //Check to see if there is a new candle if(CurBar>PrevBar) { // Update the Previous Bar # PrevBar=CurBar; //Add 1 to Candles SinceLastTrade SinceLastTrade++;
You need a way to detect a new candle. Here is part of something I made earlier this week. I am new to MQL5, so there is probably better ways to do this.
I used OnTick() to check for a new candle (not a for loop), but you should be able to get something to work with Bars().
i get it. I have a similar formula for new Candle but i still can t figure out how to check a new candle every time it comes in, and repeat that 20 times, so 20 different candles.
Then if after 20 new candles the criteria is not satisfied it quits.
Can you help me?
int count=0; datetime time; //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if(time!=iTime(Symbol(),PERIOD_CURRENT,0)) { count++; time=iTime(Symbol(),PERIOD_CURRENT,0); } if(count==20) { Alert("20 new candles !"); count=0;// reset counter } } //+------------------------------------------------------------------+
thanks for your answer Marco.
Would that work for an EA?
As far as I know onTick is an indicator function, can it be used in an EA?
Don't rely on counting bars because that can go wrong in a lot of ways and won't recover should the EA encounter troubles. Instead you should be looking for the signal to have happened 15-20 bars back (from the current bar) and if the signal is found then run further validation. Here is an example of MA cross (UP) that happened with 15-20 bars of confirmation (where the fast MA stayed above the slow MA and didn't cross back down).
bool buy_signal() { for (int i=14; i<20; i++) { //look for cross 15 to 20 bars back if (ma20.Main(i + 1) < ma50.Main(i + 1) && ma20.Main(i) >= ma50.Main(i)) { //crossed up bool validated = true; for (int j=i-1; j>=0; --j) { //potential signal found count back to current bar if (ma20.Main(j) < ma50.Main(j)) { //crossed back down, signal invalid validated = false; break; } } return validated; } } return false; }

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I have an EA which is based on 3 steps.
Once the first 2 steps satisfied i need to wait for the 3rd step to come along.
Ideally i would wait 15/20 bars for the 3rd step to show up.
The problem that i have is: how do i check for the last 15-20 bars if the 3rd signal has been satisfied?
I am using a function:
As you can see from above I m thinking of using a loop but i m not sure how to apply it to new last 15/20 bars coming in.
Thanks