You can't know when a candle closes. Only when a new tick arrives that starts a new bar is the old bar closed.
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
MT4: New candle - MQL4 programming forum #3 (2014)
MT5: Accessing variables - MQL4 programming forum #3 (2022)
I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
Running EA once at the start of each bar - MQL4 programming forum (2011)
Try this code example .
The code comments will guide you through it .
#property strict //first you need to know the latest time of the latest bar datetime barStamp=0; int _test_ticket=-1;//a test ticket int OnInit() { //reset the barstamp barStamp=0; /*if you want to ignore the first bar that already forms when the ea is attached on the chart set barStamp to : barStamp=Time[0]; */ _test_ticket=OrderSend(_Symbol,OP_BUY,0.01,Ask,1000,0.0,0.0,NULL,0,0,clrWhite); return(INIT_SUCCEEDED); } /* theres 2 variations of this : a.you want to check each candle that forms as the trade evolves and if you find something opposite , close the trade b.you are only interested in the candle the trade was opened in or , some candles after that So lets construct a function */ bool ShouldClose(int ticket,//your order's ticket int max_candles){ /* What is max_candles ? lets say your trade has opened in bar 6 if max candles is 0 , then you are only interested in the closing of bar6 if max candles is 1 , then you are interested in 6 and 5 (the next one in time to the right) if max candles is -1 ,then you are checking unlimited candles Step 1 : get the trade open time */ if(OrderSelect(ticket,SELECT_BY_TICKET)){ /* we are selecting by ticket so we check if the trade is open */ if(OrderCloseTime()==0&&(OrderType()==OP_BUY||OrderType()==OP_SELL)){//and we also restrict to buy / sell orders //we snatch the time this trade opened datetime trade_open_time=OrderOpenTime(); //we find the candle it opened in int candle_opened=find_bar(trade_open_time); /* now we need to consider some additional stuff. lets assume our trade opens mid current candle (candle[0] the live one) the first time this trade is cheked - assuming we only check on new bars - will be when the candle becomes [1] So , there we have our first filter if the candle_opened is 0 , skip it We also notice that if we only want to check the candle that the order opened in (i.e. candle[1] for a valid example) the distance is 0 . Meaning trade opened in candle[1] the market is on candle[0] , technically that is one bar after the opening but we start from [1] so the distance is 0 So , our second filter measures the candles elapsed since the trade's candle and we subtract 1 so candles elapsed from [1] is 0 candles elapsed from [2] is 1 etc and finally we will only be checking candle [1] direction as we are accessing this function only on new candle formations So here goes our filter : */ if(candle_opened>0&&(((candle_opened-1)<=max_candles)||max_candles==-1)) { /* the filter in other words says if the trade is now on candle 1 + and the distance of the candle we check [1] from the candle the trade opened in is within the max_candles allowed OR we don't care (-1) then enter the check */ //and what is the check ? //if the trade is a buy and the candle[1] is bearish , kill it if(OrderType()==OP_BUY&&Open[1]>Close[1]){ return(true);//return true because the question is "should the trade close?" } //if the trade is a sell and the candle[1] is bullish , kill it else if(OrderType()==OP_SELL&&Close[1]>Open[1]){ return(true); } } } } return(false); } //this is the function that finds the candle within which the trade (or a time) belongs to int find_bar(datetime time){ int found=-1; for(int i=0;i<Bars-2;i++){ if(Time[i]<=time){ found=i; break; }} return(found); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //and the check code //if we get a new bar if(Time[0]>barStamp) { //store the new barstamp barStamp=Time[0]; //check a trade bool close_it=ShouldClose(_test_ticket,0); if(close_it){ OrderClose(_test_ticket,0.01,Bid,1000,clrWhite); } } }
int OnInit(){ ⋮ _test_ticket=OrderSend(…don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
This is an example William .
😅
Yes! There are many examples in the CodeBase. Choose one and study it. Then make your own adaptations.
Of course i know coding and codebase source, only i couldn't find a proper example from more than 3-5k examples in codebase thats why i asked. So if you are really willing to help, please reply with a helpful solution like Lorentzos Roussos did.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
PS: if iClose-iOpen>0 it will be bullish candle, if <0 it will be bearish candle
So i need to get info when trade candle closes. Do you suggest any idea how to get this info?
Waiting for your suggestions, many thanks.