Download MetaTrader 5

Help with orderprofit code

To add comments, please log in or register
prweza
273
prweza  

Hi Please help


I want the ea to open a buy order when profit exceed 20> I've coded this, see below but the code is unreliable( sometimes it works,sometimes it doesn't). 

Any suggestions on how it could be improved?

if((OrderSelect(gBuyTicket, SELECT_BY_TICKET)==true)

 && OrderProfit()>20)BuyA=true;
nicholishen
940
nicholishen  
prweza:

Hi Please help


I want the ea to open a buy order when profit exceed 20> I've coded this, see below but the code is unreliable( sometimes it works,sometimes it doesn't). 

Any suggestions on how it could be improved?


if(OrderSelect(gBuyTicket,SELECT_BY_TICKET)&&(OrderProfit()+OrderCommission())>20)
   BuyA=true;
But most likely your problem is somewhere downstream in your program.
prweza
273
prweza  

What about order count loop, will that work?

nicholishen
940
nicholishen  
prweza:

What about order count loop, will that work?


You need to post all of your code for anyone to help you.

prweza
273
prweza  
prweza:

What about order count loop, will that work?


Here is now my code. Will it work?

  for(int i=OrdersTotal(); i>=0; i--){
   if((OrderSelect(gBuyTicket,SELECT_BY_TICKET)==true) 

 && OrderProfit()>20)BuyA=true;
 }
Keith Watford
Moderator
10617
Keith Watford  
prweza:

Here is now my code. Will it work?

 for(int i=OrdersTotal(); i>=0; i--){
   if((OrderSelect(gBuyTicket,SELECT_BY_TICKET)==true) 

 && OrderProfit()>20)BuyA=true;
 }

No.

You are selecting the same trade everytime, so the loop is ineffective.

prweza
273
prweza  
Keith Watford:

No.

You are selecting the same trade everytime, so the loop is ineffective.


Thanks could you suggest improvements?

What about this:

//++++++open another order if current order is in profit order on profit 
  for(int i=OrdersTotal()-1; i>=0; i--)
        if ((OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderProfit()>20&& OrderSymbol() == _Symbol && OrderMagicNumber() == MagicNumber
        && (OrderType()==OP_BUY || OrderType()==OP_SELL)==true) && OrderProfit()>20)BuyA=true;
   

Keith Watford
Moderator
10617
Keith Watford  
prweza:

Thanks could you suggest improvements?

What about this:

//++++++open another order if current order is in profit order on profit 
  for(int i=OrdersTotal()-1; i>=0; i--)
        if ((OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderProfit()>20&& OrderSymbol() == _Symbol && OrderMagicNumber() == MagicNumber
        && (OrderType()==OP_BUY || OrderType()==OP_SELL)==true) && OrderProfit()>20)BuyA=true;
   

Pointless checking the same condition twice.

Your code should work ok but you also need to consider when to execute the code. If it is executed every tick you could be opening a new trade every tick. So you need to check whether you will actually require a new trade opened before executing this code.

To add comments, please log in or register