timothysmith78:
My problem is that if a trade is already open, it doesn't open a new one.
Can you please look over my code to suggest possible solutions.
-
Play videoPlease edit your post.
For large amounts of code, attach it.General rules and best pratices of the Forum. - General - MQL5 programming forum
I don't know how you got <div class="code">, which is why your post has no highlighting and is double spaced. Instead use SRC which generates <pre class="code">. if(Ask<=LowerBB) OrderEntry(0); if(Bid>=UpperBB) OrderEntry(1);
Why are you using 0/1 which you call direction? Just use the trade constants OP_BUY, OP_SELL so your code is self-documenting. Order Properties - Trade Constants - Standard Constants, Enumerations and Structures - MQL4 Reference if (TimeCurrent() >= TimeSent + (WaitTime *60)) buyticket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,0,0,NULL,MagicNumber,0,Green); TimeSent = TimeCurrent(); if(buyticket>0) if (OrderModify(buyticket,OrderOpenPrice(),Ask-StopLoss*Point,NULL,0,CLR_NONE)) Print("Order ",buyticket," was successfully modified."); else Print("Order ",buyticket," was NOT successfully modified.",GetLastError());
Need brackets. All lines must be inside the if. If it is not time, then buyticket is not changed (still zero,) but you change TimeSent anyway. So nothing ever opens.- Why are you modifying TimeSent if an order was not opened?
-
If you had checked your return codes for the OrderSend error (and printed an error message including& _LastError, you would know why; that you are not calling OrderSend at all. What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles - You can not use any
Trade Functions until you select an order.
Code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points,) and metals. Compute what a pip is and use it, not points. How to manage JPY pairs with parameters? - MQL4 and MetaTrader 4 - MQL4 programming forum -
NULL is not a valid price.
- You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, MarketInfo does not. OrderSend does not.
- Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
- Zero is the same as PERIOD_CURRENT which means _Period.
- No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
for(int i=OrdersTotal()-1; i >= 0; i--) if( OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()== pair) total++;
Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.) Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum if(OpenOrdersThisPair(Symbol())<=5) : for(int i=OrdersTotal()-1; i >= 0; i--) if( OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderType()==OP_BUY)
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) because while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum - For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
-
and check OrderSelect. What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles - and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.
if (OrderClose(OrderTicket(),LotSize,High[0],3,CLR_NONE))
You can not close at the High or Low. Only at the current market. Use OrderClosePrice and OrderLots. Adjust slippage (item 7.) RefreshRates (item 9.4.)else Print("Buy order",OrderTicket(), "was NOT closed successfully");
Print _LastError to find out why (item 4.)

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
Hi MQL4 folks,
I have created a basic BB EA that goes long on the middle bb and goes short on the upper band. Both long and short orders are closed at
the middle BB.
I have added a timer that allows a new trade to execute every 15 minutes if a signal is generated which appears to work ok. My problem
is that if a trade is already open, it doesn't open a new one.
Can you please look over my code to suggest possible solutions. I feel it may have something to do with the OpenOrdersThisPair function, but am not sure.
Your help would be much appreciated!