ArrayMinimum and Maximum clarification

 

Hi all

I have been (for a couple days now.....) trying to make a trailing stop with iOpen(iLowest) on pairs that are other than the one on the chart and it raised a whole lot of problems, so I went back to basics to understand each of the tools I am trying to use.

I can not understand this:

   int test[]={3,2,1,10,9,8,7};
   printf("Test min at position "+ArrayMinimum(test,0,3)+" with value "+test[ArrayMinimum(test,0,4)]); //AMin(array,start,count)
   printf("Test min at position "+ArrayMaximum(test,0,4)+" with value "+test[ArrayMaximum(test,0,4)]); //AMin(array,start,count)

returns this:

Test min at position 2 with value 1

Test min at position 3 with value 10


OK, this meets my expected results. I am not crazy yet.

Now, a little more tricky:

The lowest open in the last 12 bars on this TF on this symbol:

      printf("Min Open in "+Symbol()
        +" position "+iLowest(Symbol(),PERIOD_CURRENT,MODE_OPEN,12,0)
        +" with value"+iOpen(Symbol(),PERIOD_CURRENT,iLowest(Symbol(),PERIOD_CURRENT,MODE_OPEN,12,0)));

The result is also as expected:

Min Open in AUDJPY position 10 with value 81.093

Now on another a set of other pairs:

   printf("Opens while on "+Symbol()+" chart:");
   for(int i=0; i<2; i++){
      printf("Min Open in "+SymbolList[i]
        +" position "+iLowest(SymbolList[i],PERIOD_CURRENT,MODE_OPEN,12,0)
        +" with value "+iOpen(SymbolList[i],PERIOD_CURRENT,iLowest(SymbolList[i],PERIOD_CURRENT,MODE_OPEN,12,0)));
   }

Returns:

Opens while on NZDUSD chart:

Min Open in AUDUSD position 10 with value 0.71249

Min Open in EURAUD position 2 with value 1.57506

This is also as expected.

My intention is to, regardless what symbol and what TF I have open, the EA will still be true to the symbol and TF I want it to look at, in this case the 1 min.



Returned this:

Opens while on NZDUSD chart:

Min Open in AUDUSD position 32 with value 0.71439

Min Open in EURAUD position 48 with value 1.57632

   printf("Opens while on "+Symbol()+" chart:");
   for(int i=0; i<2; i++){
      printf("Min Open in "+SymbolList[i]
        +" position "+iLowest(SymbolList[i],PERIOD_M1,MODE_OPEN,60,0)
        +" with value "+iOpen(SymbolList[i],PERIOD_M1,iLowest(SymbolList[i],PERIOD_M1,MODE_OPEN,12,0)));
   }

Positions are correct, values are not. I know I am mixing apples and Oranges here. Now.... I got to correct this.

When I get the position independently, it returns the correct one.

When I get it nested inside the iOpen, it gets it wrong.

I have seen people using the NULL, and I got lucky once and it worked but I didn't fully understand it. Lets explore it now.

   printf("Opens while on "+Symbol()+" chart:");
   for(int i=0; i<2; i++){
      printf("Min Open in "+SymbolList[i]
        +" position "+iLowest(SymbolList[i],PERIOD_M1,MODE_OPEN,60,0)
        +" with value "+iOpen(SymbolList[i],PERIOD_M1,iLowest(NULL,NULL,MODE_OPEN,12,0)));
   }

Returns:

Opens while on NZDUSD chart:

Min Open in AUDUSD position 40 with value 0.71429

Min Open in EURAUD position 56 with value 1.57685


The positions are OK, the values are not. They return the start of the 3rd or 4th last candle, not the 40th and 56th.

Symbol is correct, indexing is not on the nexted iLowest.

Can someone point my error please? I did read both the apples and oranges, and I still stuck.


 

I might have found the problem but can anyone confirm this was the issue?

   printf("Opens while on "+Symbol()+" chart:");
   for(int i=0; i<2; i++){
      int local_index=iLowest(SymbolList[i],PERIOD_M1,MODE_OPEN,60,0);
      printf("Min Open in "+SymbolList[i]
        +" position "+iLowest(SymbolList[i],PERIOD_M1,MODE_OPEN,60,0)
        +" with value "+iOpen(SymbolList[i],PERIOD_M1,local_index));
   }


Instead of nesting iLowest, I calculate it beforehand and pass the variable in.

Returns:

Opens while on NZDUSD chart:

Min Open in AUDUSD position 1 with value 0.71391

Min Open in EURAUD position 6 with value 1.57609


 
  printf("Test min at position "+ArrayMinimum(test,0,3)+" with value "+test[ArrayMinimum(test,0,4)]); //AMin(array,start,count)




There seems to be an error in this lien of code. 


instead of 

ArrayMinimum(test,0,3)

you need to write probably,

ArrayMinimum(test,0,
4 )


let me know if that helps!

 
Satyam Shivam #:




There seems to be an error in this lien of code. 


instead of 

you need to write probably,


let me know if that helps!

Yes I was using the 4, just the code was copy/pasted before I fix that. The key issue is on the bottom. I think I got it resolved but need confirmation since I am fairly new.

 

On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 (2019)

On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
          Error 4806 while using CopyBuffer() - Expert Advisors and Automated Trading - MQL5 programming forum #10 (2020.12.15)
          Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum (2019)
          Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
          Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum #2 (2018)
          SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum (2019)

 
William Roeder #:

On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 (2019)

On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
          Error 4806 while using CopyBuffer() - Expert Advisors and Automated Trading - MQL5 programming forum #10 (2020.12.15)
          Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum (2019)
          Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
          Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum #2 (2018)
          SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum (2019)

You said:  On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.

Can I tackle that with OnInit pull of the TFs I will later use?

Maybe like this:

   for(int i=0; i<2; i++){
      spam=iMA(SymbolList[i],PERIOD_M1,10,0,MODE_EMA,PRICE_TYPICAL);
   }

My idea is just to have the data pulled, not to ever use that handle, that is why I dont mind overwriting it.

 
   double spamList[];
   for(int i=0; i<2; i++){
      spamList[i]=iMA(SymbolList[i],PERIOD_M1,10,0,MODE_EMA,PRICE_TYPICAL);
   }

maybe?

Reason: