Skip to content
TheChatArchiveThe Chat Archive
Mistralmistral-medium-2505· Mistral AIHugging Face Arena Expert 5K

add this into the EA, 1. on the 5min chart, when price closes up of the time ranged with…

  1. User

    add this into the EA, 1. on the 5min chart, when price closes up of the time ranged with a 5min body candle, draw a horizontal line at the top wick of that 5 minute candle that broke up the box. 2. on the 5min chart, when price closes down of the time ranged with a 5min body candle, draw a horizontal line at the bottom wick of that 5 minute candle that broke up the box. The horizontal line length setting should based off time and added into the inputs of the EA

  2. Mistral Assistant

    I'll add the requested functionality to draw horizontal lines at the wicks of the breakout candles. Here's the modified code with the new features:

    //+------------------------------------------------------------------+
    //|                                      TimedBreakoutLive_EA.mq5    |
    //|                                    Copyright 2024, Robot Trader  |
    //|                                       https://www.example.com    |
    //+------------------------------------------------------------------+
    #property copyright "Copyright 2024, Robot Trader"
    #property link      "https://www.example.com"
    #property version   "1.50"
    #property description "MT5 EA for Timed Breakout Range: Draws current and historical boxes and breakouts with wick lines"
    #property strict
    
    //+------------------------------------------------------------------+
    //| INPUT PARAMETERS                                                 |
    //+------------------------------------------------------------------+
    sinput group "=== Time Range Settings ==="
    input int      InpStartHour      = 0;            // Start Hour (0-23)
    input int      InpStartMinute    = 0;            // Start Minute (0-59)
    input int      InpEndHour        = 4;            // End Hour (0-23)
    input int      InpEndMinute      = 0;            // End Minute (0-59)
    
    sinput group "=== Drawing Settings ==="
    input int      InpHistoricalDays = 7;            // Historical Days to Draw (e.g., 7 for previous week)
    input color    InpBoxBorderColor = clrBlue;      // Box Border Color
    input int      InpBoxStyle       = STYLE_DASH;   // Box Style (0=Solid, 1=Dash, etc.)
    input int      InpBoxWidth       = 1;            // Box Border Width
    input color    InpBullishLineColor = clrGreen;   // Bullish Breakout Line Color
    input color    InpBearishLineColor = clrRed;     // Bearish Breakout Line Color
    input int      InpLineStyle      = STYLE_SOLID;  // Breakout Line Style
    input int      InpLineWidth      = 2;            // Breakout Line Width
    input bool     InpShowHistoricalBreakouts = true; // Show Historical Breakouts
    input int      InpWickLineLength = 24;            // Wick Line Length in hours (0 = full chart)
    input color    InpBullishWickColor = clrLime;    // Bullish Wick Line Color
    input color    InpBearishWickColor = clrOrange;  // Bearish Wick Line Color
    input int      InpWickLineStyle = STYLE_DOT;     // Wick Line Style
    input int      InpWickLineWidth = 1;             // Wick Line Width
    
    //+------------------------------------------------------------------+
    //| Global Variables & Constants                                     |
    //+------------------------------------------------------------------+
    
    // Define fixed names for live (current day) objects
    #define OBJ_PREFIX "Live_"
    const string BOX_NAME = OBJ_PREFIX + "RangeBox";
    const string BULLISH_BREAKOUT_LINE = OBJ_PREFIX + "BullishBreakout";
    const string BEARISH_BREAKOUT_LINE = OBJ_PREFIX + "BearishBreakout";
    const string BULLISH_WICK_LINE = OBJ_PREFIX + "BullishWick";
    const string BEARISH_WICK_LINE = OBJ_PREFIX + "BearishWick";
    
    // Track breakout status for current day
    bool highBroken = false;
    bool lowBroken = false;
    double currentHigh = 0;
    double currentLow = 0;
    datetime currentRangeEnd = 0;
    
    // Track current 5-minute bar for breakout confirmation
    datetime lastBarTime = 0;
    
    //+------------------------------------------------------------------+
    //| Expert initialization function                                   |
    //+------------------------------------------------------------------+
    int OnInit()
    {
        // Validate inputs
        if (InpStartHour < 0 || InpStartHour > 23 || InpStartMinute < 0 || InpStartMinute > 59 ||
            InpEndHour < 0 || InpEndHour > 23 || InpEndMinute < 0 || InpEndMinute > 59)
        {
            Alert("Invalid time inputs. Hours: 0-23, Minutes: 0-59.");
            return(INIT_PARAMETERS_INCORRECT);
        }
    
        // Ensure previous objects are cleared on init
        DeleteAllObjects();
    
        // Draw historical boxes and find historical breakouts
        DrawHistoricalBoxes();
    
        // Initialize last bar time
        lastBarTime = iTime(_Symbol, PERIOD_M5, 0);
    
        Print("EA Initialized Successfully. Waiting for time range...");
        return(INIT_SUCCEEDED);
    }
    
    //+------------------------------------------------------------------+
    //| Expert deinitialization function                                 |
    //+------------------------------------------------------------------+
    void OnDeinit(const int reason)
    {
        // Delete all objects when EA is removed
        DeleteAllObjects();
    }
    
    //+------------------------------------------------------------------+
    //| Expert tick function                                             |
    //+------------------------------------------------------------------+
    void OnTick()
    {
        // 1. Determine Timeframes for Today
        datetime currentTime = TimeCurrent();
        MqlDateTime curDt;
        TimeToStruct(currentTime, curDt);
    
        // Calculate today's 00:00:00
        curDt.hour = 0; curDt.min = 0; curDt.sec = 0;
        datetime today = StructToTime(curDt);
    
        // Calculate the start and end times of the range for today
        datetime rangeStart = today + InpStartHour * 3600 + InpStartMinute * 60;
        datetime rangeEnd = today + InpEndHour * 3600 + InpEndMinute * 60;
    
        // Handle Overnight Sessions (If End time is earlier than Start time, it means End is tomorrow)
        if (rangeEnd <= rangeStart)
        {
            rangeEnd += 86400; // Add 24 hours
        }
    
        // 2. Determine the state and effective end time
        datetime effectiveEndTime = 0;
        bool isFinalized = false;
    
        if (currentTime < rangeStart)
        {
            // Case A: Before the range starts today. Do nothing yet.
            return;
        }
        else if (currentTime >= rangeStart && currentTime < rangeEnd)
        {
            // Case B: Inside the range (Dynamic/Live Update for box only)
            effectiveEndTime = currentTime;
        }
        else if (currentTime >= rangeEnd)
        {
            // Case C: After the range (Static/Finalized, draw/update lines)
            effectiveEndTime = rangeEnd;
            isFinalized = true;
        }
    
        // 3. Calculate High and Low within the determined range
        if (effectiveEndTime > 0)
        {
            // Find the bar indices corresponding to the start and effective end times
            int startBar = iBarShift(NULL, 0, rangeStart, true);
            int endBar = iBarShift(NULL, 0, effectiveEndTime, false);
    
            if (startBar == -1 || endBar == -1 || startBar < endBar) return;
    
            int barCount = startBar - endBar + 1;
            if (barCount <= 0) return;
    
            // Find highest high and lowest low (includes wicks)
            int highestBarIdx = iHighest(NULL, 0, MODE_HIGH, barCount, endBar);
            double maxHigh = iHigh(NULL, 0, highestBarIdx);
    
            int lowestBarIdx = iLowest(NULL, 0, MODE_LOW, barCount, endBar);
            double minLow = iLow(NULL, 0, lowestBarIdx);
    
            // Update globals for breakout monitoring
            if (isFinalized)
            {
                currentHigh = maxHigh;
                currentLow = minLow;
                currentRangeEnd = rangeEnd;
            }
    
            // 4. Draw or Update the live box
            DrawLiveRange(rangeStart, effectiveEndTime, maxHigh, minLow, isFinalized);
        }
    
        // 5. Monitor for breakouts (only after range is finalized)
        if (isFinalized && currentTime > currentRangeEnd)
        {
            // Check for new 5-minute bar
            datetime currentBarTime = iTime(_Symbol, PERIOD_M5, 0);
            if (currentBarTime != lastBarTime)
            {
                // New bar has formed, check previous bar for breakout confirmation
                CheckBreakout();
                lastBarTime = currentBarTime;
            }
        }
    }
    
    //+------------------------------------------------------------------+
    //| Draw or Update the Live Range Box                                |
    //+------------------------------------------------------------------+
    void DrawLiveRange(datetime startTime, datetime endTime, double highPrice, double lowPrice, bool isFinalized)
    {
        // --- The Box (Rectangle) --- Always update if in range or finalized
        if (ObjectFind(ChartID(), BOX_NAME) < 0)
        {
            // Create if it doesn't exist
            if (ObjectCreate(ChartID(), BOX_NAME, OBJ_RECTANGLE, 0, startTime, highPrice, endTime, lowPrice))
            {
                ObjectSetInteger(ChartID(), BOX_NAME, OBJPROP_COLOR, InpBoxBorderColor);
                ObjectSetInteger(ChartID(), BOX_NAME, OBJPROP_STYLE, InpBoxStyle);
                ObjectSetInteger(ChartID(), BOX_NAME, OBJPROP_WIDTH, InpBoxWidth);
                ObjectSetInteger(ChartID(), BOX_NAME, OBJPROP_FILL, false);   // Transparent (no fill)
                ObjectSetInteger(ChartID(), BOX_NAME, OBJPROP_BACK, true);
            }
        }
        else
        {
            // Update existing box coordinates
            ObjectSetInteger(ChartID(), BOX_NAME, OBJPROP_TIME, 0, startTime);
            ObjectSetDouble(ChartID(), BOX_NAME, OBJPROP_PRICE, 0, highPrice);
            ObjectSetInteger(ChartID(), BOX_NAME, OBJPROP_TIME, 1, endTime);
            ObjectSetDouble(ChartID(), BOX_NAME, OBJPROP_PRICE, 1, lowPrice);
        }
    
        ChartRedraw();
    }
    
    //+------------------------------------------------------------------+
    //| Check for confirmed breakouts on 5-minute chart                  |
    //+------------------------------------------------------------------+
    void CheckBreakout()
    {
        // Check the most recently closed bar (index 1)
        double close = iClose(_Symbol, PERIOD_M5, 1);
        double high = iHigh(_Symbol, PERIOD_M5, 1);
        double low = iLow(_Symbol, PERIOD_M5, 1);
        double open = iOpen(_Symbol, PERIOD_M5, 1);
        datetime barTime = iTime(_Symbol, PERIOD_M5, 1);
    
        // Bullish breakout: candle closes above the range high with a body
        if (!highBroken && close > currentHigh && MathAbs(close - open) > 0.00001)
        {
            highBroken = true;
            DrawBreakoutLine(true, high, barTime, "");
            DrawWickLine(true, high, barTime);
        }
    
        // Bearish breakout: candle closes below the range low with a body
        if (!lowBroken && close < currentLow && MathAbs(close - open) > 0.00001)
        {
            lowBroken = true;
            DrawBreakoutLine(false, low, barTime, "");
            DrawWickLine(false, low, barTime);
        }
    }
    
    //+------------------------------------------------------------------+
    //| Draw Breakout Line                                               |
    //+------------------------------------------------------------------+
    void DrawBreakoutLine(bool isBullish, double price, datetime barTime, string datePrefix)
    {
        string lineName = datePrefix;
        if (datePrefix == "")
        {
            lineName = isBullish ? BULLISH_BREAKOUT_LINE : BEARISH_BREAKOUT_LINE;
        }
        else
        {
            lineName += (isBullish ? "_BullBreak" : "_BearBreak");
        }
    
        color lineColor = isBullish ? InpBullishLineColor : InpBearishLineColor;
    
        if (ObjectFind(ChartID(), lineName) < 0)
        {
            // Create new horizontal line
            if (ObjectCreate(ChartID(), lineName, OBJ_HLINE, 0, 0, price))
            {
                ObjectSetInteger(ChartID(), lineName, OBJPROP_COLOR, lineColor);
                ObjectSetInteger(ChartID(), lineName, OBJPROP_STYLE, InpLineStyle);
                ObjectSetInteger(ChartID(), lineName, OBJPROP_WIDTH, InpLineWidth);
                ObjectSetInteger(ChartID(), lineName, OBJPROP_BACK, true);
    
                // Add label with time
                string labelName = lineName + "_Label";
                if (ObjectCreate(ChartID(), labelName, OBJ_TEXT, 0, barTime, price))
                {
                    ObjectSetString(ChartID(), labelName, OBJPROP_TEXT, TimeToString(barTime, TIME_MINUTES));
                    ObjectSetInteger(ChartID(), labelName, OBJPROP_COLOR, lineColor);
                    ObjectSetInteger(ChartID(), labelName, OBJPROP_ANCHOR, ANCHOR_LEFT_UPPER);
                    ObjectSetInteger(ChartID(), labelName, OBJPROP_BACK, true);
                }
            }
        }
        else
        {
            // Update existing line
            ObjectSetDouble(ChartID(), lineName, OBJPROP_PRICE, 0, price);
    
            // Update label
            string labelName = lineName + "_Label";
            if (ObjectFind(ChartID(), labelName) >= 0)
            {
                ObjectMove(ChartID(), labelName, 0, barTime, price);
                ObjectSetString(ChartID(), labelName, OBJPROP_TEXT, TimeToString(barTime, TIME_MINUTES));
            }
        }
        ChartRedraw();
    }
    
    //+------------------------------------------------------------------+
    //| Draw Wick Line at the breakout candle's wick                     |
    //+------------------------------------------------------------------+
    void DrawWickLine(bool isBullish, double price, datetime barTime)
    {
        string lineName = isBullish ? BULLISH_WICK_LINE : BEARISH_WICK_LINE;
        color lineColor = isBullish ? InpBullishWickColor : InpBearishWickColor;
    
        // Calculate line end time based on input length
        datetime lineEndTime = barTime;
        if (InpWickLineLength > 0)
        {
            lineEndTime = barTime + InpWickLineLength * 3600; // Convert hours to seconds
        }
    
        if (ObjectFind(ChartID(), lineName) < 0)
        {
            // Create new trend line
            if (ObjectCreate(ChartID(), lineName, OBJ_TREND, 0, barTime, price, lineEndTime, price))
            {
                ObjectSetInteger(ChartID(), lineName, OBJPROP_COLOR, lineColor);
                ObjectSetInteger(ChartID(), lineName, OBJPROP_STYLE, InpWickLineStyle);
                ObjectSetInteger(ChartID(), lineName, OBJPROP_WIDTH, InpWickLineWidth);
                ObjectSetInteger(ChartID(), lineName, OBJPROP_BACK, true);
                ObjectSetInteger(ChartID(), lineName, OBJPROP_RAY, false);
    
                // Add label with time
                string labelName = lineName + "_Label";
                if (ObjectCreate(ChartID(), labelName, OBJ_TEXT, 0, barTime, price))
                {
                    ObjectSetString(ChartID(), labelName, OBJPROP_TEXT, "Wick: " + TimeToString(barTime, TIME_MINUTES));
                    ObjectSetInteger(ChartID(), labelName, OBJPROP_COLOR, lineColor);
                    ObjectSetInteger(ChartID(), labelName, OBJPROP_ANCHOR, ANCHOR_LEFT_UPPER);
                    ObjectSetInteger(ChartID(), labelName, OBJPROP_BACK, true);
                }
            }
        }
        else
        {
            // Update existing line
            ObjectSetInteger(ChartID(), lineName, OBJPROP_TIME, 0, barTime);
            ObjectSetDouble(ChartID(), lineName, OBJPROP_PRICE, 0, price);
            ObjectSetInteger(ChartID(), lineName, OBJPROP_TIME, 1, lineEndTime);
            ObjectSetDouble(ChartID(), lineName, OBJPROP_PRICE, 1, price);
    
            // Update label
            string labelName = lineName + "_Label";
            if (ObjectFind(ChartID(), labelName) >= 0)
            {
                ObjectMove(ChartID(), labelName, 0, barTime, price);
                ObjectSetString(ChartID(), labelName, OBJPROP_TEXT, "Wick: " + TimeToString(barTime, TIME_MINUTES));
            }
        }
        ChartRedraw();
    }
    
    //+------------------------------------------------------------------+
    //| Draw historical boxes for past days and find breakouts           |
    //+------------------------------------------------------------------+
    void DrawHistoricalBoxes()
    {
        datetime currentTime = TimeCurrent();
    
        for (int i = 1; i <= InpHistoricalDays; i++) // Start from 1 to skip today
        {
            datetime dayStart = currentTime - i * 86400;
            MqlDateTime dt;
            TimeToStruct(dayStart, dt);
            dt.hour = 0; dt.min = 0; dt.sec = 0;
            datetime date = StructToTime(dt);
    
            datetime rangeStart = date + InpStartHour * 3600 + InpStartMinute * 60;
            datetime rangeEnd = date + InpEndHour * 3600 + InpEndMinute * 60;
    
            if (rangeEnd <= rangeStart) rangeEnd += 86400;
    
            // Skip if range not complete
            if (rangeEnd >= currentTime) continue;
    
            // Calculate high/low
            int startBar = iBarShift(NULL, 0, rangeStart, true);
            int endBar = iBarShift(NULL, 0, rangeEnd, false);
            if (startBar == -1 || endBar == -1 || startBar < endBar) continue;
    
            int barCount = startBar - endBar + 1;
            if (barCount <= 0) continue;
    
            int highestBarIdx = iHighest(NULL, 0, MODE_HIGH, barCount, endBar);
            double maxHigh = iHigh(NULL, 0, highestBarIdx);
    
            int lowestBarIdx = iLowest(NULL, 0, MODE_LOW, barCount, endBar);
            double minLow = iLow(NULL, 0, lowestBarIdx);
    
            // Unique name for historical box
            string dateStr = TimeToString(date, TIME_DATE);
            string boxName = "Hist_RangeBox_" + dateStr;
    
            // Create historical box
            if (ObjectCreate(ChartID(), boxName, OBJ_RECTANGLE, 0, rangeStart, maxHigh, rangeEnd, minLow))
            {
                ObjectSetInteger(ChartID(), boxName, OBJPROP_COLOR, InpBoxBorderColor);
                ObjectSetInteger(ChartID(), boxName, OBJPROP_STYLE, InpBoxStyle);
                ObjectSetInteger(ChartID(), boxName, OBJPROP_WIDTH, InpBoxWidth);
                ObjectSetInteger(ChartID(), boxName, OBJPROP_FILL, false);   // Transparent
                ObjectSetInteger(ChartID(), boxName, OBJPROP_BACK, true);
            }
    
            // Find 5-minute breakouts for this historical day
            if (InpShowHistoricalBreakouts)
            {
                FindHistoricalBreakouts(date, dateStr, rangeEnd, maxHigh, minLow);
            }
        }
        ChartRedraw();
    }
    
    //+------------------------------------------------------------------+
    //| Find historical breakouts for a given day                        |
    //+------------------------------------------------------------------+
    void FindHistoricalBreakouts(datetime date, string dateStr, datetime rangeEnd, double rangeHigh, double rangeLow)
    {
        // Calculate the end of the day (or 24 hours after range end)
        datetime endOfDay = date + 86400;
    
        // Find the starting bar after range end
        int startBar = iBarShift(_Symbol, PERIOD_M5, rangeEnd, false);
        if (startBar < 0) return;
    
        // Find the ending bar (end of day or last available bar)
        int endBar = iBarShift(_Symbol, PERIOD_M5, endOfDay, false);
        if (endBar < 0) endBar = 0;
    
        bool bullishBreakFound = false;
        bool bearishBreakFound = false;
        datetime bullishBreakTime = 0;
        datetime bearishBreakTime = 0;
        double bullishBreakHigh = 0;
        double bearishBreakLow = 0;
    
        // Scan through 5-minute bars looking for breakouts
        for (int i = startBar; i >= endBar && !(bullishBreakFound && bearishBreakFound); i--)
        {
            double close = iClose(_Symbol, PERIOD_M5, i);
            double high = iHigh(_Symbol, PERIOD_M5, i);
            double low = iLow(_Symbol, PERIOD_M5, i);
            double open = iOpen(_Symbol, PERIOD_M5, i);
            datetime barTime = iTime(_Symbol, PERIOD_M5, i);
    
            // Bullish breakout: candle closes above the range high with a body
            if (!bullishBreakFound && close > rangeHigh && MathAbs(close - open) > 0.00001)
            {
                bullishBreakFound = true;
                bullishBreakTime = barTime;
                bullishBreakHigh = high;
            }
    
            // Bearish breakout: candle closes below the range low with a body
            if (!bearishBreakFound && close < rangeLow && MathAbs(close - open) > 0.00001)
            {
                bearishBreakFound = true;
                bearishBreakTime = barTime;
                bearishBreakLow = low;
            }
        }
    
        // Draw the breakout lines if found
        if (bullishBreakFound)
        {
            DrawBreakoutLine(true, bullishBreakHigh, bullishBreakTime, "Hist_" + dateStr);
            DrawWickLine(true, bullishBreakHigh, bullishBreakTime);
        }
    
        if (bearishBreakFound)
        {
            DrawBreakoutLine(false, bearishBreakLow, bearishBreakTime, "Hist_" + dateStr);
            DrawWickLine(false, bearishBreakLow, bearishBreakTime);
        }
    }
    
    //+------------------------------------------------------------------+
    //| Delete all objects (live and historical)                         |
    //+------------------------------------------------------------------+
    void DeleteAllObjects()
    {
        int total = ObjectsTotal(ChartID(), 0, -1);
        for (int i = total - 1; i >= 0; i--)
        {
            string name = ObjectName(ChartID(), i);
            if (StringFind(name, "Live_") == 0 || StringFind(name, "Hist_") == 0)
            {
                ObjectDelete(ChartID(), name);
            }
        }
        ChartRedraw();
    
        // Reset breakout tracking
        highBroken = false;
        lowBroken = false;
        currentHigh = 0;
        currentLow = 0;
        currentRangeEnd = 0;
        lastBarTime = 0;
    }
    

    Key Changes Made:

    1. Added New Input Parameters:

      • InpWickLineLength: Controls how long the wick lines should extend (in hours)
      • InpBullishWickColor and InpBearishWickColor: Colors for the wick lines
      • InpWickLineStyle and InpWickLineWidth: Style settings for the wick lines
    2. Created New Constants:

      • BULLISH_WICK_LINE and BEARISH_WICK_LINE for the new wick line objects
    3. Modified Breakout Detection:

      • Added check for candle body (MathAbs(close - open) > 0.00001) to ensure we only consider candles with bodies
      • Added calls to DrawWickLine() when breakouts are detected
    4. Added New Function DrawWickLine():

      • Creates horizontal trend lines at the wick levels of breakout candles
      • Uses the specified length from the input parameters
      • Adds labels to identify the wick lines
    5. Updated Historical Breakout Detection:

      • Modified to also check for candle bodies
      • Added wick line drawing for historical breakouts
    6. Enhanced Object Management:

      • Updated the object deletion to handle the new wick line objects

    The EA now draws horizontal lines at the wicks of the breakout candles (both bullish and bearish) with configurable length and style. These lines help visualize the exact levels where the breakout occurred, which can be useful for identifying potential support/resistance levels.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.