Creating Time Range Breakout EA for MT5

Aug 28, 2024

Time Range Breakout EA in MetaTrader 5

Introduction

  • Presenter: Toby
  • Objective: Create a Time Range Breakout EA (Expert Advisor) for MetaTrader 5.
  • Goal: Achieve trading results similar to a successful strategy running on a live account for over a year.
  • Complexity: This EA will be more complex than previous examples.

Recap of Previous Videos

  • Suggested to review earlier videos on:
    • Simple Time Entry EA
    • Simple Moving Average Crossover EA

EA Logic Overview

  • Define a time range with a start and an end time.
  • The EA will:
    • Save the highest and lowest price within this range.
    • Execute a Buy trade if the price breaks above the highest point.
    • Execute a Sell trade if the price breaks below the lowest point.
    • Close trades at a specified close time.

Starting with MetaEditor

  1. Open MetaTrader 5.
  2. Create a new Expert Advisor:
    • Navigate to top left, select "New" and choose "Expert Advisor" template.
    • Name it "Time Range EA".
  3. Clean up the initial code for better formatting.

Input Variables Definition

  • Create an inputs section:
    • Input for Start Time of the Range:
      • Type: Integer, Default: 600 (10:00 AM)
      • Comment: "Range start time in minutes"
    • Input for Duration of the Range:
      • Type: Integer, Comment: "Duration in minutes"
      • Example: 120 (2 hours)
    • Input for Close Time:
      • Type: Integer, Default: 1200 (8:00 PM)
      • Comment: "Range close time"
    • Input for Lot Size:
      • Type: Double, Default: 0.01
      • Comment: "Lot size"
    • Input for Magic Number:
      • Type: Integer, Default: 12345
      • Comment: "Magic number" (unique identifier for trades)

Input Validation Checks

  • Implement checks in the OnInit function:
    • Validate Magic Number: should be > 0.
    • Validate Lot Size: should be > 0, with a max of 1.
    • Validate Range Start: must be >= 0 and < 1440 (minutes in a day).
    • Validate Duration: must be > 0 and < 1440.
    • Validate Close Time: must be > 0 and < 1440.
    • Ensure Close Time does not equal Start Time + Duration.

Global Variables Definition

  • Create a Global Variables section:
    • Structure Definition: Create a structure for range variables:
      • Members include: Start Time, End Time, Close Time, High, Low, Entry Flags.
      • Constructor initializes these variables.
  • Define a variable of type range structure.

Additional Variables Required

  • Include variables for:
    • Previous Tick and Last Tick events (both of type MqlTick).
    • Trading class instance for trade operations.
  • Include necessary libraries:
    • #include <Trade/Trade.mqh> for trade operations.

Next Steps

  • The actual logic for the EA will be coded in the OnTick function, to be covered in the next part.

Conclusion

  • Viewers encouraged to like the video and subscribe for future updates.
  • Open for questions in the comments regarding mq4/mql5 or automated trading.