Calculating total profit/loss in orders of the same magic number.

 
void checkprofit()
{
double dblProfit=0;
int POS=0;
bool boolTerm=false;
while(boolTerm==false)
                  {//4-2-1
       if(OrderSelect(POS,SELECT_BY_POS))
                    {//4-2-2
         if(OrderMagicNumber()==magicnumber) dblProfit=dblProfit+OrderProfit();
         POS++;
                    }//4-2-2
        else
        boolTerm=true;
                  }//4-2-1
   if (dblProfit>= takeprofit || dblProfit<=stoploss)
   {//4-2-3
    for(int i=OrdersTotal()-1;i>=0;i--)
       {//4-2-4
       OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES);
       if(OrderMagicNumber()==magicnumber)
       {//4-2-5
       int type   = OrderType();
              
       bool result = false;
              
       switch(type)
          {//4-2-6
          //Close opened long positions
          case OP_BUY  : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),slippage,Red);
                         break;
              
          //Close opened short positions
          case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),slippage,LimeGreen);
          }//4-2-6
          
       if(result == false)
          {//4-2-7
            Sleep(0);
          }//4-2-7  
        }//4-2-5
      } //4-2-4
      
      //return(0);
   }//4-2-3  
}


Hi All

I have written an EA to open two order for each magic number ...

after opening the orders in every tick the Ea will check if the total profit or loss of two orders.. if they reach the target profit/loss the orders will close and open two new order.

I send the checkorder() function I wrote in my code.

My EA opens orders but it never close the orders after profit or loss is reached.

Could you please check the code and find the logic error/s.

 

Hello,

Please EDIT your post and use the SRC button when you post code.

Thank you.
 

Please use the SRC button when posting code. I have done it for you this time.

To be honest, your code is not easy to read.

for(int i=OrdersTotal()-1;i>=0;i--)


OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES);
You cannot SELECT_BY_TICKET using an index number.
 
Keith Watford:

Please use the SRC button when posting code. I have done it for you this time.

To be honest, your code is not easy to read.

for(int i=OrdersTotal()-1;i>=0;i--)


OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES);
You cannot SELECT_BY_TICKET using an index number.

Thank Keith.

This is my very first code in mql4.

could you please correct me by assuming that there is only two orders for each magic number.

And the name of the order is int ticket1,ticket2

int ticket1=OrderSend(FirstSymbol,OP_BUY,lotsize,MarketInfo(FirstSymbol,MODE_ASK),slippage,0,0,NULL,magicnumber,0,clrBlue);

 

thank you 

 
Alain Verleyen:

Hello,

Please EDIT your post and use the SRC button when you post code.

Thank you.

Done.

Thanks 

 
  1. if(OrderMagicNumber()==magicnumber) dblProfit=dblProfit+OrderProfit();
    Total profit is OrderProfit() + OrderSwap() + OrderCommission()
  2. Your code
    while(boolTerm==false)
                      {//4-2-1
           if(OrderSelect(POS,SELECT_BY_POS))
                        {//4-2-2
             if(OrderMagicNumber()==magicnumber) dblProfit=dblProfit+OrderProfit();
             POS++;
                        }//4-2-2
            else
            boolTerm=true;
                      }//4-2-1
    Simplified
    for(int i=OrderTotal() - 1; i >= 0; --i) if(
       OrderSelect(POS,SELECT_BY_POS)
    && OrderMagicNumber()==magicnumber
      ) dblProfit=dblProfit+OrderProfit();

  3. Not testing for symbol means the code breaks unless you use a different MN on other pairs. Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
 

void checkprofit()
  {
   double dblProfit=0;
   for(int i=OrdersTotal()-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS))
        {
         if(OrderMagicNumber()==magicnumber && OrderSymbol()==Symbol())
            dblProfit+=OrderProfit()+OrderSwap()+OrderCommission();
        }
     }
   if(dblProfit>=takeprofit || dblProfit<=stoploss)
     {//4-2-3
      for(int i=OrdersTotal()-1;i>=0;i--)
        {//4-2-4
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
            if(OrderMagicNumber()==magicnumber && OrderSymbol()==Symbol())
              {//4-2-5
               bool result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Red);

               if(result==false)
                 {//4-2-7
                  Sleep(0);
                 }//4-2-7 
              }//4-2-5
        } //4-2-4

     }//4-2-3 
  }

Not tested

