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

 
d1w4m3:


Thanks for the answer, but I've only just started learning for a couple of weeks, how to implement this in code, or tell me where to read please. Or according to Igor Kim?

And yet it will open a position, not according to the system, when just starting its work, your condition does not avoid this problem.

And if you think about it? Why doesn't it avoid the problem?

When he starts work, and if he has not worked before (in general the first run), then there has not been his position yet, and logic will show that the last closed position simply does not exist yet. This means that it could not have been closed at either stop or take, which in turn gives permission to open a position on the signal.

Isn't it?

 
Artyom Trishkin:

And if you think about it? Why doesn't it avoid the problem?

When it starts working, and if it has not worked before (first run at all), there has not yet been a position, and logic will show that the last closed position simply does not exist yet. This means that it could not have been closed at either stop or take, which in turn gives permission to open a position on the signal.

Isn't it?


That is the thing: by a signal! But the condition may be met not on bar 2 (as soon as the indicator has changed the buffer) but on any second bar (after that everything works through the system of course). The problem is in the condition to open orders, how should we fix it only on the 2nd bar after the change of the indicator buffer?

void OnTick()

{
uptr = NormalizeDouble(iCustom(NULL, 0, "Slope Direction Line", period, method, price, 0, n), 4);
dntr = NormalizeDouble(iCustom(NULL, 0, "Slope Direction Line", period, method, price, 1, n), 4);
if(DN_Trend()==true)
{
if(CountSell() == 0 && Bid < dntr && Open[3] < dntr && Close[3] < dntr)
{
ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, "Sloper", Magic, 0, Red);
if (ticket > 0)
{
SL = NormalizeDouble(Bid + StopLoss*Point, Digits);
TP = NormalizeDouble(Bid - TakeProfit*Point, Digits);
if (OrderSelect(ticket, SELECT_BY_TICKET))
if(!OrderModify(ticket, OrderOpenPrice(), SL, TP, 0))
Print("Sales error");
}

}


 
d1w4m3:


Well, the point is that it is based on the signal! But the condition may satisfy not on the 2nd bar (as soon as the indicator has changed the buffer) but on any second bar (after that everything works with the system of course). The problem is in the condition to open orders, how to fix it but on the 2nd bar after the change of the indicator buffer?

void OnTick()

{
uptr = NormalizeDouble(iCustom(NULL, 0, "Slope Direction Line", period, method, price, 0, n), 4);
dntr = NormalizeDouble(iCustom(NULL, 0, "Slope Direction Line", period, method, price, 1, n), 4);
if(DN_Trend()==true)
{
if(CountSell() == 0 && Bid < dntr && Open[3] < dntr && Close[3] < dntr)
{
ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, "Sloper", Magic, 0, Red);
if (ticket > 0)
{
SL = NormalizeDouble(Bid + StopLoss*Point, Digits);
TP = NormalizeDouble(Bid - TakeProfit*Point, Digits);
if (OrderSelect(ticket, SELECT_BY_TICKET))
if(!OrderModify(ticket, OrderOpenPrice(), SL, TP, 0))
Print("Sales error");
}

}


Insert the code correctly. There is an SRC button in the message editor menu.
I didn't understand anything from your explanations.
 
d1w4m3:


That's the point! But the condition may be met not on bar 2 (as soon as the indicator has changed the buffer), but on any second bar (after that everything works with the system of course). The problem is in the condition to open orders, how to fix it but on the 2nd bar after the change of the indicator buffer?

void OnTick()
{
  uptr = NormalizeDouble(iCustom(NULL, 0, "Slope Direction Line", period,  method, price, 0, n), 4);
  dntr = NormalizeDouble(iCustom(NULL, 0, "Slope Direction Line", period,  method, price, 1, n), 4); 
  
if(DN_Trend()==true)
{
  if(CountSell() == 0 && Bid < dntr && Open[3] < dntr && Close[3] < dntr)
  { 
   ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, "Sloper", Magic, 0, Red);
      if (ticket > 0)
      {
         SL = NormalizeDouble(Bid + StopLoss*Point, Digits);
         TP = NormalizeDouble(Bid - TakeProfit*Point, Digits);
         
         if (OrderSelect(ticket, SELECT_BY_TICKET))
         if(!OrderModify(ticket, OrderOpenPrice(), SL, TP, 0))
           Print("Ошибка на продажу");
      }

  }


1. this is what the code would look like if inserted correctly. There's a reason the developers came up with this...

2. a subtle hint: where are Open[4] and Close[4] relative to the indicator values?

 
extern int TakeProfit  = 200;
extern double Lots     = 0.1;
extern int StopLoss    = 52;
extern int Magic       = 1111;
extern int Slippage    = 3;
extern int n           = 3; // На какую свечу открывать ордер
//------------------------------------------------------------------
extern int  LevelWLoss  = 1;      // Уровень безубытка
extern int  LevelProfit = 30;     // Уровень профита
//------------------------------------------------------------------
extern string    Slope  = "Параметры Slope";
extern int       period = 163; 
extern int       method = 3;                         
extern int       price  = 0;    
//------------------------------------------------------------------
double uptr, SL, TP, dntr;
int    ticket, nd;
bool   fm;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   if (Digits == 3 || Digits == 5)
   {
       TakeProfit  *=10;
       StopLoss    *=10;
       Slippage    *=10;
       LevelWLoss  *=10;
       LevelProfit *=10;
   }
   return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{

}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
  uptr = NormalizeDouble(iCustom(NULL, 0, "Slope Direction Line", period,  method, price, 0, n), 4);
  dntr = NormalizeDouble(iCustom(NULL, 0, "Slope Direction Line", period,  method, price, 1, n), 4); 
  
if(DN_Trend()==true)
{
  if(CountSell() == 0 && Bid < dntr && Open[3] < dntr && Close[3] < dntr)
  { 
   ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, "Sloper", Magic, 0, Red);
      if (ticket > 0)
      {
         SL = NormalizeDouble(Bid + StopLoss*Point, Digits);
         TP = NormalizeDouble(Bid - TakeProfit*Point, Digits);
         
         if (OrderSelect(ticket, SELECT_BY_TICKET))
         if(!OrderModify(ticket, OrderOpenPrice(), SL, TP, 0))
           Print("Ошибка на продажу");
      }
  }
}//if(DN_Trend()==true)

if(UP_Trend()==true)
{
   if( CountBuy() == 0  && Ask > uptr && Open[3] > uptr && Close[3] > uptr  )
  {
   ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, "Sloper", Magic, 0, Blue);
      if (ticket>0)
      {
         TP = NormalizeDouble(Ask + TakeProfit*Point, Digits);
         SL = NormalizeDouble(Ask - StopLoss*Point, Digits);
         
         if (OrderSelect(ticket, SELECT_BY_TICKET))
            if(!OrderModify(ticket, OrderOpenPrice(), SL, TP, 0))
               Print("Ошибка на покупку");
       }
    }
}//if(UP_Trend()==true)

      if ( CountSell()>0  && Open[3] > uptr && Close[3] > uptr && UP_Trend()==true)
      {
      for(int i = OrdersTotal() -1; i>=0; i--)
          {
            if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
            {
            if (OrderMagicNumber() == Magic && OrderType() == OP_SELL)
            if(!OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Black))
            Print("Ошибка");
            }
          }
      }
      if ( CountBuy()>0 && Open[3] < dntr && Close[3] < dntr && DN_Trend()==true)
      {
      for(int i = OrdersTotal() -1; i>=0; i--)
          {
            if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
            {
            if (OrderMagicNumber() == Magic && OrderType() == OP_BUY)
            if(!OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Black))
            Print("Ошибка");
            }
          }
      }
      //+переход в безубыток+
   for( int i=0; i<OrdersTotal(); i++) 
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
        {
        if(OrderSymbol()== Symbol() && OrderMagicNumber()==Magic)
          {
            if(OrderType()==OP_BUY) 
              {
                if(OrderStopLoss()-OrderOpenPrice()<LevelWLoss*Point) 
                 {
                   if(Bid-OrderOpenPrice()>LevelProfit*Point) 
                 {
                  fm=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+LevelWLoss*Point,OrderTakeProfit(),CLR_NONE);
                 }
              }
            }
           }
         if(OrderType()==OP_SELL) 
           {
            if(OrderStopLoss()==0 || OrderOpenPrice()-OrderStopLoss()<LevelWLoss*Point) 
              {
               if(OrderOpenPrice()-Ask>LevelProfit*Point) 
                 {
                  fm=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-LevelWLoss*Point,OrderTakeProfit(),CLR_NONE);
                 }
              }
           }
       
        }
     }
     
