Getting Highest High and Lowest Low within the last 24 hours
Getting Highest High and Lowest Low within the last 24 hours
Loading saved threads...
Hammons2012 · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Hammons2012
Original post: https://stackoverflow.com/questions/77068941
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I'm trying to build a function in MQL4 for an Expert Advisor to get the highest high and the lowest low within a specific time frame, but the function I'm creating isn't working. I'm getting data returned, however it's not reporting the true highest high and the lowest low within the time frame. Here is the code:
void FindHighLowWithinTimeFrame(int FunctStartHour, int FunctEndHour, double& FunctHighestHigh, double& FunctLowestLow)
{
int period = Period(); // Get the current chart's period in minutes
int MinutesInDay = 24 * 60; // Total minutes in a day
int BarsInDay = MinutesInDay / period; // Number of bars in a trading day for the current chart's time frame
// int BarsInDay = 24 * 60 / period; // Number of bars in a trading day
datetime currentTime = Time[0];
datetime startTime = Time[BarsInDay]; // Start time for the past 24 hours
datetime endTime = Time[0]; // End time (current time)
// Convert user-defined start and end hours to a time within the last 24 hours
datetime startWithin24Hours = Time[BarsInDay] + FunctStartHour * 3600;
datetime endWithin24Hours = Time[BarsInDay] + FunctEndHour * 3600;
int StartBar = iBarShift(Symbol(), period, startTime);
int EndBar = iBarShift(Symbol(), period, endTime);
// Ensure StartBar and EndBar are within valid range
if (StartBar < 0) StartBar = 0;
if (EndBar >= Bars) EndBar = Bars - 1;
double TempHighestHigh = High[StartBar]; // Initialize with the high of the first bar
double TempLowestLow = Low[StartBar]; // Initialize with the low of the first bar
for (int i = StartBar + 1; i <= EndBar; i++)
{
if (Time[i] >= startWithin24Hours && Time[i] <= endWithin24Hours)
{
if (High[i] > TempHighestHigh)
TempHighestHigh = High[i];
if (Low[i] < TempLowestLow)
TempLowestLow = Low[i];
}
}
FunctHighestHigh = TempHighestHigh; // Update the output parameters with the highest high and lowest low
FunctLowestLow = TempLowestLow;
}
The idea is I want to get the highest high and the lowest low from each Forex session frame within the last 24 hours.
I've tried using the iHighest and iLowest functions, but I couldn't get those to work either. I can't tell from which bar the highest high and lowest low are coming from, and I've tried to add print/comment statements to identify the bar, but I have been unsuccessful.
Quote
Report
Mark SdS · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Mark SdS
Original post: https://stackoverflow.com/a/77069945
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I recognize this.
Probably the error is in the assumption that a trading day has 24*60 M1 bars.
Most markets are closed during one or two hours (don't ask me why) which makes such linear synchronized calculation between minutes and bars tricky.
Eliminate the time component and continue just in bars. Might look overwhelming, but it is doable.
Quote
Report
ranxero · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: ranxero
Original post: https://stackoverflow.com/a/79436197
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
just stumbled across this :) probably already solved. Still here some hints:
first get the broad start date of the current day:
datetime today = iTime(Symbol(), PERIOD_D1, 0);
// convert to bar index in current timeframe
int shift = iBarShift(Symbol(), Period(), today);
// convert to time in current timeframe
datetime currentPeriodTime = Time[shift];
int scurityCounter = 0; // to prevent endless loop
// while calculated time is smaller than today subtract 1
while (TimeDayOfYear(currentPeriodTime)<DayOfYear()) {
shift--;
currentPeriodTime = Time[shift];
scurityCounter++;
if (scurityCounter>121) {
break;
}
}
// shift should now be the first bar of the current day in the given timeframe, no matter at what time your broker opens
// to get the highest high you then can use
int highestIndex = iHighest(Symbol(), Period(), MODE_HIGH, shift, 1);
double high = High[highestIndex];
// for lowest
int lowestIndex = iLowest(Symbol(), Period(), MODE_LOW, shift, 1);
double low = Low[lowestIndex];
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: Mark SdS Source score (net votes, not local likes): 1 Original post: https://stackoverflow.com/a/77069945 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I recognize this. Probably the error is in the assumption that a trading day has 24*60 M1 bars. Most markets are closed during one or two hours (don't ask me why) which makes such linear synchronized calculation between minutes and bars tricky. Eliminate the time component and continue just in bars. Might look overwhelming, but it is doable.
Checking account access…