Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1282

 
Vladimir Karputov:

The problem is with the EA. By the way, the rules on shared hosting prohibit dlls.

It used to work just fine. I should have tried it on free hosting. Anyway gave methaquotes 15 quid )))

Thanks for the reply!

 

The EA opens market orders and then modifies them, sets TP and SL. Checking in the tester. The first order is modified using the required parameters. The second order is opened in the same direction as the first one but the first order is modified according to the second one's parameters and the second one remains with zero SL and TP. In this case the condition is: if SL == 0 and TP == 0, the other order is modified because it has non-zero parameters. Why? Changing the search from the first to the last one does not change anything.

One more thing. In the tester, if two orders are opened, the order profit is shown in the column profit, i.e. the profit of the second order is shown in the first open order and vice versa. Why?

There is more. We need to draw a trend line following two extrema. The line is drawn but it is a stub from bar to bar. It is not a trend line but just a segment while we would like to see a line.

void PositionModifiSell()
{
   int    i;
   double sl    = 0; 
   double tp    = 0;  
   int    total = PositionsTotal();
   
   for(i = total-1; i >= 0; i--)
   {
      if(m_position.SelectByIndex(i))                            
      {
         if(m_position.Symbol() == Symbol() && m_position.Magic() == Magic)
         {
            if(m_position.PositionType() == POSITION_TYPE_SELL)
            {
               if(m_position.StopLoss() == 0 && m_position.TakeProfit() == 0)
               {
                  sl = NormalizeDouble(iHigh(NULL, 0, ExtrimUp()) + VS*_Point, _Digits);                
                                    
                  if(PriceCurrentDw1() - PriceDwHigh() < LastPriceOpenOrder() - (sp+PW)*_Point)
                  {
                     tp = NormalizeDouble(PriceCurrentDw1() - PriceDwHigh(), _Digits);
                  }
                  else
                     tp = NormalizeDouble(bid - TP*_Point, _Digits);
                  
                  if(!m_trade.PositionModify(_Symbol, sl, tp))
                  {
                     Print("Метод PositionModify() потерпел поражение. Код возврата = ", m_trade.ResultRetcode(),
                           ". Описание кода: ", m_trade.ResultRetcodeDescription());
                  }
                  else
                  {
                     Print("Метод PositionModify() исполнен успешно. Код возврата = ", m_trade.ResultRetcode(),
                           " (", m_trade.ResultRetcodeDescription(),")");
                  }
               }
            }
         }
      }
   }
}
 
Youri Lazurenko:

The EA opens market orders and then modifies them, sets TP and SL. Checking in the tester. The first order is modified using the required parameters. The second order is opened in the same direction as the first one but the first order is modified according to the second one's parameters and the second one remains with zero SL and TP. In this case the condition is: if SL == 0 and TP == 0, the other order is modified because it has non-zero parameters. Why? Changing the search from the first to the last one does not change anything.

One more thing. In the tester, if two orders are opened, the order profit is shown in the column profit, i.e. the profit of the second order is shown in the first open order and vice versa. Why?

There is more. We need to draw a trend line following two extrema. The line is drawn but it is a stub from bar to bar. It is not a trend line, just a segment while we would like to see a line.

1. Not ORDERS, but POSITIONS.

2. Formulate your question - so far I've seen a narrative, and I haven't seen a question or an algorithm for "how one would like it to work".

 
Vladimir Karputov:

1. Not ORDERERS, but POSITIONS

2. Formulate your question - so far I've seen a narrative, and I haven't seen the question and algorithm "how you want it to work".

You know it's positions, since it's in mql5. And there is a position in the code. The question is why the first position is modified and not the last one, even though it's specified in the condition:

if(m_position.StopLoss() == 0 && m_position.TakeProfit() == 0)
 
Youri Lazurenko:

It's clear that the positions are there, since it's in mql5. And in the code it is position. The question is why the first position is modified and not the last, even though it's specified in the condition:

The question is where. Describe the algorithm "I want the algorithm to be this ...".

 
Vladimir Karputov:

The question is where??? Describe the algorithm "I want the algorithm to be like this ...".

When the trend line is broken and the bar closes above/below it, we open a position with zero SL and TP. Immediately after the opening, the necessary TP and SL are set (the position is modified). Why, it is not the last position that is modified, but the previous one, while violating the condition:

if(m_position.StopLoss() == 0 && m_position.TakeProfit() == 0)

Is it clear now? Even if we introduce the time of opening a position into the condition, nothing changes. There is no limit on the number of positions.

Документация по MQL5: Константы, перечисления и структуры / Торговые константы / Свойства позиций
Документация по MQL5: Константы, перечисления и структуры / Торговые константы / Свойства позиций
  • www.mql5.com
Свойства позиций - Торговые константы - Константы, перечисления и структуры - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Youri Lazurenko:

When the trend line is broken and the bar closes above/below it, a position with zero SL and TP is opened. Immediately after the opening, the necessary TP and SL are set (the position is modified). Why, it is not the last position that is modified, but the previous one, while violating the condition:

Is it clear now? Even if we introduce the time of opening a position into the condition, nothing changes. There is no limit on the number of positions.

Are you sure that at the moment of modification a new position already exists? (Sending a trade order is not a guarantee that the position exists).

When entering the condition (SL == 0.0 and TP == 0.0) print the position ticket.

 
Vladimir Karputov:

Are you sure that the new position is already available at the time of modification? (Sending a trade order is not a guarantee that a position exists).

When you enter the condition (SL == 0.0 and TP == 0.0) print the position ticket.

Yes, the second position is already there. I am reading the log. The Sell method was successful, code 10009. (Ticket #3). And then follows a modification, only of the position with ticket #2, which is also executed successfully (modification, although, I emphasize, according to the conditions should not happen, SL is no longer 0!).

Already wrote, tried both by time of opening, and by ticket, no luck, modifies previous position. For example:

void PositionModifiSell()
{
   int      i;
   datetime Time;
   double   sl    = 0; 
   double   tp    = 0;  
   datetime time  = 0;
   int      total = PositionsTotal();
   
   for(i = total-1; i >= 0; i--)
   {
      if(m_position.SelectByIndex(i))                            
      {
         if(m_position.Symbol() == Symbol() && m_position.Magic() == Magic)
         {
            if(m_position.PositionType() == POSITION_TYPE_SELL)
            {
               Time = m_position.Time();
               
               if(Time > time)
               {
                  sl = NormalizeDouble(iHigh(NULL, 0, ExtrimUp()) + VS*_Point, _Digits);
                  
                  if(PriceCurrentDw1() - PriceDwHigh() < LastPriceOpenOrder() - (sp+PW)*_Point)
                  {
                     tp = NormalizeDouble(PriceCurrentDw1() - PriceDwHigh(), _Digits);
                  }
                  else
                     tp = NormalizeDouble(bid - TP*_Point, _Digits);
                  
                  if(!m_trade.PositionModify(_Symbol, sl, tp))
                  {
                     Print("Метод PositionModify() потерпел поражение. Код возврата = ", m_trade.ResultRetcode(),
                           ". Описание кода: ", m_trade.ResultRetcodeDescription());
                  }
                  else
                  {
                     Print("Метод PositionModify() исполнен успешно. Код возврата = ", m_trade.ResultRetcode(),
                           " (", m_trade.ResultRetcodeDescription(),")");
                  }
                  time = Time;
               }
            }
         }
      }
   }
}
//------------ или
void PositionModifiSell()
{
   int      i;
   ulong  Ticket;
   double sl     = 0; 
   double tp     = 0;  
   ulong  ticket = 0;
   int    total  = PositionsTotal();
   
   for(i = total-1; i >= 0; i--)
   {
      if(m_position.SelectByIndex(i))                            
      {
         if(m_position.Symbol() == Symbol() && m_position.Magic() == Magic)
         {
            if(m_position.PositionType() == POSITION_TYPE_SELL)
            {
               Tiсket = m_position.Ticket();
               
               if(ticket != Ticket)
               {
                  sl = NormalizeDouble(iHigh(NULL, 0, ExtrimUp()) + VS*_Point, _Digits);
                  
                  if(PriceCurrentDw1() - PriceDwHigh() < LastPriceOpenOrder() - (sp+PW)*_Point)
                  {
                     tp = NormalizeDouble(PriceCurrentDw1() - PriceDwHigh(), _Digits);
                  }
                  else
                     tp = NormalizeDouble(bid - TP*_Point, _Digits);
                  
                  if(!m_trade.PositionModify(_Symbol, sl, tp))
                  {
                     Print("Метод PositionModify() потерпел поражение. Код возврата = ", m_trade.ResultRetcode(),
                           ". Описание кодa: ", m_trade.ResultRetcodeDescription());
                  }
                  else
                  {
                     Print("Метод PositionModify() исполнен успешно. Код возврата = ", m_trade.ResultRetcode(),
                           " (", m_trade.ResultRetcodeDescription(),")");
                  }
                  ticket = Ticket;
               }
            }
         }
      }
   }
}
Документация по MQL5: Константы, перечисления и структуры / Торговые константы / Свойства позиций
Документация по MQL5: Константы, перечисления и структуры / Торговые константы / Свойства позиций
  • www.mql5.com
Свойства позиций - Торговые константы - Константы, перечисления и структуры - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Youri Lazurenko:

Yes, the second position is already there. I'm reading the log. The Sell method was successful, code 10009. (Ticket #3). And then the modification follows, only the position with ticket #2, which is also done successfully.

Already wrote, tried both by opening time and by ticket, no way, modifies previous position. For example:


That is: You don't check the result of the position and you don't even want to print the ticket. If this is the case, attach the full code (attach using the button Attach file)

 
Vladimir Karputov:

That is: You do not check the result of the position and you do not even want to print the ticket. In that case, attach the full code (attach it using the button )

You don't even want to print the ticket - I don't get it. Here is the log extract.

2021.02.01 12:54:55.338 2018.01.02 17:00:00 market sell 0.01 EURUSD (1.20384 / 1.20402)

2021.02.01 12:54:55.338 2018.01.02 17:00:00 deal #2 sell 0.01 EURUSD at 1.20384 done (based on order #2)

2021.02.01 12:54:55.338 2018.01.02 17:00:00 deal done [#2 sell 0.01 EURUSD at 1.20384]

2021.02.01 12:54:55.338 2018.01.02 17:00:00 order performed sell 0.01 at 1.20384 [#2 sell 0.01 EURUSD at 1.20384]

2021.02.01 12:54:55.344 2018.01.02 17:00:00 CTrade::OrderSend: market sell 0.01 EURUSD [done at 1.20384]

2021.02.01 12:54:55.344 2018.01.02 17:00:00 Sell() method successfully completed. Return code = 10009 (done at 1.20384)

2021.02.01 12:54:55.344 2018.01.02 17:00:00 position modified [#2 sell 0.01 EURUSD 1.20384 sl: 1.20813 tp: 1.20013]

2021.02.01 12:54:55.346 2018.01.02 17:00:00 CTrade::OrderSend: modify position #2 EURUSD (sl: 1.20813, tp: 1.20013) [done]

2021.02.01 12:54:55.346 2018.01.02 17:00:00 PositionModify() method executed successfully. Return code = 10009 (done).

============================================================================================================== разделил, появление второй позиции

2021.02.01 12:55:48.819 2018.01.03 09:00:00 market sell 0.01 EURUSD (1.20442 / 1.20460)

2021.02.01 12:55:48.819 2018.01.03 09:00:00 deal #3 sell 0.01 EURUSD at 1.20442 done (based on order #3)

2021.02.01 12:55:48.819 2018.01.03 09:00:00 deal done [#3 sell 0.01 EURUSD at 1.20442]

2021.02.01 12:55:48.819 2018.01.03 09:00:00 order performed sell 0.01 at 1.20442 [#3 sell 0.01 EURUSD at 1.20442]

2021.02.01 12:55:48.821 2018.01.03 09:00:00 CTrade::OrderSend: market sell 0.01 EURUSD [done at 1.20442]

2021.02.01 12:55:48.821 2018.01.03 09:00:00 Sell() method successfully executed. Return code = 10009 (done at 1.20442)

====================================================================================================================== after successfully opened position, modification, but previous order

2021.02.01 12:55:48.821 2018.01.03 09:00:00 position modified [#2 sell 0.01 EURUSD 1.20384 sl: 1.20663 tp: 1.20154]

2021.02.01 12:55:48.823 2018.01.03 09:00:00 CTrade::OrderSend: modify position #2 EURUSD (sl: 1.20663, tp: 1.20154) [done]

2021.02.01 12:55:48.823 2018.01.03 09:00:00 PositionModify() method executed successfully. Return code = 10009 (done)


I've posted the full position modification code on Sell, with options.

Совершение сделок - Торговые операции - Справка по MetaTrader 5
Совершение сделок - Торговые операции - Справка по MetaTrader 5
  • www.metatrader5.com
Торговая деятельность в платформе связана с формированием и отсылкой рыночных и отложенных ордеров для исполнения брокером, а также с управлением...
Reason: