Closing orders first max value, second min value

 

Hello,

I am trying to write a code which closes orders by rule. However, I could not get a success.

The need is close order,
     first close-> max value, second close--> min value,
     than we have new reduced orders list and the same rule for remaining list:
     first close-> max value, second close--> min value,
     than again, again and again...

The aim to obtain real max relative drawdown.

For example:

     I assume that I have opened 5 orders as following;
     first is 100 usd, second is 80 usd, third is -200 usd, fourth is -50 usd, and fifth is -10 usd

     The need is to close following orders respectively: 100 usd, -200 usd, 80 usd, -50 usd, and -10 usd.

By this way, I want to achive a real max relative drawdown for trader who opens nt only one order at the same time.

Best, Murat Y.

 
Murat Yazici: I am trying to write a code which closes orders by rule. However, I could not get a success.

The need is close order,
     first close-> max value, second close--> min value,
     than we have new reduced orders list and the same rule for remaining list:
     first close-> max value, second close--> min value,
     than again, again and again...

The aim to obtain real max relative drawdown. For example:

     I assume that I have opened 5 orders as following;
     first is 100 usd, second is 80 usd, third is -200 usd, fourth is -50 usd, and fifth is -10 usd

     The need is to close following orders respectively: 100 usd, -200 usd, 80 usd, -50 usd, and -10 usd.

By this way, I want to achive a real max relative drawdown for trader who opens nt only one order at the same time.

Show your attempt at coding it. We can only help guide you, not to code it for you.
 
Murat Yazici:

Hello,

I am trying to write a code which closes orders by rule. However, I could not get a success.

The need is close order,
     first close-> max value, second close--> min value,
     than we have new reduced orders list and the same rule for remaining list:
     first close-> max value, second close--> min value,
     than again, again and again...

The aim to obtain real max relative drawdown.

For example:

     I assume that I have opened 5 orders as following;
     first is 100 usd, second is 80 usd, third is -200 usd, fourth is -50 usd, and fifth is -10 usd

     The need is to close following orders respectively: 100 usd, -200 usd, 80 usd, -50 usd, and -10 usd.

By this way, I want to achive a real max relative drawdown for trader who opens nt only one order at the same time.

Best, Murat Y.

Hello, 

I hope it helps you.

<Moderator's Note: the following code is MQL5 and will not work in MQL4>

//+------------------------------------------------------------------+
//|                                                       Prueba.mq5 |
//|                                                Simón Del Vecchio |
//|                    https://www.mql5.com/es/users/simondelvecchio |
//+------------------------------------------------------------------+
#property copyright "Simón Del Vecchio"
#property link      "https://www.mql5.com/es/users/simondelvecchio"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Includes                                                         |
//+------------------------------------------------------------------+
#include <Trade\SymbolInfo.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\AccountInfo.mqh>
//+------------------------------------------------------------------+
//| Objetos                                                          |
//+------------------------------------------------------------------+
CSymbolInfo Info;
CTrade Trade;
CPositionInfo Posicion;
CAccountInfo Cuenta;
//+------------------------------------------------------------------+
//| Inputs                                                           |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Variables globales                                               |
//+------------------------------------------------------------------+
string Recorrido = "Even";
//+------------------------------------------------------------------+
//| Función de inicialización del EA                                 |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      ClosePositions();
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void ClosePositions()
  {
   double MaxActual = 0;
   double MinActual = 0;
   ulong Ticket = 0;
   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      Posicion.SelectByIndex(i);
      //In even we look for the maxima and in odd we look for the minima
      if(Recorrido == "Even" && Posicion.Profit() > MaxActual)
        {
         MaxActual = Posicion.Profit();
         Ticket = Posicion.Ticket();
        }
      else
         if(Recorrido == "Odd" && Posicion.Profit() < MinActual)
           {
            MinActual = Posicion.Profit();
            Ticket = Posicion.Ticket();
           }
     }
   Trade.PositionClose(Ticket, -1);
   if(Recorrido == "Even")
      Recorrido = "Odd";
   else
      Recorrido = "Even";
  }
//+------------------------------------------------------------------+
 
Antonio Simon Del Vecchio #:

Hello, 

I hope it helps you.

Thank you, Antonio Simon! A very great code...
For the aim, it works to get real max relative drawdown.
I will use it. Best.

 
Antonio Simon Del Vecchio #:

Hello, 

I hope it helps you.

Please note that this topic is in the MQL4 and MT4 section.
The code that you have posted is MQL5 which will not work in MT4.

 
Murat Yazici #:

Thank you, Antonio Simon! A very great code...
For the aim, it works to get real max relative drawdown.
I will use it. Best.

You have posted your topic  in the MQL4 and MT4 section.

Yet you seem to think that MQL5 code is useful to you!

Please confirm whether your code is MQL4 or MQL5. If you have posted in the wrong section, I will move your topic.

 
Keith Watford #:

You have posted your topic  in the MQL4 and MT4 section.

Yet you seem to think that MQL5 code is useful to you!

Please confirm whether your code is MQL4 or MQL5. If you have posted in the wrong section, I will move your topic.

Right topic! I just understood the idea.

string Recorrido = "Even";

Void OnTick()

{

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

}

void Trade::ClosePositions()

  {

   double MaxActual = 0;

   double MinActual = 0;

   ulong Ticket = 0;

   int slippage=0;

   

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

     {

      bool select=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

      //In even we look for the maxima and in odd we look for the minima

      if(Recorrido == "Even" && OrderProfit()> MaxActual )

        {

         MaxActual = OrderProfit();

         Ticket = OrderTicket();

        }

      else

        {

         if(Recorrido == "Odd" && OrderProfit()< MinActual )

           {

            MinActual = OrderProfit();

            Ticket = OrderTicket();

           }

        }

         

     }

   OrderClose(Ticket,OrderLots(),OrderClosePrice(),slippage,clrAliceBlue);

   if(Recorrido == "Even") Recorrido = "Odd";

   else Recorrido = "Even";

  }

 
Keith Watford #:

Please note that this topic is in the MQL4 and MT4 section.
The code that you have posted is MQL5 which will not work in MT4.

Excuse me, where do I see that it is for MT4? I do not see anywhere that they tell me that it is for MT4.

 
Antonio Simon Del Vecchio #:

Excuse me, where do I see that it is for MT4? I do not see anywhere that they tell me that it is for MT4.

In your first screenshot, you will see "Forum" in the bar at the top.
Click on that and the window will open with all the forum sections.

 
Keith Watford #:

In your first screenshot, you will see "Forum" in the bar at the top.
Click on that and the window will open with all the forum sections.

In other words, the people who want to help in the forum, should not click on the links on the first page but enter through other links! That way we will be aware of the category in which we are helping. Something is wrong. I'll keep that in mind for the next one.
 
Antonio Simon Del Vecchio #:
In other words, the people who want to help in the forum, should not click on the links on the first page but enter through other links! That way we will be aware of the category in which we are helping. Something is wrong. I'll keep that in mind for the next one.

You can also go to the top of a page and hover your mouse over the bent arrow at the top left. The category will pop up.

Reason: