fxDreema

    • Register
    • Login
    • Search
    • Back to the main page
    • Categories
    • Recent
    • Tags
    • Popular
    • Search

    How to Get Broker's Trading Hours and Allow Trading for the First 30 Minutes of the Last Hour?

    Questions & Answers
    customcode expertadvisor brokertime mql5
    2
    6
    436
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      Adam692 last edited by

      I want to use a custom code that retrieves the broker's trading hours and allows trading for the first 30 minutes of the last trading hour, then disables it.

      I'm not an experienced coder, and I tried generating the code with Copilot, but it doesn’t place trades.

      Does anyone have a working solution for this?

      Here’s my draft: https://fxdreema.com/shared/2ohjfaKyb

      1 Reply Last reply Reply Quote 0
      • jstap
        jstap last edited by jstap

        Try connecting try another block, in fx blocks need to be connected to something:

        image.png

        Learn fxDreema Without the Wait!

        My comprehensive book, available on Amazon, is packed with examples and invaluable insights to help you fast-track your learning journey.

        The paperback and hardback editions include MT4 & MT5 QR codes for easy access to all prebuilt projects and robots, including my latest gold trading robot!

        Don’t miss out

        Click here➡️ https://mybook.to/fxDreema to get your copy today!

        Enjoy! 😊

        A 1 Reply Last reply Reply Quote 0
        • A
          Adam692 @jstap last edited by

          @jstap If I connect it to anything, I get an error message when I try to compile it.
          02.png

          1 Reply Last reply Reply Quote 0
          • jstap
            jstap last edited by

            If you put // Infront of on tick //on tick() it will remove 1/2 of the errors, because on FX you are running it on the on tick tab. The rest of the errors are more difficult, try asking AI what code is a custom function, what is Global variables, includes, and what part goes on tick, then use this information to create a custom block, this is the only way I know to fix these errors.

            Learn fxDreema Without the Wait!

            My comprehensive book, available on Amazon, is packed with examples and invaluable insights to help you fast-track your learning journey.

            The paperback and hardback editions include MT4 & MT5 QR codes for easy access to all prebuilt projects and robots, including my latest gold trading robot!

            Don’t miss out

            Click here➡️ https://mybook.to/fxDreema to get your copy today!

            Enjoy! 😊

            A 1 Reply Last reply Reply Quote 0
            • A
              Adam692 @jstap last edited by

              @jstap I tried multiple times to fix the code with AI, but there were always errors. This version of the code had the least errors, so I’m sharing it now:
              // Custom Code for FxDreema to handle last hour trading logic

              // Ensure the platform is MT5
              #ifdef MQL5

              // Get the current server time
              datetime currentTime = TimeCurrent();

              // Get the trading session end time for the current symbol
              datetime sessionEndTime = (datetime)SymbolInfoInteger(_Symbol, SYMBOL_SESSION_END);

              // Calculate the difference in seconds between the current time and session end time
              int secondsToEnd = (int)(sessionEndTime - currentTime);

              // Check if we are in the last hour of trading
              if (secondsToEnd <= 3600 && secondsToEnd > 0) // 3600 seconds = 1 hour
              {
              if (!isLastHourActive) // If not already active
              {
              isLastHourActive = true;
              lastHourStartTime = currentTime; // Record the start time
              }
              }

              // Deactivate after 30 minutes
              if (isLastHourActive && (currentTime - lastHourStartTime) > 1800) // 1800 seconds = 30 minutes
              {
              isLastHourActive = false;
              }

              #else
              Print("This code requires MT5 platform.");
              #endif

              These are the error messages I received:
              'SYMBOL_SESSION_END' - undeclared identifier
              'SymbolInfoInteger' - no one of the overloads can be applied to the function call

              I asked the AI to analyze the code, and this is the explanation it provided:

              The provided code snippet is designed to handle logic related to the last hour of trading for a specific symbol. Here's an analysis of its components:


              1. Platform Check

              #ifdef MQL5
              • Purpose: Ensures that the code is only executed on the MetaTrader 5 (MT5) platform. If the platform is not MT5, the code will not run and will instead print a message.


              1. Current Server Time

              datetime currentTime = TimeCurrent();
              • Purpose: Retrieves the current server time. This is used as the reference point for all time-based calculations.


              1. Trading Session End Time

              datetime sessionEndTime = (datetime)SymbolInfoInteger(_Symbol, SYMBOL_SESSION_END);
              • Purpose: Fetches the end time of the trading session for the current symbol (_Symbol).
              • Key Function: SymbolInfoInteger retrieves symbol-specific properties, and SYMBOL_SESSION_END provides the session's end time.


              1. Time Difference Calculation

              int secondsToEnd = (int)(sessionEndTime - currentTime);
              • Purpose: Calculates the number of seconds remaining until the trading session ends.
              • Key Variable: secondsToEnd is used to determine if the current time is within the last hour of trading.


              1. Last Hour Activation

              if (secondsToEnd <= 3600 && secondsToEnd > 0) // 3600 seconds = 1 hour
              {
              if (!isLastHourActive) // If not already active
              {
              isLastHourActive = true;
              lastHourStartTime = currentTime; // Record the start time
              }
              }
              • Purpose: Checks if the current time is within the last hour of trading (secondsToEnd <= 3600).
              • Logic:
              o If the condition is met and the isLastHourActive flag is not already set, it activates the flag and records the start time of the last hour.


              1. Deactivation After 30 Minutes

              if (isLastHourActive && (currentTime - lastHourStartTime) > 1800) // 1800 seconds = 30 minutes
              {
              isLastHourActive = false;
              }
              • Purpose: Deactivates the isLastHourActive flag if 30 minutes have passed since the last hour started.
              • Logic:
              o Ensures that the logic only remains active for the first 30 minutes of the last hour.


              1. Fallback for Non-MT5 Platforms

              #else
              Print("This code requires MT5 platform.");
              #endif
              • Purpose: Prints a message if the code is executed on a platform other than MT5.
              • Fallback Behavior: Prevents execution on unsupported platforms.


              Key Variables

              1. isLastHourActive:
                o A boolean flag indicating whether the last hour logic is active.
              2. lastHourStartTime:
                o Stores the time when the last hour logic was activated.

              Summary
              • The code is designed to monitor the last hour of trading for a symbol and activate specific logic during this period.
              • It ensures compatibility with MT5 and gracefully handles unsupported platforms.
              • The logic is time-sensitive, activating for the first 30 minutes of the last hour and deactivating afterward.

              https://fxdreema.com/shared/ivT0aNgqc

              1 Reply Last reply Reply Quote 0
              • jstap
                jstap last edited by

                Yes but this doesn't create a custom block, this topic will give you an idea of how to create: https://fxdreema.com/forum/topic/21603/close-all-mt5-trades-quickly/14

                Learn fxDreema Without the Wait!

                My comprehensive book, available on Amazon, is packed with examples and invaluable insights to help you fast-track your learning journey.

                The paperback and hardback editions include MT4 & MT5 QR codes for easy access to all prebuilt projects and robots, including my latest gold trading robot!

                Don’t miss out

                Click here➡️ https://mybook.to/fxDreema to get your copy today!

                Enjoy! 😊

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post

                Online Users

                M
                B
                T
                S

                12
                Online

                146.7k
                Users

                22.4k
                Topics

                122.6k
                Posts

                Powered by NodeBB Forums | Contributors