Note OrderClosePrice() works for both Buys and Sells

 
whroeder1:
  1. if(OrderMagicNumber()==magicnumber) dblProfit=dblProfit+OrderProfit();
    Total profit is OrderProfit() + OrderSwap() + OrderCommission()
  2. Your code
    while(boolTerm==false)
                      {//4-2-1
           if(OrderSelect(POS,SELECT_BY_POS))
                        {//4-2-2
             if(OrderMagicNumber()==magicnumber) dblProfit=dblProfit+OrderProfit();
             POS++;
                        }//4-2-2
            else
            boolTerm=true;
                      }//4-2-1
    Simplified
    for(int i=OrderTotal() - 1; i >= 0; --i) if(
       OrderSelect(POS,SELECT_BY_POS)
    && OrderMagicNumber()==magicnumber
      ) dblProfit=dblProfit+OrderProfit();

  3. Not testing for symbol means the code breaks unless you use a different MN on other pairs. Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum

I have this warning for OrderSelect function in 4-2-4 section:

 

return value of 'OrderSelect' should be checked last.mq4 200 8

 
Morteza Khorasani:

I have this warning for OrderSelect function in 4-2-4 section:

 

return value of 'OrderSelect' should be checked last.mq4 200 8

Have you looked at the code that I posted earlier?
 
Keith Watford:
Have you looked at the code that I posted earlier?

Yes I checked and now I reduced the code to:

void checkprofit()
{

int  mTotal = OrdersTotal();
double Profit = 0;
  for(int i = mTotal-1; i>=0; i--)  
   {
      if (OrderSelect(i, SELECT_BY_POS) && OrderMagicNumber()==magicnumber)    
      Profit += OrderProfit()+OrderSwap()+OrderCommission();
   }

   if (Profit>= takeprofit || Profit<=stoploss)
   {//4-2-3
    for(int i = mTotal-1; i>=0; i--)
       {//4-2-4
      
       if(OrderSelect(i,SELECT_BY_POS) && OrderMagicNumber()==magicnumber)
      
       OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Red);

          
        
      } //4-2-4
      
      //return(0);
   }//4-2-3  

 I See one warning:

return value of 'OrderClose' should be checked last.mq4 198 8     (in section 4-2-4)

 

By the way in code I opened orders like this:

 

int ticket1=OrderSend(SecondSymbol,OP_BUY,lotsize,MarketInfo(SecondSymbol,MODE_ASK),slippage,0,0,NULL,magicnumber,0,clrBlue);
int ticket2=OrderSend(SecondSymbol,OP_BUY,lotsize,MarketInfo(SecondSymbol,MODE_ASK),slippage,0,0,NULL,magicnumber,0,clrBlue);

Now my code opens two orders and close theme immediately after they opened, and then opens  two new orders and repeats this.
 
Morteza Khorasani:

Yes I checked and now I reduced the code to:

void checkprofit()
{

int  mTotal = OrdersTotal();
double Profit = 0;
  for(int i = mTotal-1; i>=0; i--)  
   {
      if (OrderSelect(i, SELECT_BY_POS) && OrderMagicNumber()==magicnumber)    
      Profit += OrderProfit()+OrderSwap()+OrderCommission();
   }

   if (Profit>= takeprofit || Profit<=stoploss)
   {//4-2-3
    for(int i = mTotal-1; i>=0; i--)
       {//4-2-4
      
       if(OrderSelect(i,SELECT_BY_POS) && OrderMagicNumber()==magicnumber)
      
       OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Red);

          
        
      } //4-2-4
      
      //return(0);
   }//4-2-3  

 I See one warning:

return value of 'OrderClose' should be checked last.mq4 198 8     (in section 4-2-4)

 

By the way in code I opened orders like this:

 

int ticket1=OrderSend(SecondSymbol,OP_BUY,lotsize,MarketInfo(SecondSymbol,MODE_ASK),slippage,0,0,NULL,magicnumber,0,clrBlue);
int ticket2=OrderSend(SecondSymbol,OP_BUY,lotsize,MarketInfo(SecondSymbol,MODE_ASK),slippage,0,0,NULL,magicnumber,0,clrBlue);

Now my code opens two orders and close theme immediately after they opened, and then opens  two new orders and repeats this.

I found the error.

Just added a minus sign before stoploss

if (Profit>= takeprofit || Profit<=stoploss)

if (Profit>= takeprofit || Profit<=(-stoploss))
Thank you all for the help