Please help! I am super confused. How come low is higher than high

 
mrateM[iHighest(_Symbol,PERIOD_D1,MODE_HIGH,barNumM,0)].high-mrateM[iLowest(_Symbol,PERIOD_D1,MODE_LOW,barNumM,0)].low

How come sometimes this can return negative values? Within the same range shouldn't the highest high>=lowest low?

 
Tsz Yau Lo:

How come sometimes this can return negative values? Within the same range shouldn't the highest high>=lowest low?

How is mrateM filled? Assuming its a CopyRates result, you get different index values in both sides of the subtraction operation.
iHighest returns the highest index, iLowest returns the lowest index - they are different unless a very specific case where all candles have same high/low.

Need to see the mRateM filling, depending on what you want to achieve, you need same index in both sides, probably should write something like:

mrateM[iHighest(_Symbol,PERIOD_D1,MODE_HIGH,barNumM,0)].high-mrateM[iHighest(_Symbol,PERIOD_D1,MODE_HIGH,barNumM,0)].low

and 

mrateM[iLowest(_Symbol,PERIOD_D1,MODE_LOW,barNumM,0)].high-mrateM[iLowest(_Symbol,PERIOD_D1,MODE_LOW,barNumM,0)].low
 
Amir Yacoby #:

How is mrateM filled? Assuming its a CopyRates result, you get different index values in both sides of the subtraction operation.
iHighest returns the highest index, iLowest returns the lowest index - they are different unless a very specific case where all candles have same high/low.

Need to see the mRateM filling, depending on what you want to achieve, you need same index in both sides, probably should write something like:

and 

Dear mate firstly thank you for your reply.

MqlRates mrateM[];

Firstly, what I want to achieve here is to subtract the lowest low from the highest high within the same range of bars. How should I code that correctly, please kindly advise.

 
Amir Yacoby #:

How is mrateM filled? Assuming its a CopyRates result, you get different index values in both sides of the subtraction operation.
iHighest returns the highest index, iLowest returns the lowest index - they are different unless a very specific case where all candles have same high/low.

Need to see the mRateM filling, depending on what you want to achieve, you need same index in both sides, probably should write something like:

and 

To be more specific, the lowest low and highest high in most circumstances wont happen within the same bar.

Example:

30 bars total

Highest high occurred in the 3rd bar 1.1

Lowest low occurred in the 29th bar 1.05


What I want to get is simply 1.1-1.05=0.05

 

And the CopyRates?

You need to make sure you use CopyRates for mRareM,_Symbol,PERIOD_D1 on the same bar you calculate:

mrateM[iHighest(_Symbol,PERIOD_D1,MODE_HIGH,barNumM,0)].high-mrateM[iLowest(_Symbol,PERIOD_D1,MODE_LOW,barNumM,0)].low

Otherwise you get offset 

 

Yes I copied rates already.


Did these codes locate highest high and lowest low however?

mrateM[iHighest(_Symbol,PERIOD_D1,MODE_HIGH,barNumM,0)].high-mrateM[iLowest(_Symbol,PERIOD_D1,MODE_LOW,barNumM,0)].low
 
Tsz Yau Lo #:

Yes I copied rates already.


Did these codes locate highest high and lowest low however?

Yes they are - but they work on streaming price from chart which changes each new bar, and not on the copied array mrateM.

This is why you need to make sure you copy the rates at the exact same bar you use that code.
If you wait for a new bar between the CopyRates and the calculation, you will have different index offsets between what you copied to the mrateM array and the iHighest/iLowest which work on streaming chart price.

 
MqlRates mrateM[];
   
if(CopyRates(_Symbol,PERIOD_D1,0,barNumM,mrateM)<0)
    {
    Alert("Error copying D1 rates/history data - error:",GetLastError(),"!");
    return;
    }

SLS=mrateM[iHighest(_Symbol,PERIOD_D1,MODE_HIGH,barNumM,0)].high-mrateM[iLowest(_Symbol,PERIOD_D1,MODE_LOW,barNumM,0)].low;

This is what I have been doing, I added codes to check the index number, and I recognized the index is correct... Thats why I am like ultra confused.

 
Tsz Yau Lo #:

This is what I have been doing, I added codes to check the index number, and I recognized the index is correct... Thats why I am like ultra confused.

can you supply the whole relevant code, including barNumM and SLS definitions and value settings?

 
input int      barNumM=42;             // Number of bars in mid term array
double   SLS=0;


MqlRates mrateS[];
MqlRates mrateM[];

if(CopyRates(_Symbol,PERIOD_H4,0,barNumS,mrateS)<0)
{
Alert("Error copying H4 rates/history data - error:",GetLastError(),"!");
return;
}
if(CopyRates(_Symbol,PERIOD_D1,0,barNumM,mrateM)<0)
{
Alert("Error copying D1 rates/history data - error:",GetLastError(),"!");
return;
}

SLS=mrateM[iHighest(_Symbol,PERIOD_D1,MODE_HIGH,barNumM,0)].high-mrateM[iLowest(_Symbol,PERIOD_D1,MODE_LOW,barNumM,0)].low;

This is the full code


 

When I tested it, some works but some don't.

Example: 2019.07.01 16:00:00   SLS:-0.005050000000000221


Reason: