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

 
Andrey Sokolov #:

These are not good times.

You mean write for you?

Don't tear yourself up Alexander

 

lost in the elementary stuff:

how do i organise a trawl pose - co-directed, for some reason it trawls one pose, i.e. it doesn't trawl... MT5

for (i=0; i<PositionsTotal(); i++)   
     if (a_position.Select(_Symbol))       
     if (PositionSelect(_Symbol)) if(Symbol()==PositionGetSymbol(i))  
     if (Magic==PositionGetInteger(POSITION_MAGIC))  
   //  magic = myposition.Magic();   
      {
         //  ------------   перевод в бу  BUY   ------------    
         //if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) 
         if (a_position.PositionType() == POSITION_TYPE_BUY)
          if (NLb_fun > 0)
            {
                Print(" перевод в безубыток BUY, _SL = ", _SL, " POSITION_PRICE_OPEN: ",
                     NormalizeDouble(PositionGetDouble(POSITION_PRICE_OPEN),_Digits),
                     " NLb_fun = ", NormalizeDouble(NLb_fun,_Digits));
               Modify = true;
 if(Modify)
              {
               Print(" серия рыночных позиций BUY назначена к переводу в безубыток, текущией позы POSITION_PRICE_OPEN = ",
               NormalizeDouble(PositionGetDouble(POSITION_PRICE_OPEN),_Digits)," тикет = ",PositionGetInteger(POSITION_TICKET));
                
               trade.PositionModify(_Symbol,SymbolInfoDouble(_Symbol, SYMBOL_BID) - 50*_Point,PositionGetDouble(POSITION_TP));
               Print(" Перенос в бу стоп лосса позиции Buy № ",  PositionGetInteger(POSITION_TICKET));
    ...
           } // к if(Modify)
        }    // к if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) 

...

i can use a simple code section as enumeration of positions for a trawl with HEDGE market positions - thank you.

the issue is solved! Congrats once again to Vladimir Karputov!!!

with his trawl!

https://www.mql5.com/ru/code/17263

the key trick is to look at the index in the loop and modify it with the ticket option!!!

//--- при таком методе мы будет сюда попадать на каждом тике.
   for(int i=PositionsTotal()-1;i>=0;i--)
      if(m_position.SelectByIndex(i))
         if(m_position.Symbol()==Symbol() && m_position.Magic()==m_magic)
           {
            //--- TrailingStop -> подтягивание StopLoss у ПРИБЫЛЬНОЙ позиции
            if(m_position.PositionType()==POSITION_TYPE_BUY)
              {
               //--- когда у позиции ещё нет StopLoss
               if(m_position.StopLoss()==0)
                 {
                  //--- пока StopLoss равен 0.0, TrailingStep не учитываем
                  if(m_symbol.Bid()-ExtTrailingStop>m_position.PriceOpen())
                    {
                     //--- модификация позиции
                     m_trade.PositionModify(m_position.Ticket(),m_position.PriceOpen(),0.0);
                    }
                 } 
TrailingStop
TrailingStop
  • www.mql5.com
Пример советника с реализацией Trailing Stop.
 

A simple task: you need to bump all elements from ArrayJ which are the same index and value as elements of ArrayI:

int OnInit()
  {
   int CommonArray[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 13, 13, 13, 13, 13, 13, 13};
   int ArrayI[20]=   {0, 0, 0, 0, 0, 0, 0, 8, 9, 10,  0, 12, 13, 13, 13, 13,  0,  0, 13,  0};
   int ArrayJ[];

   for(int j=0; j<ArraySize(ArrayI); j++)
   {
      ArrayResize(ArrayJ,j+1);
      ArrayJ[j]=CommonArray[j];
   }

   ArrayPrint(ArrayI);
   ArrayPrint(ArrayJ);

   for(int i=0; i<ArraySize(ArrayI); i++)
      if(ArrayI[i])
         for(int j=0; j<ArraySize(ArrayJ); j++)
            if(ArrayI[i]==ArrayJ[j])
               ArrayRemove(ArrayJ,j,1);

   ArrayPrint(ArrayJ);
//---
   return(INIT_SUCCEEDED);
  }

The key string is highlighted. Result:

2022.02.26 13:56:48.489 test (NZDUSD,H4)         0  0  0  0  0  0  0  8  9 10  0 12 13 13 13 13  0  0 13  0
2022.02.26 13:56:48.489 test (NZDUSD,H4)         1  2  3  4  5  6  7  8  9 10 11 12 13 13 13 13 13 13 13 13
2022.02.26 13:56:48.489 test (NZDUSD,H4)         1  2  3  4  5  6  7 11

Expected:

2022.02.26 13:56:48.489 test (NZDUSD,H4)         0  0  0  0  0  0  0  8  9 10  0 12 13 13 13 13  0  0 13  0
2022.02.26 13:56:48.489 test (NZDUSD,H4)         1  2  3  4  5  6  7  8  9 10 11 12 13 13 13 13 13 13 13 13
2022.02.26 13:56:48.489 test (NZDUSD,H4)         1  2  3  4  5  6  7          11                13 13    13

ArrayJ is dynamic, there seems to be something wrong with it... But I don't need a static one either.

Broke my mind. WHAT'S UP? Or is this the equivalent of the old song:

https://www.mql5.com/ru/forum/1111/page3141#comment_27152680

и

https://www.mql5.com/ru/forum/1111/page3142#comment_27371998

about named constants?

Ошибки, баги, вопросы
Ошибки, баги, вопросы
  • 2022.01.28
  • www.mql5.com
Общее обсуждение: Ошибки, баги, вопросы
 
x572intraday elements of ArrayI:

The key string is highlighted. Result:

Expected:

ArrayJ is dynamic, there seems to be something wrong with it... But I don't need a static one either.

1. ArrayResize would be better moved out of the loop.

ArrayResize(ArrayJ,ArraySize(ArrayI));
for(int j=0; j<ArraySize(ArrayI); j++)
   {
      ArrayJ[j]=CommonArray[j];
   }

2. ArrayRemove doesn't make an array element "empty", it "shifts" subsequent elements into its place. Hence the matching of elements in subsequent indexes is broken.

 
JRandomTrader #:

1. ArrayResize should be moved out of the loop

2. ArrayRemove does not make an array element "empty", but "shifts" subsequent elements in its place. Hence the matching of elements in subsequent indexes is broken.

There's no question about 2, I've just put the intervals there to make it clearer. Besides, in Help it says about a static array:"If the function is used for an array of a fixed size, the array size itself doesn't change: the remaining "tail" is physically copied to the startposition ." The example in the Help also uses a fixed size array, and I have a dynamic one.

Re 1. We can't put it outside a loop, since in a real task we don't know beforehand either the size of ArrayJ or ArrayI, much less the size of CommonArray, since they all don't coincide.

I also have an example where element dumping doesn't break halfway, as in this example:

int OBJTFVArray[]={1, 2, 3, 4, 5, 6, 10, 12, 1520, 30, 16385, 16386, 16387, 16388, 16390, 16392, 16396, 16408, 32769, 49153, 49154, 49155, 49156, 49157, 49158, 49159, 49160};
int PArray[20]={0, 0, 0, 0, 0, 00, 16390, 16392, 163960, 32769, 49153, 49153, 49153, 49153, 0, 0, 49153, 0};
int PArray_[];

int OnInit()
  {
   for(int p_=0; p_<ArraySize(PArray); p_++)
   {
      ArrayResize(PArray_,p_+1);
      PArray_[p_]=OBJTFVArray[p_+8];
   }

   ArrayPrint(PArray);
   ArrayPrint(PArray_);

   for(int p=0; p<ArraySize(PArray); p++)
      if(PArray[p])
         for(int p_=0; p_<ArraySize(PArray_); p_++)
            if(PArray[p]==PArray_[p_])
               ArrayRemove(PArray_,p_,1);

   ArrayPrint(PArray_);

   return(INIT_SUCCEEDED);
  }

The result is good:

 0     0     0     0     0     0     0 16390 16392 16396     0 32769 49153 49153 49153 49153     0     0 49153     0
15    20    30 16385 16386 16387 16388 16390 16392 16396 16408 32769 49153 49154 49155 49156 49157 49158 49159 49160
15    20    30 16385 16386 16387 16388                   16408             49154 49155 49156 49157 49158 49159 49160

But you need that variant to work. The hitch apparently happens if there are elements with the same value (see previous post) in the tail on the right - which is similar to the problem I was referring to above.

 
x572intraday #:

There are no questions about 2, I put the intervals for clarity. In addition, in Help it says about static array:"If the function is used for an array of fixed size, the size of the array itself does not change: it physically copies the remaining "tail" to the startposition ." The example in the Help also uses a fixed size array, and I have a dynamic one.

Regarding 1. We can't put it outside a loop, since in a real task we don't know beforehand either the size of ArrayJ or ArrayI, much less the size of CommonArray, since they all don't coincide.

I also have an example where element shuffling isn't interrupted halfway through, as in the above example:

The result is good:

But I need that variant to work. The hitch apparently happens if there are elements with the same value in the tail on the right (see previous post) - which is similar to the problem I referred to above.

1. You can and should take it out of loop, the way I've shown. Unless ArrayI size changes over the loop.

2. Then something like this

int k=0; // объявляем до цикла - чтобы использовать после
for(int i=0; i<ArraySize(ArrayI); i++) // после предыдущего кода (1) размеры ArrayI и ArrayJ равны
  if(ArrayI[i]!=ArrayJ[i])
    {
     ArrayJ[k++]=ArrayI[i];
    }
ArrayResize(ArrayJ,k);
 
And, in fact, it is possible to combine cycles 1 and 2 by copying only non-matching elements from ArrayI into ArrayJ.
 
x572intraday elements of ArrayI:

The key string is highlighted. Result:

Expected:

ArrayJ is dynamic, there seems to be something wrong with it... But I don't need a static one either.

Broke my mind. WHAT'S UP? Or is this the equivalent of the old song:

https://www.mql5.com/ru/forum/1111/page3141#comment_27152680

и

https://www.mql5.com/ru/forum/1111/page3142#comment_27371998

about named constants?

You have an extra loop.

Here is the correct variant:

void OnStart()
{
   int CommonArray[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 13, 13, 13, 13, 13, 13, 13};
   int ArrayI[20]=   {0, 0, 0, 0, 0, 0, 0, 8, 9, 10,  0, 12, 13, 13, 13, 13,  0,  0, 13,  0};
   int ArrayJ[];

   for(int j=0; j<ArraySize(ArrayI); j++)
   {
      ArrayResize(ArrayJ,j+1);
      ArrayJ[j]=CommonArray[j];
   }

   ArrayPrint(ArrayI);
   ArrayPrint(ArrayJ);

        for( int i = ArraySize( ArrayI )-1; i >= 0; i-- ){
                if( ArrayI[i] > 0 && ArrayI[i] == ArrayJ[i] ){
                        ArrayRemove( ArrayJ, i, 1 );
                }
        }

   ArrayPrint( ArrayJ );
//---
}
 
EVGENII SHELIPOV #:

Don't tear yourself up, Alexander.

Why Alexander? I am Andrei.

" Don't tear yourself up" - clarify.

 
Koldun Zloy #:

You have an extra cycle.

This is the right option:

About the extra loop... It may be inappropriate here but it is necessary in a large code, so it was moved here. I don't have resources to check the correctness of the algorithm at present, but formally it works as intended.

JRandomTrader, the result was surprising: all zeros. I will double-check later.

Thank you all.

JRandomTrader
JRandomTrader
  • 2018.10.28
  • www.mql5.com
Профиль трейдера