Closing all open trades automatically results in losing price value even when the difference is positive profits - page 3

 
tompips2018:

Anyways, the logic was RIGHT. For me. It's just wrong for you.

It was wrong for you as well.

That is why your calculations worked out that the trades were in profit when they actually were not.

 
There's none so deaf as those who will not hear.
 
Keith Watford:

It was wrong for you as well.

That is why your calculations worked out that the trades were in profit when they actually were not.

OK so you admit both of us our wrong. At least I admit I missed something. You ain't.

Now it's profitting. So what did I say again? Case closed.

Why keep this thread alive.

To fight for your ego?

So what do you want to read from me? Concede? Some people do, but I don't. As I said, this thread is done.


When someone disagrees with you, respect their stance. If you want to feel right about yourself, feel free to do so. I believe you are a good programmer. But it looks like moderating forums like these and trying your best to be neutral is way out of your forte. Don't get too absorbed, dude. Just moderate :-D.You can't even simply call it a day, and trying hard to get a self-confirmation of your conviction to be right. You can always be right for yourself. Unfortunately, from my side of the fence, you're wrong for me. ;-) 


With all due respect, for a moderator, it seems you're the one who needs moderatin'.


Or should we call an admin to do your job of moderating yourself? ;-)

 
Ah, this thread is getting way off-topic, a feisty amateur noob vs. a mod who can't moderate.

This is gonna be exciting.
Files:
 
tompips2018:

OK so you admit both of us our wrong. At least I admit I missed something. You ain't.

No, I said that your calculations were wrong. I was not wrong.

Why keep this thread alive.

Others may learn from it in future.

To fight for your ego?

My ego!?

You seem to be the one with the ego.

When someone disagrees with you, respect their stance. If you want to feel right about yourself, feel free to do so. I believe you are a good programmer. But it looks like moderating forums like these and trying your best to be neutral is way out of your forte. Don't get too absorbed, dude. Just moderate :-D.You can't even simply call it a day, and trying hard to get a self-confirmation of your conviction to be right. You can always be right for yourself. Unfortunately, from my side of the fence, you're wrong for me. ;-) 

You should take some of your own advice on board. I made a valid suggestion regarding a better way to close all trades and you simply refused to accept that there was any problem with your function. One day you will see that your function does not work correctly. I just hope that it doesn't result in losses for you.

With all due respect, for a moderator, it seems you're the one who needs moderatin'.

 I am a forum user primarily and I am posting here as a user, not a moderator.
 
Keith Watford:

No, I said that your calculations were wrong. I was not wrong.

Others may learn from it in future.

My ego!?

You seem to be the one with the ego.

You should take some of your own advice on board. I made a valid suggestion regarding a better way to close all trades and you simply refused to accept that there was any problem with your function. One day you will see that your function does not work correctly. I just hope that it doesn't result in losses for you.

 I am a forum user primarily and I am posting here as a user, not a moderator.

"No, I said that your calculations were wrong. I was not wrong." 

"My ego!?"

"You seem to be the one with the ego."



We already know that, didn't we? Why keep this discussion going? Reader will already learn backreading without reading after my request for closure. Why keep on ranting? Ah I know. Clue: your EGO :-D :-D


"You should take some of your own advice on board. I made a valid suggestion regarding a better way to close all trades and you simply refused...."

Didn't I answered that already? For effing sakes. You just can't accept it can you? :-D :-D :-D


Why don't you backread yourself. 3:)



"..I just hope that it doesn't result in losses for you."



Well IT IS GIVING ME PROFITS NOW. If you really *hope* so, the best you could do is keep quiet, leave this thread behind without saying a word because - EV'RYTHING'S BEEN ANSWERED ALREADY. :-D  ¯\_( ͡° ͜ʖ ͡°)_/¯



That's the purpose of this forum, right? Now move on. 3:) :-D


" I am a forum user primarily and I am posting here as a user, not a moderator."


Then zip it. I already got I want. Wear your mod hat and use your mod sense. Close it. ;-)

 
tompips2018:


Oh dear!

As there seems to be only 1 way to put this to bed.

Please try this simple code in the tester it uses your function.

