Seperate Magic number control on Expert Advisor

 
I am busy developing an EA. I have gotten the EA working correctly on focusing on one magic number.
what do I need to add so the EA focuses on 2 magic numbers, but focuses on them separately?

Example 

EA take profit = $5.0

magic number 1 = $2.5
magic number 2 = $5.0

EA closes magic number 2

EA does not close magic number 1 until Take profit has been reached of $5.0

Here is the coding I have for magic number 1
How do I add magic number 2?

double Profit(){
double Prof=0;
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if( OrderMagicNumber()== magic_number1 ){
Basic Principles - Trading Operations - MetaTrader 5
Basic Principles - Trading Operations - MetaTrader 5
  • www.metatrader5.com
is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels. is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the...
 
Franzel Botha:
what do I need to add so the EA focuses on 2 magic numbers, but focuses on them separately?
if( OrderMagicNumber()== magic_number1 )
{
        // Do something
}
else if( OrderMagicNumber()== magic_number2 )
{
        // Do something
}
 
Anthony Garot:

Thnx. will try it out.

 
Anthony Garot:

Just wanna say thank you very much.
It worked 100%.

Regards Franzel

 
Anthony Garot:

I ran into a small problem. now the EA closed magic number 2 but not magic number 1.
How do I fix this issue?
Sorry. I am new to the mql4 coding and still learning a lot.


double Profit(){
double Prof=0;
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if( OrderMagicNumber()== magic_number ){
}
else if( OrderMagicNumber()== magic_number2 )
{

Prof = Prof + OrderProfit()+ OrderCommission() + OrderSwap();
}
} 
}
return(Prof);
}

void CloseOrder(void)
{
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_number){

else if( OrderMagicNumber()== magic_number2 ){

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() );
}
}
}
}
void reset(void)
{
 

Please use the </> button to insert your code.


 
Eleni Anna Branou:

Please use the </> button to insert your code.


ok. no prob. done

 
Franzel Botha:

I ran into a small problem. now the EA closed magic number 2 but not magic number 1.
How do I fix this issue?
Sorry. I am new to the mql4 coding and still learning a lot.


To close both:

if(OrderMagicNumber()==magic_number || OrderMagicNumber()== magic_number2 )
{
    // Close code here
}

I think you would gain quite a bit by reading this tutorial:

https://book.mql4.com/operators/if

Conditional Operator 'if - else' - Operators - MQL4 Tutorial
Conditional Operator 'if - else' - Operators - MQL4 Tutorial
  • book.mql4.com
As a rule, if you write an application program, you need to code several solutions in one program. To solve these tasks, you can use the conditional operator 'if-else' in your code. Format of the Operator 'if-else' Full Format The full-format operator 'if-else' contains a heading that includes a condition, body 1, the key word 'else', and body...
Reason: