Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1624

 
EVGENII SHELIPOV #:

Good day !!!

Please help in writing code for closing min and max orders in a grid EA when a certain level of drawdown is reached

I wrote two functions to calculate the profit of max and min orders

I have also written a function calculating the amount of these orders. There seems to be no problem here.

The question is how to close only these two orders I have found the ClosseAll() function.

The question is how to change the OrderTicket() function which closes all orders

to attach variables max_ticket and min_ticket which determine tickets of only min and max orders in the grid

Or do YOU have YOUR solution to this issue

void ClosseAll()
{
  int slip = MarketInfo(_Symbol,MODE_SPREAD)*2;
  for(int i = OrdersTotal()-1; i>=0; i--)
  {
     if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
     {
      if (OrderClose(max_ticket , OrderLots(), Bid, slip)&&OrderClose(min_ticket , OrderLots(), Bid, slip))
            Print("OK");
      else
            Print("Не удалось закрыть ордер! ",GetLastError());
     }
  }
}

and fix it.

if(OrderTicket() > max_ticket) max_ticket = OrderTicket();
 
makssub #:

Sorry, I'm writing it wrong again. Let me try again)

There is a grid of open orders. I need to find the opening price (OrderOpenPrice) of the order closest to the current price. In order to continue to build the grid, but my orders are almost chaotically built.

I understand how to write the overshoot, but I'm unable to express it correctly in the language (I'm like a dog, I understand everything)).

If you have examples or link to examples, post them, please. MQL4

in a loop:

if (MathAbs(OrderOpenPrice()-Bid)<previous_value)
   {
   previous_value=MathAbs(OrderOpenPrice()-Bid);
   nearest_order=OrderTicket();
   }

before loop initialize previous_value=DBL_MAX a nearest_order=0

 
PapaYozh #:

How is it different, by the way, apart from the fact that it is called implicitly when the object is created?

and plus when you call it in the constructor no memory is allocated yet, it doesn't compile:

class A
{
public:
   int               val;
                     A() {}
};
//+------------------------------------------------------------------ +
class B
{
public:
   A                 a;
                     B():a(),a.val(10) {} //
};

but in the constructor body, no problem:

class A
{
public:
   int               val;
                     A() {}
};
//+------------------------------------------------------------------ +
class B
{
public:
   A                 a;
                     B():a() {a.val=10;}
};



about base class constructors - they can be called explicitly:

class A
{
public:
   int               val;
                     A(const int v): val(v) {}
                     A() {}
};
//+------------------------------------------------------------------ +
class B: public A
{
public:
                     B(): A(100) {}
};
 
MakarFX #:

Fix that, too.

About the slippage I already have at the beginning of the advisor

MakarFX #:

and fix that.

MakarFX #:

fix this

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init() 
{
   Spread = MarketInfo(Symbol(), MODE_SPREAD) * Point;
   MinLot = MarketInfo(Symbol(),MODE_MINLOT);
   return (0);
}

Only maximal orders are closed according to the closing results


 
Taras Slobodyanik #:

in the loop:

initialise previous_value=DBL_MAX a nearest_order=0 before the loop

MakarFX #:

and correct this

MakarFX #:

and fix that.

For errors in the log


 
Good afternoon Has anyone heard of the likes of MirachLtd-Real .Does anyone have any feedback?

 
EVGENII SHELIPOV #:

About the slippage I already have at the beginning of the EA

This slippage is exactly for this function...so you don't have to go through BUY or SELL

//--- global parameters
int max_ticket = 0;
int min_ticket = INT_MAX;
//+----------------------------------------------------------------------------+
void ClosseAll()
{
  int slipp = MarketInfo(_Symbol,MODE_SPREAD)*2;
  for(int i = OrdersTotal()-1; i>=0; i--)
  {
     if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
     {
      if (OrderClose(max_ticket , OrderLots(), Bid, slipp)&&OrderClose(min_ticket , OrderLots(), Bid, slipp))
            Print("OK");
      else
            Print("Не удалось закрыть ордер! ",GetLastError());
     }
  }
}
//+----------------------------------------------------------------------------+
//| Расчет профита максимального ордера в сетке                                |
//+----------------------------------------------------------------------------+
double GetProfitMaxOrder()
{
   double max_ticket_profit = 0 ;
      {
      for (int cnt = OrdersTotal() - 1; cnt >= 0; cnt--) 
         {
         if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
            {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
               {
               if(OrderType() == OP_BUY || OrderType() == OP_SELL)
                  {
                  if(OrderTicket() > max_ticket) 
                     {
                     max_ticket = OrderTicket();
                     max_ticket_profit =  OrderProfit();
                     }
                  }
               }
            }
         }
      }
   return( max_ticket_profit);
}
//+----------------------------------------------------------------------------+
//| Расчет профита минимального ордера в сетке                                 |
//+----------------------------------------------------------------------------+
double GetProfitMinOrder()
{
   double min_ticket_profit = 0;
      {
      for (int cnt = OrdersTotal() - 1; cnt >= 0; cnt--) 
         {
         if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
            {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) 
               {
               if (OrderType() == OP_BUY || OrderType() == OP_SELL)
                  {
                  if(OrderTicket() < min_ticket)
                     {
                     min_ticket = OrderTicket();
                     min_ticket_profit = OrderProfit(); 
                     }
                  }
               }
            }
         }
      }
   return(min_ticket_profit);
 
How better to create a class object: after class description or locally, for example in OnTick()? The second option is probably not very economical: CPU and RAM load.
 
MakarFX #:

This slippage is just for this function...so you don't have to go through BUY or SELL

int slipp = MarketInfo(_Symbol,MODE_SPREAD)*2;

You don't need to multiply by a point here

possible loss of data due to type conversion NEVALASHKA.mq4 376 13

I get a warning at compile time


 
EVGENII SHELIPOV #:

There is no need to multiply by a point

possible loss of data due to type conversion NEVALASHKA.mq4 376 13

I get a warning at compile time


Do not multiply MODE_SPREAD - Spread in pips

you can check

   Print(MarketInfo(_Symbol,MODE_SPREAD)," / ",MarketInfo(_Symbol,MODE_SPREAD)*Point);

and make like this

int slipp = (int) MarketInfo(_Symbol,MODE_SPREAD)*2;
Reason: