Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1206

 
Artyom Trishkin:

Resize the nnc_arr_in array by 1 more than its size in cell 0 of the structures array:

Thank you.

 

Question mql5

There are two open positions and a signal(Flag==true)

only one position should be closed by this signal, i.e. the one with the larger profit.

How to close only the one with the higher profit?

What is the best way to handle this event?

I am trying to do it this way:

if (invertStart == true) // получил событие, о том что есть две позиции и одну можно закрыть.
   {     
   int npos_L1=0;   NPos_L1(npos_L1);
   if(npos_L1==1)
   {
   int positions=PositionsTotal();
   for(int i=0;i<=positions;i++)
   {
   ulong ticket=PositionGetTicket(i);
   if(ticket!=0 && PositionGetInteger(POSITION_TYPE)== POSITION_TYPE_SELL && POSITION_PROFIT>=50) //  50  допустим...
   {
   m_trade.PositionClose(ticket); 
   i--;
   invertStart = false;
   return;  
   }
   }
   }
   }  

The last open position will be closed as a result.

Please advise how to do it correctly, I would like to have an example. I read reference books and primers, but I don't always understand the subtleties.

 
Vladpedro:

Question mql5

There are two open positions and a signal(Flag==true)

only one position should be closed by this signal, i.e. the one with the larger profit.

How to close only the one with the higher profit?

What is the best way to handle this event?

I am trying to do it this way:

The last open position will be closed as a result.

Please advise how to do it correctly, I need an example. I read reference books but I don't always understand details.

Code:

   ulong ticket=ULONG_MAX;
   double profit=DBL_MIN;
   if(invertStart == true)  // получил событие, о том что есть две позиции и одну можно закрыть.
     {
      int positions=PositionsTotal();
      for(int i=PositionsTotal()-1; i>=0; i--)
        {
         ulong tmp_ticket=PositionGetTicket(i);
         if(ticket!=0)
           {
            double tmp_profit=PositionGetDouble(POSITION_PROFIT);
            if(tmp_profit>profit)
              {
               ticket=tmp_ticket;
               profit=tmp_profit;
              }
           }
        }
      //---
      if(ticket!=ULONG_MAX)
         m_trade.PositionClose(ticket);
     }
 
Vladimir Karputov:

Code:

Thank you very much, seems to work. Only complains about ticket, as there is already such a local variable.

The declaration of 'ticket' hides local variable Lim_Stop_03_Sell_pos_06.mq5 299 10

Some of these tickets might need to be hidden inside.

 
Vladpedro:

Thank you very much, it seems to be working. Only it complains about ticket, because there is already such a local variable.

The declaration of 'ticket' hides local variable Lim_Stop_03_Sell_pos_06.mq5 299 10

Some of these tickets might need to be hidden inside.

Rename the variable

ulong ticket=ULONG_MAX;

в

ulong close_ticket=ULONG_MAX;

and of course do not forget to rename it in your code.

 
Vladimir Karputov:

Rename the variable

в

and of course don't forget to rename it in the code as well.

Yes thanks, I've already sorted it out... renamed it.

 

Good day!

Can you please tell me where I can find an EA that sends an email or push letter if the price is above the top line, or below the bottom line (the line is horizontal or slanted and can be moved on the chart)

Maybe someone has one, please share.

Only on MT5

Thank you!

 

if I connect the indicator in EA

int OnInit()
{
   ind_handle = iCustom(_Symbol, indperiod, "MyInd", indParam);
   return(INIT_SUCCEEDED);
}

and then I get indicator buffers using CopyBuffer(ind_handle, 0, 1, 3, buffer1)


do i have to read every tick of indicator buffers?

I need an answer, and preferably the source - docs, articles:

1. the indicator always calculates itself in parallel

2. the indicator is calculated only at the moment of call of CopyBuffer()

 
Igor Makanu:

if I connect the indicator in EA

and then I get indicator buffers using CopyBuffer(ind_handle, 0, 1, 3, buffer1)


do i have to read every tick of indicator buffers?

I need an answer, and preferably the source - docs, articles:

1. the indicator always calculates itself in parallel

2. the indicator is calculated only at the moment of call of CopyBuffer()

Igor, if the indicator is written correctly, only the values of the current bar can be changed. Therefore, if you need current values, you should use the CopyBuffer right before using these fresh values. And if we need history values, they don't change, and there's no reason to re-read them.

There is information somewhere about the calculation of the indicator on every tick. As far as I remember, by default the indicator is recalculated only when it is accessed, but it is possible to make it recalculate on every tick. How to do it, you should look in the documentation.

 
Alexey Viktorov:
Igor, if the indicator is written correctly, only the values of the current bar can be changed. Therefore, if you need current values, you should use the CopyBuffer right before using these fresh values. And if we need history values, they don't change, and there's no reason to re-read them.

There is some information somewhere about the calculation of the indicator on every tick. As far as I remember, by default the indicator is recalculated only when it is called, but it is possible to make it recalculated at every tick. How to do this is in the documentation.

OK, so I will have to test it anyway

the problem is in the indicator redrawing, I do not want to rewrite the indicator yet