Please post in the correct section in future.
I will move this to the MQL4 and Metatrader 4 section.
Keith Watford:
Please post in the correct section in future.
I will move this to the MQL4 and Metatrader 4 section.
sorry... and thanks
bool CheckTradeInBar() { if(OrdersTotal()>0) { for(int t=0; t<=OrdersTotal(); t++) { if(OrderSelect(t,SELECT_BY_POS,MODE_TRADES)==true) { //if(OrderSymbol() != Symbol()) continue; //No need for this line as you check the symbol in the following line. if(OrderSymbol() == Symbol()) { if( (TimeDayOfYear(OrderOpenTime()) == TimeDayOfYear(TimeCurrent())) && (OrderOpenTime() > Time[0]) ) { return false; //You return false when there is a trade opened during the lifetime of the bar ??????? Print("Trade already opened in current bar"); //This won't print as you have already returned t++; //WHY???? } else //if( OrderOpenTime() < Time[0] ) { return true; //You are returning true if a checked trade did not open during the bar, you need to check the remaining trades. //You should return nothing here and return where I have indicated lower down. } } } } } // You need to return something here probably true }
You don't need all the TimeDayOfYear stuff.
Sorry, I re-formatted your code to how I prefer, personal choice, I know.
Keith Watford:
You don't need all the TimeDayOfYear stuff.
Sorry, I re-formatted your code to how I prefer, personal choice, I know.
Perfect. It worked. Thank you.
Just typed, not compiled, not tested.
bool CheckTradeInBar(){ for(int t=OrdersTotal()-1; t>=0; --t) if( && OrderSelect(t,SELECT_BY_POS) // && OrderMagicNumber == MN && OrderSymbol() == Symbol() ) if( OrderOpenTime() >= Time[0]) ) return true; // Order opened this bar. return false; }Just typed, not compiled, not tested.
Keith Watford:
You don't need all the TimeDayOfYear stuff.
Sorry, I re-formatted your code to how I prefer, personal choice, I know.
this is what i landed on. I tested it already and it works. Thanks again
bool CheckTradeInBar() { if(OrdersTotal()>0) { for(int t=0; t<=OrdersTotal(); t++) { if(OrderSelect(t,SELECT_BY_POS,MODE_TRADES)==true) { if(OrderSymbol() == Symbol()) { if( OrderOpenTime() > Time[0] ) { Print("Trade already opened in current bar"); return false; } //Return false when there is a trade opened during the lifetime of the bar } } } } return true; //Return true when there is no trade opened during the lifetime of the bar }

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Dear all,
I wrote an EA that allows a total of 5 trades to be opened at one time. However, I only want one trade to open per bar although not necessarily at the beginning of the bar. Therefore, the code written here (https://www.mql5.com/en/forum/216567) or similar variations does not work for me.
I used Day trading, so the EA will run on every tick until the conditions are met to open 1 trade, and then it will stop. It will only check again when a new bar is formed.
I wrote the code below. However, i'm getting an error: '}' - not all control paths return a value
Please assist me. I'm not sure how to update the code so it works properly.