Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1283

 
Youri Lazurenko:


If there is no full code, I will not be able to help you. Please attach the full code (attach using the Attach file button )

 
If in void OnTick(), it looks like this:
   if(currentTime >= OpenTrade && currentTime < CloseTrade)
   {
      if(SignalBuy1() || SignalBuy2())
      { 
         if(timeBarInside != iTime(NULL, 0, 1))
         {
            OpenOrderBuy();
            PositionModifiBuy();
            CandleColor(ColorCandleBuy);
            timeBarInside = iTime(NULL, 0, 1);
         }
      }
      
      if(SignalSell1() || SignalSell2()) 
      {
         if(timeBarInside != iTime(NULL, 0, 1))
         {
            OpenOrderSell();
            PositionModifiSell();
            CandleColor(ColorCandleSell);
            timeBarInside = iTime(NULL, 0, 1);
         }
      }  
   }
What does full code mean? The entire EA? It's quite cumbersome, and the rest of the functions have nothing to do with the modification. I understand that we can immediately set SL and TP when a position is opened, but I want to do it that way, I'm already wondering why it doesn't work. In mql4 it's done like two fingers... and here it's a pain in the ass.
 

And back to drawing the trend line. If you need a code.

bool CreateLine(string name, datetime time1, double price1, datetime time2, double price2, color clr)
{
   ObjectDelete(0, name);  
      
   if(ObjectCreate(0, name, OBJ_TREND, 0, time1, price1, time2, price2))
   {
      ObjectSetInteger(0, name, OBJPROP_COLOR, clr); 
      ObjectSetInteger(0, name, OBJPROP_STYLE, TrendStyle); 
      ObjectSetInteger(0, name, OBJPROP_WIDTH, TrendWidth); 
      
      ChartRedraw(); 
      Sleep(10);
      
      return(true);
   }
   else 
   {
      Print("Ошибка в создании линии"); 
      return (false);
   }
}
Files:
1.png  82 kb
2.png  36 kb
 
Youri Lazurenko:

And back to drawing the trend line. If you need code.

Read documentation!!!

OBJ_TREND

Note

The mode of trend line extension to the right and/or to the left can be specified (OBJPROP_RAY_RIGHT and OBJPROP_RAY_LEFT properties respectively).

By default, these properties are set to false.

So my question is: Why delete the object and create it again? Isn't it easier to create only if no such object is found on the chart? And even if it is necessary to change the anchor coordinates, there is no need to delete the object and create it again.

 
Youri Lazurenko:


For the third and final time: please attach the full code.

 
Alexey Viktorov:

Read the documentation!!!

These properties are set to false by default.

And question: Why delete an object and create it again? Isn't it easier to create only if no such object is found on the chart? And even if it's necessary to change the anchor coordinates, there's no need to delete the object and create it again.

Thanks, I'll have a look now. And thanks for the valuable comments. I'm not a programmer, just for myself, I often use someone else's work.

 
Vladimir Karputov:

Please for the third and final time: attach the full code.

Already solved the problem. I modify via Tisket, not Symbol. I don't understand what the full code has to do with, for example, what does the position opening condition have to do with modification?

And now it looks like this, maybe someone will need it.

void PositionModifiSell()
{
   int    i;
   ulong  ticket;
   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)
               {
                  ticket = m_position.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(ticket, sl, tp))
                  {
                     Print("Метод PositionModify() потерпел поражение. Код возврата = ", m_trade.ResultRetcode(),
                           ". Описание кода: ", m_trade.ResultRetcodeDescription());
                  }
                  else
                  {
                     Print("Метод PositionModify() исполнен успешно. Код возврата = ", m_trade.ResultRetcode(),
                           " (", m_trade.ResultRetcodeDescription(),")");
                  }
               }
            }
         }
      }
   }
}
 
Youri Lazurenko:


Study the help - what is the difference between selecting a position by ticket and by symbol. It will come in handy so you don't make mistakes:

PositionSelect

bool  PositionSelect(
   string  symbol     // имя инструмента
   );

Note

With "netting" position accounting (ACCOUNT_MARGIN_MODE_RETAIL_NETTING andACCOUNT_MARGIN_MODE_EXCHANGE) only oneposition can be opened persymbol at any given time, which results from one or moretrades. Positions should not be confused with activepending orders which are also displayed in the "Trade" tab of the "Toolbox" panel.

When the positions are displayed independently (ACCOUNT_MARGIN_MODE_RETAIL_HEDGING), several positions can be opened for each symbol simultaneously. In this case, PositionSelect will select the position with the smallest tick.

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

Read the documentation!!!

These properties are set to false by default.

And question: Why delete an object and create it again? Isn't it easier to create only if no such object is found on the chart? And even if it is necessary to change the anchor coordinates, there is no need to delete the object and create it again.

Thanks again, now the line is like a line, nice to see. But I needObjectDelete(0, name); otherwise the line once drawn is not redrawn. Same in mql4, just forgot.

 
Youri Lazurenko:

Thanks again, now the line is like a line, nice to see. OnlyObjectDelete(0, name);; is needed, otherwise once drawn the line is not redrawn. The same in mql4, I just forgot.

What an absurd idea. The drawn graphical object moves smoothly. An example is given in help for each object (for horizontal lineOBJ_HLINE).

You can also change any property to an already drawn object: colour, style , smell ...
Документация по MQL5: Константы, перечисления и структуры / Константы объектов / Типы объектов / OBJ_HLINE
Документация по MQL5: Константы, перечисления и структуры / Константы объектов / Типы объектов / OBJ_HLINE
  • www.mql5.com
OBJ_HLINE - Типы объектов - Константы объектов - Константы, перечисления и структуры - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
Reason: