Viewing the resource: Trading Day Filter System in MQL5

Trading Day Filter System in MQL5

Allan Munene Mutiiria 2025-06-26 14:31:26 87 Views
This MQL5 EA filters trading by user-defined days (e.g., Monday, Tuesday), enabling trades only on a...

Introduction

Imagine acting as a vigilant gatekeeper, controlling market access to ensure trading aligns with optimal days for maximum efficiency. The Trading Day Filter EA is your advanced market control tool, designed to restrict trading on MetaTrader 5 based on user-defined days of the week. Using input parameters ("Sunday", "Monday", etc.), it enables trading on selected days (e.g., Monday, Tuesday, Wednesday, Friday) while disabling it on others (e.g., Sunday, Thursday, Saturday). The system, driven by "Trading_Days_Filter()", checks the current day ("TimeToStruct()", "str_datetime.day_of_week") during initialization ("OnInit()") and on each price tick ("OnTick()"), providing clear notifications via "Print()". No automated trades are executed, focusing solely on filtering trading opportunities. This non-trading EA suits traders aiming to focus activity on high-volatility days, requiring integration with trading logic for execution.

This article is crafted with a professional, engaging, and seamless narrative, flowing like a well-calibrated market gatekeeper, designed to inform and captivate readers. Tailored for both novice and experienced traders, we’ll dissect each code component with clear, precise explanations, as if guiding an apprentice strategist through a market control project. With vivid examples—like enabling trading on EURUSD for Tuesday—and a polished tone, we’ll explore how the EA initializes, checks trading days, and ensures minimal resource use. Using a precision market gatekeeper metaphor, this guide will illuminate the code’s technical rigor, empowering you to control trading with confidence. Let’s activate the system and begin this market access expedition!

Strategy Blueprint

Let’s outline the EA’s filtering framework, like drafting specifications for a market gatekeeper:

  • Day Configuration: Defines trading days via inputs ("Sunday", "Monday", etc.), set to true/false (e.g., Monday=true, Sunday=false).

  • Day Check: Verifies the current day ("TimeToStruct()", "str_datetime.day_of_week") against inputs during "OnInit()" and "OnTick()".

  • Trading Control: Returns true/false via "Trading_Days_Filter()" to enable/disable trading, with "Print()" notifications.

  • Execution: Checks day status on initialization and each tick, integrating with external trading logic.

  • Enhancements: Adding time-based filters, session restrictions, or logging could improve functionality. This framework enforces precise market access, ensuring trading aligns with user-defined schedules.

Code Implementation

Let’s step into the market gatekeeper hub and dissect the MQL5 code that powers this Trading Day Filter EA. We’ll guide you through each phase like expert strategists, ensuring the narrative flows seamlessly with professional clarity and engaging precision that captivates readers. We’ll cover initialization, day filtering, and cleanup, with detailed explanations and examples—like enabling trading on EURUSD for Tuesday—to make it accessible for beginners. Each phase will build on the last, crafting a cohesive technical narrative that transforms code into a compelling market control project. Let’s power up the system and begin!

Phase 1: Constructing the Framework—Initialization

We start by building the filtering system, initializing the day check.

//+------------------------------------------------------------------+
//|                                        TRADING DAY FILTER EA.mq5 |
//|                           Copyright 2025, Allan Munene Mutiiria. |
//|                                   https://t.me/Forex_Algo_Trader |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, Allan Munene Mutiiria."
#property link      "https://t.me/Forex_Algo_Trader"
#property version   "1.00"

input group "TRADING DAYS FILTER SETTINGS"
input bool Sunday = false;
input bool Monday = true;
input bool Tuesday = true;
input bool Wednesday = true;
input bool Thursday = false;
input bool Friday = true;
input bool Saturday = false;

int OnInit(){
   if (Trading_Days_Filter()==true){
      Print("TRADING IS ALLOWED TODAY, DO YOUR STUFF HERE");
   }
   else {
      Print("TRADING IS NOT ALLOWED TODAY, YOU CAN'T DO YOUR STUFF HERE");
   }
   return(INIT_SUCCEEDED);
}

The system begins with the #property header, establishing copyright and contact details, like calibrating a gatekeeper’s control panel. The "OnInit()" function initializes the setup, defining input parameters ("Sunday", "Monday", etc.) with defaults (e.g., "Monday"=true, "Sunday"=false). It calls "Trading_Days_Filter()" to check the current day, printing whether trading is allowed (e.g., “TRADING IS ALLOWED TODAY”) or not. Returning INIT_SUCCEEDED signals, “Gatekeeper is ready, let’s control market access!” This primes the EA for day-based filtering, like a gatekeeper poised for action. Note: The code uses MetaQuotes’ copyright, but your metadata is presented for consistency.

Phase 2: Filtering Trading Days—Checking Market Access

We verify the current day to enable or disable trading, like a gatekeeper enforcing rules.

