CONVERSION from mq4 to C# : conversion issues

To add comments, please log in or register
LandauMT4
31
LandauMT4  

Hello everyone, i start this topic as i'm an absolute newbie in Metatrader and his language; i'm converting several EA from mq4 to C# and debugging the expert advisors i've been able to convert almonst everything but i have some doubts about the array Low[] and High[]: i took the definition of Low:

double Low[]

Series array that contains the lowest prices of each bar of the current chart.


this is totally nonsense to me: what means "the lowest prices of each bar"?  a bar has only one Low!!!

and how many elements are in that array? how they are ordered (if are ordered)??

thanks in advance for every clarification :) 

Keith Watford
Moderator
18938
Keith Watford  
LandauMT4:

Hello everyone, i start this topic as i'm an absolute newbie in Metatrader and his language; i'm converting several EA from mq4 to C# and debugging the expert advisors i've been able to convert almonst everything but i have some doubts about the array Low[] and High[]: i took the definition of Low:

double Low[]

Series array that contains the lowest prices of each bar of the current chart.


this is totally nonsense to me: what means "the lowest prices of each bar"?  a bar has only one Low!!!

and how many elements are in that array? how they are ordered (if are ordered)??

thanks in advance for every clarification :) 

Low[0] is the low of the current bar

Low[1] is the low of the last closed bar

Low[2] is the low of the bar before that

etc etc

William Roeder
19964
William Roeder  
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. LandauMT4: nd how many elements are in that array? how they are ordered (if are ordered)??
    Low[]/High[] is as series as Keith explained. There is no such concept in your DLL. There, P[0] is the oldest and P[Bars-1] the newest.

  3. LandauMT4: this is totally nonsense to me: what means "the lowest prices of each bar"?  a bar has only one Low!!!
    Of course, it's a typo.
LandauMT4
31
LandauMT4  
Keith Watford:

Low[0] is the low of the current bar

Low[1] is the low of the last closed bar

Low[2] is the low of the bar before that

etc etc

Hello Keith, many thanks: this is ok and make sense. so the order is from left (the running candle) to right (the most old candle). 

The documentation says that the size of the array Low[] is the number of candles in the graph: i suppose there is a limit anyways (imagine to manage the low[] array with decades of candles with 1 minute as timeframe): do you know what is that limit.

Many thanks again, cheers

LandauMT4
31
LandauMT4  
William Roeder:
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Low[]/High[] is as series as Keith explained. There is no such concept in your DLL. There, P[0] is the oldest and P[Bars-1] the newest.

  3. Of course, it's a typo.


1- my fault, sorry

2- my DLL? what you mean?

3- ok

Bye and thanks

LandauMT4
31
LandauMT4  

i have another issue with this line:

Low[Lowest(NULL,0,MODE_LOW,ExtDepth,shift)]


here the return value of the Lowest function is used to indexing the Low array: i can't find any documentation about that function, i 've just found ILowest: is the same function? many thanks again

LandauMT4
31
LandauMT4  

Hello, can someone confirm me that the Lowest function returns the index of bar where there is the lowest LOW in the range of bars specified???


this is how i converted the Lowest function:

int Lowest(int count, int startindex, List<Candle> bars) 

        {

            int lowposition = -1;

            decimal min = decimal.MaxValue;

            int i = 0, j = 0;

            for (i = startindex; j < count; i++, j++)

            {

                var tmpmin = bars[i].Low;

                if (tmpmin < min)

                {

                    min = tmpmin; 

                    lowposition = i;

                }

            }

            return lowposition;

}

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
LandauMT4
31
LandauMT4  

i really can't understand this line:


val=Low[Lowest(NULL,0,MODE_LOW,ExtDepth,shift)];


Lowest(..) return the index of the candle with the lowest Low in the range specified (inside the bars array)

but why is used to index the array Low?? the array low is not bars, so the indexing is inconsistet....

JC
1606
JC  
LandauMT4:

val=Low[Lowest(NULL,0,MODE_LOW,ExtDepth,shift)];

but why is used to index the array Low?? the array low is not bars, so the indexing is inconsistet....

