in CloseAllOrd()
1:
have look at OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
c/0/cnt/
2:
if(magic!=...) continue; //why? because you do not want to go any further with this pool entry - so let's loop onto next entry
3:
What happens if 1000 + 3000 ms goes by and still busy thread?
may want to consider retry loop and when MAXRETRIES met, abort EA with suitable diagnostics...
'abort' is of course impossible as each data tick EA called but can set EA global scope bool/int so that start() looks at this variable and if set just exits back to Terminal. yes? same idea as like seeing if SAT or SUN and EA just returns...
I ramble... but seems to me this whole area of error handling is in no way simple. On the one hand EA must trade to make profit and on the other hand NOT trapping every possible error condition and having comprehensive design to deal with each error type imho, seems madness...
4:
CurrentProfit=CurrentProfitB+CurrentProfitS;
is this what you meant?
looks asif CurrentProfit will only ever hold B||S profit for one pool entry.
Also, IF you use above statement (maybe moded with '+=' ??) do you want to add B+S each loop?
IE, each loop should be B||S not B&&S (with the other set to zero) - and the previous B,S will get added in again,... see what I'm on about?
And... is CurrentProfit global in EA scope or global in function() scope or?
5:
may be considered oldhat/labor intensive but why not play computer and desk check with pencil and paper?
it is guarranteed to focus one's mind and also, highlight coding/logic/concept issues.
Regards
(please excuse my sketchy reply: I am totally under weather thanks to a friendly neighborhood virus - lol. In between constant dribbling of nose I hit a few keys...)
of course - all that I say above are just suggestions - not meant as gospel ok?
what I am trying to do is to look at the COMBINED profit of trades with magic number 111, however those trades can be from different currency pairs
for example I may have a trade on EURUSD with 30 pips in profit and another trade with USDCHF with -15 pips, but both trades have the magic number of 111 so I look at the combined pips if +30 of EURUSD + (-15 of USDCHF )= 15 pips ( I know there is a diff in pip value but its not important at this level ) then I close the trades
So I go thorugh a loop to look for all open orders ( EURUSD , USDCHF ...etc ) with the magic number 111 if the total profit reach 15 pips of all the open trades then I close these positons regardless they are buy or sell EURUSD or USDCHF , I am looking at the magic number because the expert place more than magic number with different currency pairs
I
Show your code please,
have you read 'The First Book on MQL4'
EG, Bid,Ask are predefined variables for current symbol
IF map pool order with magic 111 it 'could' be != current symbol, yes?
meaning you cannot just use Bid,Ask since NOT associated with the mapped order...
Read docs on MarketInfo() and predefined variables.
regards
I use the following code ... (you may change as it fits your code!) ....
May be the code looks complicated, but it is not.
It's logic is very clear.
Note, TotalOrders() function is not OrdersTotal() !
also, Profit() function calculated only trades with the same Magic no.
...
extern bool AutoClosePositions = true;
...
double MaxTargetProfit;
MaxTargetProfit = TotalOrders()*10; //This is up to your profit target or formula!
...
...
if(AutoClosePositions && TotalOrders()!= 0) {
if(Profit()>=MaxTargetProfit){
int total = OrdersTotal();
for(int i=total-1;i>=0;i--){
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
bool result = false;
if(OrderMagicNumber()==MAGIC){
switch(type){
case OP_BUY : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Red);
break;
case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5, Blue);
} //switch
if(result == false){
Print("Order ", OrderTicket(), " failed to close. Error:", GetLastError() );
}//if(result == false)
}//if(OrderMagicNumber()==MAGIC){
}//for(int i= ...
}//if(AccountProfit()>=MaxTargetProfit)
}//if(AutoClose...
int TotalOrders() {
int cnt=0;
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderMagicNumber()==MAGIC && (OrderType()==OP_BUY || OrderType()==OP_SELL)) {
cnt++;
}
}
}
return(cnt);
}
double Profit(){
double Prof=0;
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderMagicNumber()==MAGIC) {
Prof = Prof + OrderProfit();
}
}
}
return(Prof);
}

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I need help please,
I am trying to close orders from different currency pairs ( long and short ) but with the same magic number when the profit reach define target " lets say 15 pips" without touching the other orders with different magic number
so I wrote this code to check open trades and count the profit on trades with magic number 111
then I check if we make profit
if we make a profit I call the function CloseAllOrd
however this will only close EURUSD after it reach 1 pip of profit
I need to close all orders( different currency pairs ) with magic number 111 at 15 pips of profit
can someone please help me