//------------  комментарии -----------------+     
  if(UP_Trend()) Comment("Восходящий тренд ", uptr);
  if(DN_Trend()) Comment("Нисходящий тренд ", dntr);
 
 }
//+------------------------------------------------------------------+
int CountSell()
{
   int count = 0;
   for (int trade = OrdersTotal()-1; trade>=0; trade--)
   {
        if(OrderSelect(trade, SELECT_BY_POS, MODE_TRADES))
    {
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == OP_SELL)
                    count++;   
    }
   }
     return(count);
}
//+------------------------------------------------------------------+
int CountBuy()
{
   int count = 0;
   for (int trade = OrdersTotal()-1; trade>=0; trade--)
   {
        if(OrderSelect(trade, SELECT_BY_POS, MODE_TRADES))
    {
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == OP_BUY)
                    count++;   
    }
   }
     return(count);
}
//+------------------------------------------------------------------+
bool UP_Trend()
{
  if(uptr > 0 && uptr != 2147483647.0) return(true);
  return(false);
}
//+------------------------------------------------------------------+
bool DN_Trend()
{
  if(dntr > 0 && dntr != 2147483647.0) return(true);
  return(false);
}
//+------------------------------------------------------------------+
 

If we insert your condition " This means that it could not be closed at either stop or take, which in turn gives permission to open a position on the signal. "

And my signal is"if(CountSell() == 0 && Bid < dntr && Open[3] < dntr && Close[3] < dntr)" (together with the shift in the indicator n=3),

It turns out that if it is placed immediately on the chart, it will open on a downtrend, from the moment of start on the 3rd candle, and then, if there is a signal, it will open as needed.

 
d1w4m3:

If we insert your condition " This means that it could not be closed at either stop or take, which in turn gives permission to open a position on the signal. "

And my signal is"if(CountSell() == 0 && Bid < dntr && Open[3] < dntr && Close[3] < dntr)" (together with the shift in the indicator n=3),

It turns out that if it is placed immediately on the chart it opens on a downtrend at the moment of start on the 3rd candle, and then, if there is a signal, it opens as required.

I do not understand anything. What do you have that you do not understand? You cannot determine that the last position was closed at stop or take? You cannot find a place in your code where to check this condition?

Or what can't you do?

How can it open on the third candle? Or explain what "it will open on the third candle" means? What does that even mean?
 

each time an order opens on some indicator signal, how do I store the type of this signal in a variable?

There is a bug (when I run the EA), the order does not open at the start of a trend (not according to a change in the colour of the indicator), because my condition does not refer to specific candles, but says if the trend is up or down opens at the start of 3 candles


 

Good afternoon to all.

I am wondering how to analyse work statistics in EXCEl

Who can tell me what can be pulled through DDE from mt4 and where can I get all permissible functions for transferring to excel

Will be very grateful for help

 
Maxim Shershnev:

Good afternoon to all.

I am wondering how to analyse work statistics in EXCEl

Who can tell me what can be pulled through DDE from mt4 and where can I get all permissible functions for transferring to excel

Will be very grateful for help

There is everything you need. The .csv record format writes to excell
Файловые операции - Справочник MQL4
Файловые операции - Справочник MQL4
  • docs.mql4.com
Файловые операции - Справочник MQL4
Reason: