Close Profitable Trades Only, at Profit Target - page 4

 
af1:

Ok Raptor, I'll try to make that loop, but before do that, what if I just change

From this:
To this:


Could this do the job?

No, you MUST count down when closing/deleting Orders.
 
RaptorUK:
No, you MUST count down when closing/deleting Orders.


Hi Raptor!

I've been checking the code and to be honest I'm not seeing much difference between the code line suggested in your post:

for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --)

and the code code line of the ea I'm testing:

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

The only difference I've seeing is that you add MODE_TRADES, and you didn't add sleep. Is this relevant?

Could you give some guide of what should I change in the code?

 
af1:


Hi Raptor!

I've been checking the code and to be honest I'm not seeing much difference between the code line suggested in your post:

and the code code line of the ea I'm testing:

The only difference I've seeing is that you add MODE_TRADES, and you didn't add sleep. Is this relevant?

Could you give some guide of what should I change in the code?

Sorry, I saw this . . .

for(int i=0;i<OrdersTotal();i++)
      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
         if(OrderType()==OP_SELL)  
         {
            OrdersSELL++;
            SellLots += OrderLots();
            double ThisTradeProfit=OrderProfit() + OrderCommission() + OrderSwap();
            if(ThisTradeProfit>0)
            BuyProfit += ThisTradeProfit;
         }

. . . and you post and assumed it was your OrderClose() code . . .

 
af1:


I didn't add the 2 extern bool, I just change this to close "only buy orders"

and change this to close "only sell orders"

So now I have two EA's. One for close buys, and one for close sells. That's ok for me.


I've tested on both accounts, demo and real. On demo works fine, but in real account, once the profit target is reached, it will start to close positions, but then it will stop closing positions when the remaining orders drops below the profit target. This happens if the price is changing at the moment of close. So it tends to leave open profitable orders, instead of close them all (profitable orders).

I read this post from RaptorUK https://www.mql5.com/en/forum/139654 . It's something like that, but I don't really know what could be the best solution for this issue.



Only just noticed this - don't know if it will make much difference, but it is not right

for(int i=0;i<OrdersTotal();i++)

      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
         if(OrderType()==OP_SELL)  
         {
            OrdersSELL++;
            SellLots += OrderLots();
            double ThisTradeProfit=OrderProfit() + OrderCommission() + OrderSwap();
            if(ThisTradeProfit>0)
            BuyProfit += ThisTradeProfit;
         }
 
af1:


I didn't add the 2 extern bool, I just change this to close "only buy orders"

and change this to close "only sell orders"

So now I have two EA's. One for close buys, and one for close sells. That's ok for me.


I've tested on both accounts, demo and real. On demo works fine, but in real account, once the profit target is reached, it will start to close positions, but then it will stop closing positions when the remaining orders drops below the profit target. This happens if the price is changing at the moment of close. So it tends to leave open profitable orders, instead of close them all (profitable orders).

I read this post from RaptorUK https://www.mql5.com/en/forum/139654 . It's something like that, but I don't really know what could be the best solution for this issue.



That suggests that there are errors closing the trades, what does it say in the log?

Maybe your slippage at only 5 points is too small?

 
RaptorUK:

Sorry, I saw this . . .

. . . and you post and assumed it was your OrderClose() code . . .

No problem.

GumRai:


Only just noticed this - don't know if it will make much difference, but it is not right

for(int i=0;i<OrdersTotal();i++)

In demo I haven't seen any difference, but in real account the issue shows up when it was closing sell orders "curiously", so maybe this could solve the issue..

Let me test again with this change and I'll inform you the results.

GumRai:


That suggests that there are errors closing the trades, what does it say in the log?

Maybe your slippage at only 5 points is too small?

No errors in the log.

Maybe bigger slippage could help. I will test it.

 

Hello Friends

Does any where has an EA for MT5 to Close all trades at profit reached.

 
BobyGill:

Hello Friends

Does any where has an EA for MT5 to Close all trades at profit reached.

Why have you brought up a 6 year old topic in the MT4 section to ask about MT5 ???

 
Guys..
Just wondering..
Is it possible to using a same logic but the one that make it's different is only When the total transaction is hit let say 200 transaction then it will trigger the closing profit.