#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   for(int x=0;x<3;x++)
   OrderSend(_Symbol,OP_BUY,0.02,Ask,50,0,0,NULL,123,0,clrNONE);
   for(int x=0;x<3;x++)
   OrderSend(_Symbol,OP_SELL,0.01,Bid,50,0,0,NULL,123,0,clrNONE);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   double profit=0;
   for(int x=OrdersTotal()-1;x>=0; x--)
    {
     if(OrderSelect(x,SELECT_BY_POS))
       profit+=OrderProfit()+OrderCommission()+OrderSwap();
    }
    Comment("Profit= "+DoubleToStr(profit,2));
   if(profit>=10)
     CloseAllTrades();
  }
//+------------------------------------------------------------------+
// ======================================================================================= 
double CloseAllTrades()
{
   //Go through all open orders
   Print(">>>>>>>>>>>>>>>>>>>>>>>>Inside CloseAllTrades function<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
   int TotalRunningTrades = OrdersTotal();
   int BuyOrSell = OrderType();
   bool r = false;
   
   for (int i=TotalRunningTrades-1;  i >= 0; i--)
   {
      //Select each trade to close
      if (OrderSelect(i,SELECT_BY_POS) == true)
      {
         if (OrderSymbol() == Symbol())
         {
            if(BuyOrSell == 0) //It's a BUY
            {  //Close the trade orders
               //               ticket        lot volume          close price                 slippage   arrow color
               r = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK),   15,     clrAliceBlue);
            }
            if(BuyOrSell == 1) //It's a SELL
            {  //Close the trade orders
               //               ticket        lot volume          close price                 slippage   arrow color
               r = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID),   15,     clrAliceBlue);
            }
            Print(r);
         }
      }  
   } // End of for (int i=TotalRunningTrades-1; Loop
   return(r);
}   //End of CloseAllTrades() function

Obviously it should be run in a period when the buys will profit.

Result

2 11:55:08.815 2020.03.03 00:00:00  ##TEST test started

2 11:55:08.868 2020.03.03 00:05:00  ##TEST EURUSD,H1: open #1 buy 0.02 EURUSD at 1.11294 ok

2 11:55:08.868 2020.03.03 00:05:00  ##TEST EURUSD,H1: open #2 buy 0.02 EURUSD at 1.11294 ok

2 11:55:08.868 2020.03.03 00:05:00  ##TEST EURUSD,H1: open #3 buy 0.02 EURUSD at 1.11294 ok

2 11:55:08.868 2020.03.03 00:05:00  ##TEST EURUSD,H1: open #4 sell 0.01 EURUSD at 1.11274 ok

2 11:55:08.868 2020.03.03 00:05:00  ##TEST EURUSD,H1: open #5 sell 0.01 EURUSD at 1.11274 ok

2 11:55:08.868 2020.03.03 00:05:00  ##TEST EURUSD,H1: open #6 sell 0.01 EURUSD at 1.11274 ok

0 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: >>>>>>>>>>>>>>>>>>>>>>>>Inside CloseAllTrades function<<<<<<<<<<<<<<<<<<<<<<<<<<<<

2 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: close #6 sell 0.01 EURUSD at 1.11274 at price 1.11696

0 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: true

2 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: close #5 sell 0.01 EURUSD at 1.11274 at price 1.11696

0 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: true

2 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: close #4 sell 0.01 EURUSD at 1.11274 at price 1.11696

0 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: true

3 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: OrderClose error 138

0 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: false

3 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: OrderClose error 138

0 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: false

3 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: OrderClose error 138

0 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: false

0 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: >>>>>>>>>>>>>>>>>>>>>>>>Inside CloseAllTrades function<<<<<<<<<<<<<<<<<<<<<<<<<<<<

3 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: OrderClose error 138

0 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: false

3 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: OrderClose error 138

0 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: false

3 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: OrderClose error 138

0 11:55:20.911 2020.03.03 17:00:17  ##TEST EURUSD,H1: false

etc etc etc etc

The sells are closed, but the buy closures fail.
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 

Now try this code that has my simple function

#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   for(int x=0;x<3;x++)
   OrderSend(_Symbol,OP_BUY,0.02,Ask,50,0,0,NULL,123,0,clrNONE);
   for(int x=0;x<3;x++)
   OrderSend(_Symbol,OP_SELL,0.01,Bid,50,0,0,NULL,123,0,clrNONE);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   double profit=0;
   for(int x=OrdersTotal()-1;x>=0; x--)
    {
     if(OrderSelect(x,SELECT_BY_POS))
       profit+=OrderProfit()+OrderCommission()+OrderSwap();
    }
    Comment("Profit= "+DoubleToStr(profit,2));
   if(profit>=10)
     CloseAllTrades();
  }
//+------------------------------------------------------------------+
#include <stdlib.mqh>
//+------------------------------------------------------------------+
void CloseAllTrades()
{
   for(int x=OrdersTotal()-1; x>=0; x--)
     {
      if(OrderSelect(x,SELECT_BY_POS) && OrderSymbol()==_Symbol)
        {
         if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),15,clrNONE))
            Print(TimeCurrent()," Error with Closing Order #"+(string)OrderTicket()+ErrorDescription(GetLastError()));
        }
     }
}   //End of CloseAllTrades() function

Result

2 12:06:02.833 2020.03.03 00:05:00  ##TEST EURUSD,H1: open #1 buy 0.02 EURUSD at 1.11294 ok

2 12:06:02.833 2020.03.03 00:05:00  ##TEST EURUSD,H1: open #2 buy 0.02 EURUSD at 1.11294 ok

2 12:06:02.833 2020.03.03 00:05:00  ##TEST EURUSD,H1: open #3 buy 0.02 EURUSD at 1.11294 ok

2 12:06:02.833 2020.03.03 00:05:00  ##TEST EURUSD,H1: open #4 sell 0.01 EURUSD at 1.11274 ok

2 12:06:02.833 2020.03.03 00:05:00  ##TEST EURUSD,H1: open #5 sell 0.01 EURUSD at 1.11274 ok

2 12:06:02.833 2020.03.03 00:05:00  ##TEST EURUSD,H1: open #6 sell 0.01 EURUSD at 1.11274 ok

2 12:06:11.387 2020.03.03 17:00:17  ##TEST EURUSD,H1: close #6 sell 0.01 EURUSD at 1.11274 at price 1.11696

2 12:06:11.387 2020.03.03 17:00:17  ##TEST EURUSD,H1: close #5 sell 0.01 EURUSD at 1.11274 at price 1.11696

2 12:06:11.387 2020.03.03 17:00:17  ##TEST EURUSD,H1: close #4 sell 0.01 EURUSD at 1.11274 at price 1.11696

2 12:06:11.387 2020.03.03 17:00:17  ##TEST EURUSD,H1: close #3 buy 0.02 EURUSD at 1.11294 at price 1.11676

2 12:06:11.387 2020.03.03 17:00:17  ##TEST EURUSD,H1: close #2 buy 0.02 EURUSD at 1.11294 at price 1.11676

2 12:06:11.387 2020.03.03 17:00:17  ##TEST EURUSD,H1: close #1 buy 0.02 EURUSD at 1.11294 at price 1.11676


All closed as they should!
 
Keith Watford:

Now try this code that has my simple function

Result

All closed as they should!

Well didn't it occur to you that I have modified the code SINCE the thread has finished (thanks to Seng)?

You are looking at an outdated code! :-D :-D :-D

Don't feel bad because I didn't follow your advice. We are all free to pick the ones we want to eat.


Now, go back to bed, and close the thread.   ;-)

Files:
 
tompips2018:

Well didn't it occur to you that I have modified the code SINCE the thread has finished (thanks to Seng)?

You are looking at an outdated code! :-D :-D :-D

Don't feel bad because I didn't follow your advice. We are all free to pick the ones we want to eat.


Now, go back to bed, and close the thread.   ;-)

No, the error that Keith highlights is about the use of wrong prices for closing different order types. Remember I mentioned that you should use Bid when closing Buy, and Ask when closing Sell, and Keith suggested using OrderClosePrice() - which will insert the right Ask / Bid for you? 

Your code uses either Ask or Bid to close all orders, due to the calling of OrderType() before OrderSelect().

It worked for you, because your spread happened to be 13, and you call OrderClose() with a slippage of 15. However, you'll encounter error when your spread is more than 15, or you call OrderClose() with a slippage of less than 13 - you can try that out.

Reason: