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

Sell your applications via the Market. It is secure!
88
2016.12.01 21:48
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
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.

Moderator
29607
2016.12.01 22:07

Hello,

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

Thank you.
Moderator
8860
2016.12.01 22:12

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

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

You cannot SELECT_BY_TICKET using an index number.
88
2016.12.01 22:15
Keith Watford:

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

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

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

thank you

88
2016.12.01 22:15
Alain Verleyen:

Hello,

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

Thank you.

Done.

Thanks

13959
2016.12.01 22:38
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
Moderator
8860
2016.12.01 23:06

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(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

88
2016.12.01 23:08
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

Moderator
8860
2016.12.02 05:51
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?
88
2016.12.02 12:56
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:

Now my code opens two orders and close theme immediately after they opened, and then opens  two new orders and repeats this.
88
2016.12.02 14:19
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: