Help with orderprofit code

 

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;
 
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.
 

What about order count loop, will that work?

 
prweza:

What about order count loop, will that work?


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

 
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;
 }
 
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.

 
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;
   

 
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.

Reason: