Questions from Beginners MQL5 MT5 MetaTrader 5 - page 363

 
Tapochun:
I don't think you're asking the question here. Create a separate thread, attach the code of the indicator there and hope someone will help. Without the code... it's unlikely that anyone will help you.
Indicator doesn't buy or sell).
 
When writing the ToR, I came across a situation where I don't understand how to describe the algorithm correctly. Can you tell me if it is possible to partially close a position in the EA's work?
 
Nikolay Lampickiy:
When writing the ToR, I came across a situation where I don't understand how to describe the algorithm correctly. Can you advise on the possibility of partial closing of a position in the EA's work?
In order to do that, in the order to close a position, specify a lot smaller than the position's lot (this is if MT4, in MT5 you can open an opposite position with a smaller lot, and they will overlap in the end). The position with the remaining lot will remain in the market.
 
Nikolay Lampickiy:
When writing the ToR, I came across a situation where I don't understand how to describe the algorithm correctly. Can you tell me whether a partial closing of a position is possible in the work of an EA?
What is so complicated about it? If position is not evenly split (eg 0.09) then close most of it (0.05) and move stop to breakeven, then another half of the closed position (0.02) and put put put to profit and vice versa until the entire position is closed or broke even. The step can be fixed or calculated from any range, depends on the trading system.
 
Vladimir Zubov:
What is so complicated about it? If the position is not evenly split (eg 0.09) then close most of it (0.05) and move the stop to breakeven, then another closing distance and again half of the one left (0.02) and move the stop to the next closing step and keep going up until the whole position is closed or broke even. The step can be fixed or calculated from any range, depends on the trading system.

Do I get it right? The MQL4 programming language allows formalizing the closing of only half of the market order volume... like lot 10 and close 5 lots when price reaches a certain level.

Or is it possible to close 1/3?

Or, it would be easier to implement this idea in an EA by placing different orders. When the price reaches a required level, one order is closed and the other remains in place of the partial closing of the market order volume. What do you think?

 
Vitalie Postolache:
To do this, you need to specify a lot smaller than the position's lot in the close order (this is if MT4, in MT5 you can open the opposite position with a smaller lot, and they will overlap in the end). The position with the remaining lot will remain in the market.
I am not satisfied with the opposite position... thank you for your reply.
 
Nikolay Lampickiy:

Do I get it right? The MQL4 programming language allows formalizing the closing of only half of the market order volume... like lot 10 and close 5 lots when price reaches a certain level.

Or is it possible to close 1/3?

Or, it would be easier to implement this idea in an EA by placing different orders. When the price reaches a required level, one order is closed and the other remains in place of the partial closing of the market order volume. What do you think?

We can do both. OrderClose allows you to close the volume that is indicated to it. https://docs.mql4.com/ru/trading/orderclose

Of course you can open several orders with different profit levels. In case of a connection failure it will be even more reliable, because TP levels are stored on the server and will be executed if the price intersects them.

OrderClose - Документация на MQL4
  • docs.mql4.com
OrderClose - Документация на MQL4
 
Vladimir Zubov:

You can do both. OrderClose allows to close the volume, which is indicated to it. https://docs.mql4.com/ru/trading/orderclose

Of course, you can also open several orders with different profit levels. In case of a connection failure this will be even more reliable, because TP levels are stored on the server and will be executed if the price crosses them.

Thanks for the reply)))
 

Hello all, I'm facing a problem, I'd like to hear the opinion of professionals or more experienced MT5 users. I have written an indicator and CEventBase class, which allows the indicator to generate events. When running Expert Advisors that process these events sooner or later, when running in real time, I get errors: 4001, 4102, 4104. When they start to "pile up", they won't stop happening) I attach the code of Generate function, which actually is the source of all these troubles. Would be very glad to hear your opinion...)

Good day to all!


bool CEventBase::Generate(long _chart_id, int _id_ind, SEventData &_data,
                          const bool _is_custom=true)
  {
   bool is_generated = true;
   // если индекс id события в массиве не верен
   if (_id_ind < 0 || _id_ind >= aEvents.Total())
    {
     Print("Не верно задан индекс ID события");
     return (false);
    }
   // заполняем поля 
   CEvent *event = aEvents.At(_id_ind);
   this._id = (ushort)(CHARTEVENT_CUSTOM+event.id);
   this._data = _data;
   this._data.sparam = event.name; // сохраняем имя события
   
   if(_is_custom)
     {
      ResetLastError();
      is_generated = EventChartCustom(_chart_id, event.id, this._data.lparam,
                                      this._data.dparam, this._data.sparam);
      if(!is_generated && _LastError != 4104)
         {
          Print("is_generated = ", is_generated);
          PrintFormat("%s Error while generating a custom event: %d", __FUNCTION__,_LastError);
          Print( ChartSymbol(_chart_id)," ",PeriodToString(ChartPeriod(_chart_id)), "Ошибка! _chart_id =", _chart_id, " event.id = ", event.id, " data.dparam = " ,this._data.dparam, " data.sparam = ", this._data.sparam);
          
         }
     }
   return is_generated;
  }


 
dariamap:

Hello all, I'm facing a problem, I'd like to hear the opinion of professionals or more experienced MT5 users. I have written an indicator and CEventBase class, which allows the indicator to generate events. When running Expert Advisors that process these events sooner or later, when running in real time, I get errors: 4001, 4102, 4104. When they start to "pile up", they don't stop doing it) I attach the code of Generate function, which actually is the source of all these troubles. I'll be very glad to hear your opinion on it ...)

Good day everyone!

Obviously, the method you presented is a transit method. The error is most likely not in it. It is not clear from the data you presented what is going on. There may be several variants:

  • Incorrect chart identifier (which is what the 4101 error says).
  • Too frequent queries;
  • Incorrect other parameters (event IDs for example).

Judging by the level of code you wrote, we can say that the error can be anywhere and anyplace.