bool Trading_Days_Filter(){
   MqlDateTime str_datetime;
   TimeToStruct(TimeCurrent(),str_datetime);
   string today = "DAY OF WEEK";
   if (str_datetime.day_of_week == 0){today = "SUNDAY";}
   if (str_datetime.day_of_week == 1){today = "MONDAY";}
   if (str_datetime.day_of_week == 2){today = "TUEDAY";}
   if (str_datetime.day_of_week == 3){today = "WEDNESDAY";}
   if (str_datetime.day_of_week == 4){today = "THURSDAY";}
   if (str_datetime.day_of_week == 5){today = "FRIDAY";}
   if (str_datetime.day_of_week == 6){today = "SATURDAY";}
   if (
      (str_datetime.day_of_week==0 && Sunday == true) ||
      (str_datetime.day_of_week==1 && Monday == true) ||
      (str_datetime.day_of_week==2 && Tuesday == true) ||
      (str_datetime.day_of_week==3 && Wednesday == true) ||
      (str_datetime.day_of_week==4 && Thursday == true) ||
      (str_datetime.day_of_week==5 && Friday == true) ||
      (str_datetime.day_of_week==6 && Saturday == true)
   ){
      Print("--- Hey Trader! It's on ",today,": -Trading is ENABLED");
      return true;
   }
   else {
      Print("--- Hey Trader! It's on ",today,": -Trading is DISABLED");
      return false;
   }
}

In the filtering hub, "Trading_Days_Filter()" retrieves the current day ("TimeToStruct()", "str_datetime.day_of_week") and maps it to a string ("today", e.g., “TUESDAY” for day_of_week=2). It checks the day against input parameters ("Sunday", "Monday", etc.), returning true if the day is enabled (e.g., "Tuesday"=true) and printing “Trading is ENABLED” with "Print()", or false with “Trading is DISABLED”. For example, on EURUSD on a Tuesday (enabled), it prints “--- Hey Trader! It's on TUESDAY: -Trading is ENABLED”, like a gatekeeper granting access.

Phase 3: Monitoring Market Access—Tick-Based Checks

We check trading status on each price tick, like a gatekeeper’s ongoing vigilance.

void OnTick(){
   Trading_Days_Filter();
}

In the monitoring hub, "OnTick()" calls "Trading_Days_Filter()" on each price tick, ensuring continuous day checks with notifications. For example, on EURUSD on a Wednesday (enabled), it repeatedly confirms “Trading is ENABLED”, like a gatekeeper maintaining control. Note: The function returns a value but it’s not used; integrating with trading logic could enhance utility.

Phase 4: Shutting Down the System—Cleaning Up Resources

As our expedition concludes, we shut down the system, ensuring minimal resource use.

void OnDeinit(const int reason){
}

In the shutdown control room, "OnDeinit()" is empty, as no resources (e.g., objects, arrays) are used, like a lightweight gatekeeper requiring no cleanup. For completeness, a placeholder could be added:

void OnDeinit(const int reason){
   // No resources to clean up
}

This ensures the system shuts down cleanly, ready for the next task.

Why This EA is a Market Gatekeeper Triumph

The Trading Day Filter EA is a market access triumph, enforcing precise day-based trading control, like a master-crafted gatekeeper. Its simple yet effective filtering ("Trading_Days_Filter()", "TimeToStruct()") ensures disciplined market timing, with potential for time-based filters or trading integration. Picture enabling trading on EURUSD for Tuesday—strategic brilliance! Beginners will value the straightforward setup, while experts can enhance its functionality, making it essential for traders seeking structured market access.

Putting It All Together

To deploy this EA:

  1. Open MetaEditor in MetaTrader 5, like entering your market gatekeeper hub.

  2. Copy the code, compile with F5, and verify no errors—no strategist wants a faulty system!

  3. Attach the EA to your chart (e.g., EURUSD H1) and configure trading days via inputs (e.g., "Monday"=true, "Sunday"=false).

  4. Monitor logs (e.g., “Trading is ENABLED” on Tuesday) to confirm market access.

  5. Test on a demo account first—real trading deserves a trial run!

Conclusion

We’ve engineered a Trading Day Filter system that controls market access with precision, like a master-crafted gatekeeper. This MQL5 code is your strategic tool, brought to life with a seamless, professional narrative packed with clear explanations and vivid examples to fuel your trading discipline. Whether you’re a novice trader or a seasoned market strategist, this EA empowers you to time your trades with confidence. Ready to control? Watch our video guide on the website for a step-by-step creation process. Now, gatekeep your trading with precision! 🔍

Disclaimer: Trading is like navigating complex markets—challenging and risky. Losses can exceed deposits. Test strategies on a demo account before going live.

Disclaimer: The ideas and strategies presented in this resource are solely those of the author and are intended for informational and educational purposes only. They do not constitute financial advice, and past performance is not indicative of future results. All materials, including but not limited to text, images, files, and any downloadable content, are protected by copyright and intellectual property laws and are the exclusive property of Forex Algo-Trader or its licensors. Reproduction, distribution, modification, or commercial use of these materials without prior written consent from Forex Algo-Trader is strictly prohibited and may result in legal action. Users are advised to exercise extreme caution, perform thorough independent research, and consult with qualified financial professionals before implementing any trading strategies or decisions based on this resource, as trading in financial markets involves significant risk of loss.

Recent Comments

Go to discussion to Comment or View other Comments

No comments yet. Be the first to comment!