我正在尝试在 MQL4 中为 EA 交易构建一个函数,以在特定时间范围内获得最高点和最低点,但我正在创建的函数无法正常工作。我正在获取返回的数据,但它没有报告时间范围内的真实最高高点和最低低点。这是代码:
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;
}
我的想法是我想获得过去 24 小时内每个外汇交易时段的最高点和最低点。
我尝试过使用 iHighest 和 iLowest 函数,但我也无法让它们工作。我无法判断最高点和最低点来自哪个柱,并且我尝试添加打印/注释语句来识别该柱,但我没有成功。
我认识这一点。
错误可能在于假设一个交易日有 24*60 M1 柱。 大多数市场在一两个小时内休市(不要问我为什么),这使得分钟和柱之间的线性同步计算变得棘手。
消除时间部分并仅在柱中继续。可能看起来势不可挡,但这是可行的。