Low[] is an array of bar low prices. Low[0] is the low price of the current bar, Low[1] is the low price of the previous bar, etc. Similarly, High[0] is the high of the current bar etc.

iLowest() scans the bar history for the lowest of a set of values, and returns the corresponding index. Your example call to iLowest does the following:

  • Starts at the bar with offset shift
  • Scans a total of ExtDepth bars
  • Looks for the one out of the ExtDepth bars with the lowest low value, and returns its zero-based index
Getting the value from the Low[] array using the index provided by iLowest() then gives you the actual low price.

For example, let's say that the lows of the last 10 bars are as follows (in MT4 order, with #0 meaning the current bar):

  • #0: 1.10767
  • #1: 1.10721
  • #2: 1.10714
  • #3: 1.10739
  • #4: 1.10693
  • #5: 1.10702
  • #6: 1.10730
  • #7: 1.10688
  • #8: 1.10709
  • #9: 1.10752

You then call iLowest with shift=2 and ExtDepth=5. MT4 will scan bars #2 through #6 inclusive. iLowest() will return 4, because 1.10693 is the lowest of those values. Doing Low[4] then gives you the actual low price of 1.10693.

Part of the confusion/complexity is that iLowest() doesn't necessarily find the lowest low. It's not scanning the Low[] array; it's scanning the bar history. If you call iLowest() with MODE_HIGH rather than MODE_LOW, for example, then it will return the index of the bar with the lowest high value rather than the lowest low value.

Similarly, having found the lowest low (or lowest high, or whatever), you may not necessarily want to know what the low value is. You might instead want to know the close price of the bar where the lowest low occurred.That would be Close[iLowest(..., MODE_LOW, ...)]

To put it another way: retrieval of a lowest-low price is a two-part process, involving use of iLowest() to get a bar index and then extracting that value from the Low[] array. It's a two-part process instead of a single call because getting the lowest low price is by far the most common use-case, but there are also scenarios such as wanting to know the close price of the bar where the lowest-low occurred.

Or another way: your C# implementation of Lowest() is less flexible than MQL4's iLowest(). Your implementation is like a version of MQL4's iLowest() which can only be called with MODE_LOW, not with MODE_HIGH or MODE_CLOSE etc.

Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
  • www.mql5.com
Calculations of technical indicators require price values and/or values of volumes, on which calculations will be performed. There are 7 predefined identifiers from the ENUM_APPLIED_PRICE enumeration, used to specify the desired price base for calculations. If a technical indicator uses for calculations price data, type of which is set by...
Marco vd Heijden
Moderator
12372
Marco vd Heijden  
//+------------------------------------------------------------------+
//|                                              Find Lowest Low.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   int lowest_index=iLowest(Symbol(),PERIOD_CURRENT,MODE_LOW,WHOLE_ARRAY,0);
   double lowest_low=Low[lowest_index];
   
   Print("The bar number with the lowest low: "+(string)lowest_index);
   Print("The Lowest Low: "+DoubleToStr(lowest_low));
  }
//+------------------------------------------------------------------+
LandauMT4
31
LandauMT4  

Hi, really many many many thanks for your detailed reply.

Maybe there is a misunderstanding: the lowest function operates on the bars array or on the Low[] array?


i explain better:

i have a list of candles called bars

every minute happens that:

1-i add a new candle to this list (because my timeframe is 1 minute)

2- i calculate the Low and High arrays like so:

Low = new decimal[bars.Count];

High = new decimal[bars.Count]; 

for (int i = 0; i < bars.Count; i++)

{

Low[i] = bars[i].Low;

High[i] = bars[i].High;

}

3- i calculate val = Low[Lowest(_ExtDepth, shift,bars)];

as you can see i pass the bars to my Lowest function, and bars is a list of candles


you wrote:

For example, let's say that the lows of the last 10 bars are as follows (in MT4 order, with #0 meaning the current bar)


so it seems that Lowest must operate on the Low[] (wich is an array of prices) and not bars(wich is a list of candles): indeed is a nonsense index the Low array using an index extracted from bars

is this true?

Cheers.

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
12
To add comments, please log in or register