Advanced Day of Week Entry Time by Naya
255 downloads / 97 views / Created: 08.06.2025




Average Rating: 0
Indicator Description
Advanced Day of Week Entry Time is an advanced filter used to control entry signals based on specific day and time conditions. It supports multiple datetime filtering modes, allowing precise customization of when entries are allowed.
Available Logic:
Enter the market between the specified days and times
Parameters:
1. DateTime Mode:
* Daily time filter (applies a time range filter within each selected day)
* Absolute datetime match (checks for an exact day and time range)
* Everyday ignores time filtering (entire week is allowed without time restriction)
* Everyday uses time filtering (entire week is filtered by time range only)
* Weekly session (applies time filter only on the first and last day of the period)
2. Day Range:
* From day (inclusive): Selectable from Sunday to Saturday or "Everyday"
* To day (exclusive): Selectable from Sunday to Saturday or "Everyday"
3. Time Range:
* From hour and minute (inclusive)
* To hour and minute (exclusive)
Logic Behavior based on DateTime Mode:
* Daily time filter: Allows entry if the current bar’s time falls within the defined time range on allowed days.
* Absolute datetime match: Allows entry only when both day and time match exactly, handling day overlaps accurately.
* Everyday ignores time filtering: Allows entry every day regardless of time.
* Everyday uses time filtering: Allows entry every day but only within the defined time range.
* Weekly session: Allows entry from the defined time on the first day up to the end time on the last day of the selected range.
Use Case:
Ideal for strategies requiring precise control over entry windows based on trading sessions, news cycles, or institutional market activity hours.
Available Logic:
Enter the market between the specified days and times
Parameters:
1. DateTime Mode:
* Daily time filter (applies a time range filter within each selected day)
* Absolute datetime match (checks for an exact day and time range)
* Everyday ignores time filtering (entire week is allowed without time restriction)
* Everyday uses time filtering (entire week is filtered by time range only)
* Weekly session (applies time filter only on the first and last day of the period)
2. Day Range:
* From day (inclusive): Selectable from Sunday to Saturday or "Everyday"
* To day (exclusive): Selectable from Sunday to Saturday or "Everyday"
3. Time Range:
* From hour and minute (inclusive)
* To hour and minute (exclusive)
Logic Behavior based on DateTime Mode:
* Daily time filter: Allows entry if the current bar’s time falls within the defined time range on allowed days.
* Absolute datetime match: Allows entry only when both day and time match exactly, handling day overlaps accurately.
* Everyday ignores time filtering: Allows entry every day regardless of time.
* Everyday uses time filtering: Allows entry every day but only within the defined time range.
* Weekly session: Allows entry from the defined time on the first day up to the end time on the last day of the selected range.
Use Case:
Ideal for strategies requiring precise control over entry windows based on trading sessions, news cycles, or institutional market activity hours.
Comments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
//==============================================================
// Forex Strategy Builder
// Copyright � Miroslav Popov. All rights reserved.
//==============================================================
// THIS CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE.
//==============================================================
using System;
using ForexStrategyBuilder.Infrastructure.Entities;
using ForexStrategyBuilder.Infrastructure.Enums;
using ForexStrategyBuilder.Infrastructure.Interfaces;
namespace ForexStrategyBuilder.Indicators.Store
{
public class AdvancedDayOfWeekEntryTime : Indicator
{
public AdvancedDayOfWeekEntryTime()
{
IndicatorName = "Advanced Day of Week Entry Time";
PossibleSlots = SlotTypes.OpenFilter;
IsDeafultGroupAll = true;
IsGeneratable = false;
IndicatorAuthor = "NAYA,+237674724684";
IndicatorVersion = "2.0";
IndicatorDescription = "Advanced time and day filter with multiple datetime handling modes for precise entry control.";
}
public override void Initialize(SlotTypes slotType)
{
SlotType = slotType;
IndParam.IsAllowLTF = false;
// The ComboBox parameters
IndParam.ListParam[0].Caption = "Logic";
IndParam.ListParam[0].ItemList = new[]
{
"Enter the market between the specified days and times"
};
IndParam.ListParam[0].Index = 0;
IndParam.ListParam[0].Text = IndParam.ListParam[0].ItemList[IndParam.ListParam[0].Index];
IndParam.ListParam[0].Enabled = true;
IndParam.ListParam[0].ToolTip = "Indicator's logic.";
// DateTime Mode
IndParam.ListParam[1].Caption = "DateTime Mode";
IndParam.ListParam[1].ItemList = new[]
{
"Daily time filter (time range within each day)",
"Absolute datetime match (exact from-to combination)",
"Everyday ignores time filtering",
"Everyday uses time filtering",
"Weekly session (time only on first/last day)"
};
IndParam.ListParam[1].Index = 0;
IndParam.ListParam[1].Text = IndParam.ListParam[1].ItemList[IndParam.ListParam[1].Index];
IndParam.ListParam[1].Enabled = true;
IndParam.ListParam[1].ToolTip = "Determines how datetime filtering is applied.";
// From Day
IndParam.ListParam[2].Caption = "From day (incl.)";
IndParam.ListParam[2].ItemList = new[]
{
"Everyday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};
IndParam.ListParam[2].Index = 0;
IndParam.ListParam[2].Text = IndParam.ListParam[2].ItemList[IndParam.ListParam[2].Index];
IndParam.ListParam[2].Enabled = true;
IndParam.ListParam[2].ToolTip = "Starting day for the entry period.";
// To Day
IndParam.ListParam[3].Caption = "To day (excl.)";
IndParam.ListParam[3].ItemList = new[]
{
"Everyday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};
IndParam.ListParam[3].Index = 0;
IndParam.ListParam[3].Text = IndParam.ListParam[3].ItemList[IndParam.ListParam[3].Index];
IndParam.ListParam[3].Enabled = true;
IndParam.ListParam[3].ToolTip = "Ending day for the entry period.";
// The NumericUpDown parameters for time
IndParam.NumParam[0].Caption = "From hour (incl.)";
IndParam.NumParam[0].Value = 0;
IndParam.NumParam[0].Min = 0;
IndParam.NumParam[0].Max = 23;
IndParam.NumParam[0].Enabled = true;
IndParam.NumParam[0].ToolTip = "Starting hour for the entry period.";
IndParam.NumParam[1].Caption = "From minute (incl.)";
IndParam.NumParam[1].Value = 0;
IndParam.NumParam[1].Min = 0;
IndParam.NumParam[1].Max = 59;
IndParam.NumParam[1].Enabled = true;
IndParam.NumParam[1].ToolTip = "Starting minute for the entry period.";
IndParam.NumParam[2].Caption = "To hour (excl.)";
IndParam.NumParam[2].Value = 24;
IndParam.NumParam[2].Min = 0;
IndParam.NumParam[2].Max = 24;
IndParam.NumParam[2].Enabled = true;
IndParam.NumParam[2].ToolTip = "Ending hour for the entry period.";
IndParam.NumParam[3].Caption = "To minute (excl.)";
IndParam.NumParam[3].Value = 0;
IndParam.NumParam[3].Min = 0;
IndParam.NumParam[3].Max = 59;
IndParam.NumParam[3].Enabled = true;
IndParam.NumParam[3].ToolTip = "Ending minute for the entry period.";
}
public override void Calculate(IDataSet dataSet)
{
DataSet = dataSet;
// Reading the parameters
var logicType = IndParam.ListParam[0].Index;
var dateTimeMode = IndParam.ListParam[1].Index;
var fromDayIndex = IndParam.ListParam[2].Index;
var toDayIndex = IndParam.ListParam[3].Index;
var fromHour = (int)IndParam.NumParam[0].Value;
var fromMin = (int)IndParam.NumParam[1].Value;
var toHour = (int)IndParam.NumParam[2].Value;
var toMin = (int)IndParam.NumParam[3].Value;
// Convert day indices to DayOfWeek (0=Everyday, 1=Sunday=0, 2=Monday=1, etc.)
DayOfWeek? fromDay = fromDayIndex == 0 ? (DayOfWeek?)null : (DayOfWeek)(fromDayIndex - 1);
DayOfWeek? toDay = toDayIndex == 0 ? (DayOfWeek?)null : (DayOfWeek)(toDayIndex - 1);
var fromTime = new TimeSpan(fromHour, fromMin, 0);
var toTime = new TimeSpan(toHour, toMin, 0);
// Calculation
int firstBar = 2;
var signal = new double[Bars];
for (int bar = firstBar; bar < Bars; bar++)
{
DateTime currentTime = Time[bar];
TimeSpan currentTimeOfDay = currentTime.TimeOfDay;
DayOfWeek currentDayOfWeek = currentTime.DayOfWeek;
bool isInRange = false;
switch (dateTimeMode)
{
case 0: // Daily time filter
isInRange = IsInDailyTimeFilter(currentTime, currentDayOfWeek, currentTimeOfDay,
fromDay, toDay, fromTime, toTime);
break;
case 1: // Absolute datetime match
isInRange = IsInAbsoluteMatch(currentTime, currentDayOfWeek, currentTimeOfDay,
fromDay, toDay, fromTime, toTime);
break;
case 2: // Everyday ignores time
isInRange = IsInEverydayMode(currentTime, currentDayOfWeek, currentTimeOfDay,
fromDay, toDay, fromTime, toTime, false);
break;
case 3: // Everyday uses time
isInRange = IsInEverydayMode(currentTime, currentDayOfWeek, currentTimeOfDay,
fromDay, toDay, fromTime, toTime, true);
break;
case 4: // Weekly session
isInRange = IsInWeeklySession(currentTime, currentDayOfWeek, currentTimeOfDay,
fromDay, toDay, fromTime, toTime);
break;
}
signal[bar] = isInRange ? 1 : 0;
}
// Saving the components
Component = new IndicatorComp[4];
Component[0] = new IndicatorComp
{
CompName = "Is long entry allowed",
DataType = logicType == 0 ? IndComponentType.AllowOpenLong : IndComponentType.ForceCloseLong,
ChartType = IndChartType.NoChart,
ShowInDynInfo = false,
FirstBar = firstBar,
Value = signal
};
Component[1] = new IndicatorComp
{
CompName = "Is short entry allowed",
DataType = logicType == 0 ? IndComponentType.AllowOpenShort : IndComponentType.ForceCloseShort,
ChartType = IndChartType.NoChart,
ShowInDynInfo = false,
FirstBar = firstBar,
Value = signal
};
// Additional components for visualization
Component[2] = new IndicatorComp
{
CompName = "Time Filter Active",
DataType = IndComponentType.IndicatorValue,
ChartType = IndChartType.NoChart,
ShowInDynInfo = false,
FirstBar = firstBar,
Value = signal
};
Component[3] = new IndicatorComp
{
CompName = "Visualization",
DataType = IndComponentType.Other,
ChartType = IndChartType.NoChart,
ShowInDynInfo = false,
FirstBar = firstBar,
Value = signal
};
}
private bool IsInDailyTimeFilter(DateTime currentTime, DayOfWeek currentDayOfWeek, TimeSpan currentTimeOfDay,
DayOfWeek? fromDay, DayOfWeek? toDay, TimeSpan fromTime, TimeSpan toTime)
{
// Check if we're in the day range
if (fromDay == null && toDay == null)
{
// Both days are "Everyday" - check time only
return IsTimeInRange(currentTimeOfDay, fromTime, toTime);
}
if (fromDay == null || toDay == null)
{
// One day is "Everyday" - check time only
return IsTimeInRange(currentTimeOfDay, fromTime, toTime);
}
// Check if we're in the specified day range
bool isInDayRange = IsDayInRange(currentDayOfWeek, fromDay.Value, toDay.Value);
if (!isInDayRange)
return false;
// Apply time filter within the day
return IsTimeInRange(currentTimeOfDay, fromTime, toTime);
}
private bool IsInAbsoluteMatch(DateTime currentTime, DayOfWeek currentDayOfWeek, TimeSpan currentTimeOfDay,
DayOfWeek? fromDay, DayOfWeek? toDay, TimeSpan fromTime, TimeSpan toTime)
{
// If both days are "Everyday", treat as time-only filter
if (fromDay == null && toDay == null)
{
return IsTimeInRange(currentTimeOfDay, fromTime, toTime);
}
// If one day is "Everyday", treat as time-only filter
if (fromDay == null || toDay == null)
{
return IsTimeInRange(currentTimeOfDay, fromTime, toTime);
}
// For same day, check exact time range
if (fromDay.Value == toDay.Value)
{
return (currentDayOfWeek == fromDay.Value) && IsTimeInRange(currentTimeOfDay, fromTime, toTime);
}
// For different days, check boundaries precisely
if (currentDayOfWeek == fromDay.Value)
{
return currentTimeOfDay >= fromTime;
}
else if (currentDayOfWeek == toDay.Value)
{
return currentTimeOfDay < toTime;
}
else
{
return IsDayInRange(currentDayOfWeek, fromDay.Value, toDay.Value);
}
}
private bool IsInEverydayMode(DateTime currentTime, DayOfWeek currentDayOfWeek, TimeSpan currentTimeOfDay,
DayOfWeek? fromDay, DayOfWeek? toDay, TimeSpan fromTime, TimeSpan toTime, bool useTimeFilter)
{
// If both days are "Everyday"
if (fromDay == null && toDay == null)
{
if (useTimeFilter)
return IsTimeInRange(currentTimeOfDay, fromTime, toTime);
else
return true;
}
// If one day is "Everyday"
if (fromDay == null || toDay == null)
{
if (useTimeFilter)
return IsTimeInRange(currentTimeOfDay, fromTime, toTime);
else
return true;
}
// If specific days are set, use standard logic
bool isInDayRange = IsDayInRange(currentDayOfWeek, fromDay.Value, toDay.Value);
if (!isInDayRange)
return false;
if (useTimeFilter)
return IsTimeInRange(currentTimeOfDay, fromTime, toTime);
else
return true;
}
private bool IsInWeeklySession(DateTime currentTime, DayOfWeek currentDayOfWeek, TimeSpan currentTimeOfDay,
DayOfWeek? fromDay, DayOfWeek? toDay, TimeSpan fromTime, TimeSpan toTime)
{
// If both days are "Everyday", treat as time-only filter
if (fromDay == null && toDay == null)
{
return IsTimeInRange(currentTimeOfDay, fromTime, toTime);
}
// If one day is "Everyday", treat as time-only filter
if (fromDay == null || toDay == null)
{
return IsTimeInRange(currentTimeOfDay, fromTime, toTime);
}
// Check if we're in the day range
bool isInDayRange = IsDayInRange(currentDayOfWeek, fromDay.Value, toDay.Value);
if (!isInDayRange)
return false;
// For first day of range, check start time
if (currentDayOfWeek == fromDay.Value)
{
return currentTimeOfDay >= fromTime;
}
// For last day of range, check end time
else if (currentDayOfWeek == toDay.Value)
{
return currentTimeOfDay < toTime;
}
// For days in between, all times are valid
else
{
return true;
}
}
private bool IsTimeInRange(TimeSpan currentTime, TimeSpan fromTime, TimeSpan toTime)
{
if (fromTime < toTime)
{
// Normal time range (e.g., 09:00 to 17:00)
return currentTime >= fromTime && currentTime < toTime;
}
else if (fromTime > toTime)
{
// Overnight time range (e.g., 22:00 to 06:00)
return currentTime >= fromTime || currentTime < toTime;
}
else
{
// Same time - allow all day
return true;
}
}
private bool IsDayInRange(DayOfWeek currentDay, DayOfWeek fromDay, DayOfWeek toDay)
{
if (fromDay == toDay)
{
return currentDay == fromDay;
}
else if (fromDay < toDay)
{
// Normal week range (e.g., Monday to Friday)
return currentDay >= fromDay && currentDay <= toDay;
}
else
{
// Weekend-spanning range (e.g., Friday to Monday)
return currentDay >= fromDay || currentDay <= toDay;
}
}
public override void SetDescription()
{
var logicType = IndParam.ListParam[0].Index;
var dateTimeMode = IndParam.ListParam[1].Index;
var fromDayIndex = IndParam.ListParam[2].Index;
var toDayIndex = IndParam.ListParam[3].Index;
var fromHour = (int)IndParam.NumParam[0].Value;
var fromMin = (int)IndParam.NumParam[1].Value;
var toHour = (int)IndParam.NumParam[2].Value;
var toMin = (int)IndParam.NumParam[3].Value;
string fromDayStr = IndParam.ListParam[2].ItemList[fromDayIndex];
string toDayStr = IndParam.ListParam[3].ItemList[toDayIndex];
string fromTimeStr = fromHour.ToString("00") + ":" + fromMin.ToString("00");
string toTimeStr = toHour.ToString("00") + ":" + toMin.ToString("00");
string action = logicType == 0 ? "entry" : "exit";
string modeDescription = "";
switch (dateTimeMode)
{
case 0: modeDescription = " (daily time filter)"; break;
case 1: modeDescription = " (absolute datetime match)"; break;
case 2: modeDescription = " (everyday ignores time)"; break;
case 3: modeDescription = " (everyday uses time)"; break;
case 4: modeDescription = " (weekly session)"; break;
}
string description;
if (fromDayIndex == 0 && toDayIndex == 0)
{
if (dateTimeMode == 2) // Everyday ignores time
{
description = "{action} is allowed every day{modeDescription}";
}
else
{
description = "{action} time is between {fromTimeStr} (incl.) and {toTimeStr} (excl.) every day{modeDescription}";
}
}
else
{
description = "{action} is allowed from {fromDayStr} {fromTimeStr} (incl.) to {toDayStr} {toTimeStr} (excl.){modeDescription}";
}
if (logicType == 0)
{
EntryFilterLongDescription = description;
EntryFilterShortDescription = description;
}
else
{
ExitFilterLongDescription = description;
ExitFilterShortDescription = description;
}
}
public override string ToString()
{
var logicType = IndParam.ListParam[0].Index;
var dateTimeMode = IndParam.ListParam[1].Index;
var fromDayIndex = IndParam.ListParam[2].Index;
var toDayIndex = IndParam.ListParam[3].Index;
var fromHour = (int)IndParam.NumParam[0].Value;
var fromMin = (int)IndParam.NumParam[1].Value;
var toHour = (int)IndParam.NumParam[2].Value;
var toMin = (int)IndParam.NumParam[3].Value;
string fromDayStr = IndParam.ListParam[2].ItemList[fromDayIndex];
string toDayStr = IndParam.ListParam[3].ItemList[toDayIndex];
string fromTimeStr = fromHour.ToString("00") + ":" + fromMin.ToString("00");
string toTimeStr = toHour.ToString("00") + ":" + toMin.ToString("00");
string modeAbbr = "";
switch (dateTimeMode)
{
case 0: modeAbbr = "[DTF]"; break;
case 1: modeAbbr = "[ABS]"; break;
case 2: modeAbbr = "[ENT]"; break;
case 3: modeAbbr = "[EWT]"; break;
case 4: modeAbbr = "[WKS]"; break;
}
string action = logicType == 0 ? "Entry" : "Exit";
if (fromDayIndex == 0 && toDayIndex == 0)
{
if (dateTimeMode == 2)
{
return "{action} Time Filter {modeAbbr} (All Days)";
}
else
{
return "{action} Time Filter {modeAbbr} (Daily {fromTimeStr}-{toTimeStr})";
}
}
else
{
return "{action} Time Filter {modeAbbr} ({fromDayStr} {fromTimeStr}-{toDayStr} {toTimeStr})";
}
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344//+------------------------------------------------------------------+ //| AdvancedDayOfWeekEntryTime.mqh | //| Copyright 2024, NAYA +237674724684 | //| https://www.example.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2024, NAYA +237674724684" #property link "https://www.example.com" #property version "2.0" #property strict #include <Forexsb.com/Indicator.mqh> #include <Forexsb.com/Enumerations.mqh> //+------------------------------------------------------------------+ //| Enumeration for DateTime modes | //+------------------------------------------------------------------+ enum ENUM_DATETIME_MODE { MODE_DAILY, // Daily time filter MODE_ABSOLUTE, // Absolute datetime match MODE_EVERYDAY_NOTIME, // Everyday ignores time MODE_EVERYDAY_TIME, // Everyday uses time MODE_WEEKLY_SESSION // Weekly session }; //+------------------------------------------------------------------+ //| Enumeration for days of week | //+------------------------------------------------------------------+ enum ENUM_DAY_OF_WEEK { DAY_EVERYDAY, // Everyday DAY_SUNDAY, // Sunday DAY_MONDAY, // Monday DAY_TUESDAY, // Tuesday DAY_WEDNESDAY, // Wednesday DAY_THURSDAY, // Thursday DAY_FRIDAY, // Friday DAY_SATURDAY // Saturday }; //+------------------------------------------------------------------+ //| Class AdvancedDayOfWeekEntryTime | //+------------------------------------------------------------------+ class AdvancedDayOfWeekEntryTime : public Indicator { public: AdvancedDayOfWeekEntryTime(SlotTypes slotType); virtual void Calculate(DataSet &dataSet); private: bool IsInDailyTimeFilter(MqlDateTime &mqlTime, int fromDay, int toDay, int fromTime, int toTime, int currentTime); bool IsInAbsoluteMatch(MqlDateTime &mqlTime, int fromDay, int toDay, int fromTime, int toTime, int currentTime); bool IsInEverydayMode(MqlDateTime &mqlTime, int fromDay, int toDay, int fromTime, int toTime, int currentTime, bool useTimeFilter); bool IsInWeeklySession(MqlDateTime &mqlTime, int fromDay, int toDay, int fromTime, int toTime, int currentTime); bool IsTimeInRange(int currentTime, int fromTime, int toTime); bool IsDayInRange(int currentDay, int fromDay, int toDay); }; //+------------------------------------------------------------------+ //| Constructor | //+------------------------------------------------------------------+ void AdvancedDayOfWeekEntryTime::AdvancedDayOfWeekEntryTime(SlotTypes slotType) { SlotType = slotType; IndicatorName = "Advanced Day of Week Entry Time"; WarningMessage = "Advanced time and day filter for precise entry control with multiple datetime handling modes."; IsAllowLTF = true; ExecTime = ExecutionTime_DuringTheBar; IsSeparateChart = false; IsDiscreteValues = false; IsDefaultGroupAll = true; } //+------------------------------------------------------------------+ //| Calculate method | //+------------------------------------------------------------------+ void AdvancedDayOfWeekEntryTime::Calculate(DataSet &dataSet) { Data = GetPointer(dataSet); // Reading parameters int dateTimeMode = ListParam[1].Index; int fromDayIndex = ListParam[2].Index; int toDayIndex = ListParam[3].Index; int fromHour = (int)NumParam[0].Value; int fromMin = (int)NumParam[1].Value; int toHour = (int)NumParam[2].Value; int toMin = (int)NumParam[3].Value; // Convert day indices to DayOfWeek (0=Everyday=-1, 1=Sunday=0, 2=Monday=1, etc.) int fromDay = (fromDayIndex == 0) ? -1 : (fromDayIndex - 1); int toDay = (toDayIndex == 0) ? -1 : (toDayIndex - 1); // Create time spans in minutes int fromTime = fromHour * 60 + fromMin; int toTime = toHour * 60 + toMin; const int firstBar = 2; double signal[]; ArrayResize(signal, Data.Bars); ArrayInitialize(signal, 0); for(int bar = firstBar; bar < Data.Bars; bar++) { MqlDateTime mqlTime; TimeToStruct(Data.Time[bar], mqlTime); int currentTime = mqlTime.hour * 60 + mqlTime.min; int currentDay = mqlTime.day_of_week; // 0=Sunday, 1=Monday, ..., 6=Saturday bool isInRange = false; switch(dateTimeMode) { case 0: // Daily time filter isInRange = IsInDailyTimeFilter(mqlTime, fromDay, toDay, fromTime, toTime, currentTime); break; case 1: // Absolute datetime match isInRange = IsInAbsoluteMatch(mqlTime, fromDay, toDay, fromTime, toTime, currentTime); break; case 2: // Everyday ignores time isInRange = IsInEverydayMode(mqlTime, fromDay, toDay, fromTime, toTime, currentTime, false); break; case 3: // Everyday uses time isInRange = IsInEverydayMode(mqlTime, fromDay, toDay, fromTime, toTime, currentTime, true); break; case 4: // Weekly session isInRange = IsInWeeklySession(mqlTime, fromDay, toDay, fromTime, toTime, currentTime); break; } signal[bar] = isInRange ? 1 : 0; } // Set components ArrayResize(Component, 2); Component[0].CompName = "Is long entry allowed"; Component[0].DataType = IndComponentType_AllowOpenLong; Component[0].ShowInDynInfo = false; Component[0].FirstBar = firstBar; ArrayResize(Component[0].Value, Data.Bars); ArrayCopy(Component[0].Value, signal); Component[1].CompName = "Is short entry allowed"; Component[1].DataType = IndComponentType_AllowOpenShort; Component[1].ShowInDynInfo = false; Component[1].FirstBar = firstBar; ArrayResize(Component[1].Value, Data.Bars); ArrayCopy(Component[1].Value, signal); } //+------------------------------------------------------------------+ //| Daily Time Filter | //+------------------------------------------------------------------+ bool AdvancedDayOfWeekEntryTime::IsInDailyTimeFilter(MqlDateTime &mqlTime, int fromDay, int toDay, int fromTime, int toTime, int currentTime) { // Check if we're in the day range if(fromDay == -1 && toDay == -1) { // Both days are "Everyday" - check time only return IsTimeInRange(currentTime, fromTime, toTime); } if(fromDay == -1 || toDay == -1) { // One day is "Everyday" - check time only return IsTimeInRange(currentTime, fromTime, toTime); } // Check if we're in the specified day range bool isInDayRange = IsDayInRange(mqlTime.day_of_week, fromDay, toDay); if(!isInDayRange) return false; // Apply time filter within the day return IsTimeInRange(currentTime, fromTime, toTime); } //+------------------------------------------------------------------+ //| Absolute DateTime Match | //+------------------------------------------------------------------+ bool AdvancedDayOfWeekEntryTime::IsInAbsoluteMatch(MqlDateTime &mqlTime, int fromDay, int toDay, int fromTime, int toTime, int currentTime) { // If both days are "Everyday", treat as time-only filter if(fromDay == -1 && toDay == -1) { return IsTimeInRange(currentTime, fromTime, toTime); } // If one day is "Everyday", treat as time-only filter if(fromDay == -1 || toDay == -1) { return IsTimeInRange(currentTime, fromTime, toTime); } // For same day, check exact time range if(fromDay == toDay) { return (mqlTime.day_of_week == fromDay) && IsTimeInRange(currentTime, fromTime, toTime); } // For different days, check boundaries precisely if(mqlTime.day_of_week == fromDay) { return currentTime >= fromTime; } else if(mqlTime.day_of_week == toDay) { return currentTime < toTime; } else { return IsDayInRange(mqlTime.day_of_week, fromDay, toDay); } } //+------------------------------------------------------------------+ //| Everyday Mode | //+------------------------------------------------------------------+ bool AdvancedDayOfWeekEntryTime::IsInEverydayMode(MqlDateTime &mqlTime, int fromDay, int toDay, int fromTime, int toTime, int currentTime, bool useTimeFilter) { // If both days are "Everyday" if(fromDay == -1 && toDay == -1) { if(useTimeFilter) return IsTimeInRange(currentTime, fromTime, toTime); else return true; } // If one day is "Everyday" if(fromDay == -1 || toDay == -1) { if(useTimeFilter) return IsTimeInRange(currentTime, fromTime, toTime); else return true; } // If specific days are set, use standard logic bool isInDayRange = IsDayInRange(mqlTime.day_of_week, fromDay, toDay); if(!isInDayRange) return false; if(useTimeFilter) return IsTimeInRange(currentTime, fromTime, toTime); else return true; } //+------------------------------------------------------------------+ //| Weekly Session | //+------------------------------------------------------------------+ bool AdvancedDayOfWeekEntryTime::IsInWeeklySession(MqlDateTime &mqlTime, int fromDay, int toDay, int fromTime, int toTime, int currentTime) { // If both days are "Everyday", treat as time-only filter if(fromDay == -1 && toDay == -1) { return IsTimeInRange(currentTime, fromTime, toTime); } // If one day is "Everyday", treat as time-only filter if(fromDay == -1 || toDay == -1) { return IsTimeInRange(currentTime, fromTime, toTime); } // Check if we're in the day range bool isInDayRange = IsDayInRange(mqlTime.day_of_week, fromDay, toDay); if(!isInDayRange) return false; // For first day of range, check start time if(mqlTime.day_of_week == fromDay) { return currentTime >= fromTime; } // For last day of range, check end time else if(mqlTime.day_of_week == toDay) { return currentTime < toTime; } // For days in between, all times are valid else { return true; } } //+------------------------------------------------------------------+ //| Check if time is in range | //+------------------------------------------------------------------+ bool AdvancedDayOfWeekEntryTime::IsTimeInRange(int currentTime, int fromTime, int toTime) { if(fromTime < toTime) { // Normal time range (e.g., 09:00 to 17:00) return currentTime >= fromTime && currentTime < toTime; } else if(fromTime > toTime) { // Overnight time range (e.g., 22:00 to 06:00) return currentTime >= fromTime || currentTime < toTime; } else { // Same time - allow all day return true; } } //+------------------------------------------------------------------+ //| Check if day is in range | //+------------------------------------------------------------------+ bool AdvancedDayOfWeekEntryTime::IsDayInRange(int currentDay, int fromDay, int toDay) { if(fromDay == toDay) { return currentDay == fromDay; } else if(fromDay < toDay) { // Normal week range (e.g., Monday to Friday) return currentDay >= fromDay && currentDay <= toDay; } else { // Weekend-spanning range (e.g., Friday to Monday) return currentDay >= fromDay || currentDay <= toDay; } } //+------------------------------------------------------------------+
Risk warning: Forex, spread bets and CFD are leveraged products. They may not be suitable for you as they carry a high degree of risk to your capital and you can lose more than your initial investment. You should ensure you understand all of the risks.
Copyright © 2006 - 2025, Forex Software Ltd.;
Copyright © 2006 - 2025, Forex Software Ltd.;