Problem with iHighest

 

Hello there!

I'm quite new to this language, but not new to programming at all. And I really like the language! :)


But now I got a problem...I hope you can help me!

high20 = High[iHighest(NULL,PERIOD_D1,MODE_HIGH,20,0)];

What I expect from this snippet: I returns me the highest value of the stock during the last 20 days, regardless of the way the chart is displayed.

But: It works perfectly, when the chart is set to D1. Whenever I switch the periodicity (e.g. to H4), the value of "high20" switches too, I'd say to some random number. Not to a value which would make sense to me.


Any help is appreciated! Thanks! :)

 

It was a good first effort. Just use iHigh instead of High. High is the high array for the current chart.

https://docs.mql4.com/predefined/variables/high

 

Thank you for your help! It looks like its what I was looking for. But how would you exactly write that?

If I take iHigh by itself, I guess I'm not able to look in multiple bars, just 1 at a time. Or is there a possibility to use iHigh together with iHighest?


Thanks!

 
GoingForGold:

Thank you for your help! It looks like its what I was looking for. But how would you exactly write that?

If I take iHigh by itself, I guess I'm not able to look in multiple bars, just 1 at a time. Or is there a possibility to use iHigh together with iHighest?

Oh come on, you are already using High with iHighest and you now know you need to use iHigh instead of High. Do you really need me to write it for you? Asking questions is not a substitute for thinking yourself. If you get really stuck that is a different matter.
 

Yeah, my problem is, High[] is a predifined variable. So I can insert in the [] brackets whatever i want, for example iHighest(). But iHigh() is a function. And obviously I can't use a function within a function. And so I have really no clue how to write that.

Or did you mean just use iHigh() in a loop and get the highest value? Well that would be easy, I just hoped that there was a prettier way to do so.

Sorry for annoying you, but I thought a forum exist to help people. If you don't want to help me, fine. But I guess you see, it takes more effort for me to write a question in the forum as just write the code. You see, if I would have known how to do, I would have done it.

 
GoingForGold:

But iHigh() is a function. And obviously I can't use a function within a function. And so I have really no clue how to write that.

Hah, and there we have your problem! Of course you can put a function in a function. And even if you couldn't you could always use an intermediate variable.
 

GoingForGold:

Sorry for annoying you, but I thought a forum exist to help people.

Absolutely, but it doesn't help you for me to think for you. Consider it as a homework exercise, with just enough help when you need it :-)
 
high20 = High[iHighest(NULL,PERIOD_D1,MODE_HIGH,20,0)];
iHighest returns the highest D1 bar. High[x] returns the high of the CHART bar. Oil and Water don't mix.
// Everything in D1
int iHigh20D1     = iHighest(NULL,PERIOD_D1,MODE_HIGH,20,0); // D1 chart index.
double High20days = iHigh(NULL,PERIOD_D1, iHigh20D1);        // Use index on D1 chart

// or convert to chart
datetime dayMinus20 = iTime(NULL,PERIOD_D1, 20);
int      iDayMinus20 = iBarShift(NULL,0, dayMinus20);        // Chart bar.
int      iHigh20days = iHighest(NULL,0, MODE_HIGH, iDayMinus20+1, 0);
double   High20days  = High[iHigh20days];
Reason: