Download MetaTrader 5

Finding out time of price stored in array

To add comments, please log in or register
Do you want to write a technical indicator? Find solution in MQL5 Reference!
Daniel Nichols
13
Daniel Nichols 2014.02.03 15:55 

Hello,

I'm having some issues figuring out how to get his bit of code to work. It's supposed to search the past 200 bars from the current bar, look for whether or not one of two indicators have a value, then record the high or low of the bar that the indicator has a value on (depending on if the indicator is the "high pivot" indicator, which searches out highest highs within certain bar ranges, or the "low pivot" indicator which does the same for lows) and the time of the bar in question so that they can be called on later.

After it stores the information and the loop is finished, it's supposed to pull out the highest high within the Array, the time of the bar that the High occured in, and then print them to the journal so that I know the code is working and can move on to the next step.

What it's doing instead is saving the high as it's supposed to, but not the time, instead when I ask it to give me the highest high from the array and the time associated with it, it gives me the highest high, then a (seemingly) random time that increments up by the open of every bar that gets added to the chart.

I'm assuming I have a logic error somewhere in there by thinking it's giving me the time of the bar and the price of the bar that LPValue1 or HPValue1 shows via the loop, but I can't seem to figure it out.

double HighPivotsPrice[200];

double LowPivotsPrice[200];

datetime HighPivotsTime[200];

datetime LowPivotsTime[200];

void CheckTradeOpportunity() {

double LP = iCustom(Symbol(),0,"Low Pivot",Equals,LeftStrength,RightStrength,0,0);

double HP = iCustom(Symbol(),0,"High Pivot",Equals,LeftStrength,RightStrength,0,0);

double LPPrice1, HPPrice1;

datetime LPTime1, HPTime1;

int HPIdx = 0, LPIdx = 0;

for(int i=200; i>=0; i--){

double LPValue = iCustom(Symbol(),0,"Low Pivot",Equals,LeftStrength,RightStrength,0,i);

double HPValue = iCustom(Symbol(),0,"High Pivot",Equals,LeftStrength,RightStrength,0,i);

if(LPValue > 0/* && LPValue < LPValue2*/){

LPTime1 = iTime(Symbol(),0,i);

LPPrice1 = Low[i];

LowPivotsPrice[LPIdx] = LPPrice1;

LowPivotsTime[LPIdx] = LPTime1;

LPIdx++;

}

if(HPValue > 0/* && HPValue > HPValue2*/){

HPTime1 = iTime(Symbol(),0,i);

HPPrice1 = High[i];

HighPivotsPrice[HPIdx] = HPPrice1;

HighPivotsTime[HPIdx] = HPTime1;

HPIdx++;

}

}

int highmax = ArrayMaximum(HighPivotsPrice);

int highmaxbar = ArrayBsearch(HighPivotsPrice,highmax);

datetime timeofhighmax = HighPivotsTime[highmaxbar];

int TimeOfMaxTime = iBarShift(Symbol(),0,timeofhighmax);

string TimeInMinutes = TimeToStr(timeofhighmax,TIME_MINUTES);

string TimeInDays = TimeToStr(timeofhighmax,TIME_DATE);

Print("Max high: ",HighPivotsPrice[highmax],"Time: ", TimeInDays," ",TimeInMinutes," Bar: ",TimeOfMaxTime);

}

If anyone could point out to me where the mistake lies, I'd be grateful.

Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2014.02.03 16:31  
Bazgir:

Hello,

What it's doing instead is saving the high as it's supposed to, but not the time, instead when I ask it to give me the highest high from the array and the time associated with it, it gives me the highest high, then a (seemingly) random time that increments up by the open of every bar that gets added to the chart.

I'm assuming I have a logic error somewhere in there by thinking it's giving me the time of the bar and the price of the bar that LPValue1 or HPValue1 shows via the loop, but I can't seem to figure it out.

If anyone could point out to me where the mistake lies, I'd be grateful.

After a quick look I think it may be down to ArrayBsearch() there was a thread similar and my suspicion was that it was a double compare issue . . . https://www.mql5.com/en/forum/147251
Daniel Nichols
13
Daniel Nichols 2014.02.03 16:49  
RaptorUK:
After a quick look I think it may be down to ArrayBsearch() there was a thread similar and my suspicion was that it was a double compare issue . . . https://www.mql5.com/en/forum/147251


I'm not sure I understand how it could be a double compare issue? In essence, they are two different arrays that increment up by one every time the same condition is met, that is if LPValue1 > 0 or HPValue1 > 0(so, on the chart, "Low Pivot" would have a value, or "High Pivot" would have a value), and record the time and the High/Low (depending on if it's the Low Pivot or High Pivot) of the bar it occured on. I assume that the placement in the Array would be the same for both, as neither would increment up/change at all unless another Low or High Pivot is found, in which case both will increase together.

This lead me to believe that if I found the Maximum value in the HighPivotsPrice, and then requested the placement in the Array it falls in (with the ArrayBsearch command), and then used that placement in the HighPivotsTime array, it would give me the time that that value occured on, allowing me to find out the bar in question and then print it to the journal.

If the issue lies with my use of the ArrayBsearch command, is there a workaround that will achieve the same result? I'm struggling to find something that allows me to accurately record both the time and the price of the high or low when the Low Pivot indicator has a value or the High Pivot indicator has a value.

Thanks

whroeder1
14519
whroeder1 2014.02.03 16:57  
int highmax = ArrayMaximum(HighPivotsPrice);
int highmaxbar = ArrayBsearch(HighPivotsPrice,highmax);
int TimeOfMaxTime = iBarShift(Symbol(),0,timeofhighmax);
  1. Have you created all 200 entries in LowPivotsPrice[200]? I doubt it since your loop is for(int i=200; i>=0; i--) Then the empty values are zero and that is what you always find with ArrayMinimum. Tell them the number of values you have in the array
  2. You can NOT use Bsearch unless you sort the array first and if you do you've lost the connection with the time array. There's no need to.
      HPIdx++; // Number of entries in HighPivotsPrice (nHPP)
    } }
    
    int highmax = ArrayMaximum(HighPivotsPrice, HPIdx);
    
    double   HH            = HighPivotsPrice[highmax];
    datetime timeofhighmax = HighPivotsTime[highmax];
  3. iBarShift doesn't return a time, it returns a shift.
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2014.02.03 16:59  
Bazgir:


I'm not sure I understand how it could be a double compare issue? In essence, they are two different arrays that increment up by one every time the same condition is met, that is if LPValue1 > 0 or HPValue1 > 0(so, on the chart, "Low Pivot" would have a value, or "High Pivot" would have a value), and record the time and the High/Low (depending on if it's the Low Pivot or High Pivot) of the bar it occured on. I assume that the placement in the Array would be the same for both, as neither would increment up/change at all unless another Low or High Pivot is found, in which case both will increase together.

This lead me to believe that if I found the Maximum value in the HighPivotsPrice, and then requested the placement in the Array it falls in (with the ArrayBsearch command), and then used that placement in the HighPivotsTime array, it would give me the time that that value occured on, allowing me to find out the bar in question and then print it to the journal.

If the issue lies with my use of the ArrayBsearch command, is there a workaround that will achieve the same result? I'm struggling to find something that allows me to accurately record both the time and the price of the high or low when the Low Pivot indicator has a value or the High Pivot indicator has a value.

Thanks

You are using two different functions to find the highest and the time of the highest. They work in different ways . . . did you read the documentation for ArrayBsearch() ? it wouldn't make sense to sort your array otherwise you will lose what you are looking for . . . but "Note: Binary search processes only sorted arrays. To sort numeric arrays use the ArraySort() function."
Daniel Nichols
13
Daniel Nichols 2014.02.03 17:04  
WHRoeder:
  1. Have you created all 200 entries in LowPivotsPrice[200]? I doubt it since your loop is for(int i=200; i>=0; i--) Then the empty values are zero and that is what you always find with ArrayMinimum. Tell them the number of values you have in the array
  2. You can NOT use Bsearch unless you sort the array first and if you do you've lost the connection with the time array. There's also no need to
  3. iBarShift doesn't return a time, it returns a shift.


I haven't yet worked on the LowPivot portion, just trying to bugfix what happens with the HighPivots, so that's another issue I'll need to address once I'm finished with what I'm working on, but thank you for picking it up for me. As for iBarShift not returning a time, I am aware; the int TimeOfMaxTime isn't the best label and should probably be changed to "BarOfMaxHigh" or something similar, but I just used it to get the bar number of the bar where the highest value of HighPivotsPrice occurred on the chart.

RaptorUK:
You are using two different functions to find the highest and the time of the highest. They work in different ways . . . did you read the documentation for ArrayBsearch() ? it wouldn't make sense to sort your array otherwise you will lose what you are looking for . . . but "Note: Binary search processes only sorted arrays. To sort numeric arrays use the ArraySort() function."


I figured I could circumvent the issue by not sorting them, and have it return the index associated with the value (the maximum value, in the case of HighPivotsPrice) I gave it to work with. I'll have to find another workaround to get the time the price occurred and was logged in the array.

Thanks

To add comments, please log in or register