teikee

Newbie
Feb 5, 2023
7
1
4
34
Yes, they are rectangles. How do you let your EA know which rectangle to use? Is there an input parameter for the name or something?
The names for the S/R zone rectangles is set as IndicatorName + "-HLINE-Z-" + LineNumber, where IndicatorName is the input parameter of the indicator and LineNumber is generated from the S/R level's price (e.g., if it's on 1.06425, the LineNumber will be equal 106425).
MQL4:
double resistanceTop = 0;
        double resistanceBottom = 0;
        double supportTop = 0;
        double supportBottom = 0;
 
        int totalObjects = ObjectsTotal();
        double minDistanceResist = 1.0e+10;
        double minDistanceSupport = 1.0e+10;
 
        // Search for rectangles on the chart and check their colors
        for (int i = 0; i < totalObjects; i++) {
            string objectName = ObjectName(i);
            color objectColor = (color)ObjectGetInteger(0, objectName, OBJPROP_COLOR, i);
 
            if (objectColor == ResistanceRectangleColor) {
                double rectTop = ObjectGet(objectName, OBJPROP_PRICE1);
                double rectBottom = ObjectGet(objectName, OBJPROP_PRICE2);
                double distance = MathMin(MathAbs(currentPrice - rectTop), MathAbs(currentPrice - rectBottom));
 
                if (distance < minDistanceResist) {
                    minDistanceResist = distance;
                    resistanceTop = rectTop;
                    resistanceBottom = rectBottom;
                }
            }
 
            if (objectColor == SupportRectangleColor) {
                double rectTop = ObjectGet(objectName, OBJPROP_PRICE1);
                double rectBottom = ObjectGet(objectName, OBJPROP_PRICE2);
                double distance = MathMin(MathAbs(currentPrice - rectTop), MathAbs(currentPrice - rectBottom));
 
                if (distance < minDistanceSupport) {
                    minDistanceSupport = distance;
                    supportTop = rectTop;
                    supportBottom = rectBottom;


This is my code. It will search for the nearest object and match with object color.
Appreciated if you could advise me on my coding.
 
Last edited by a moderator:

Enivid

Administrator
Staff member
Nov 30, 2008
19,301
1,529
144
Odesa
www.earnforex.com
MQL4:
double resistanceTop = 0;
        double resistanceBottom = 0;
        double supportTop = 0;
        double supportBottom = 0;
 
        int totalObjects = ObjectsTotal();
        double minDistanceResist = 1.0e+10;
        double minDistanceSupport = 1.0e+10;
 
        // Search for rectangles on the chart and check their colors
        for (int i = 0; i < totalObjects; i++) {
            string objectName = ObjectName(i);
            color objectColor = (color)ObjectGetInteger(0, objectName, OBJPROP_COLOR, i);
 
            if (objectColor == ResistanceRectangleColor) {
                double rectTop = ObjectGet(objectName, OBJPROP_PRICE1);
                double rectBottom = ObjectGet(objectName, OBJPROP_PRICE2);
                double distance = MathMin(MathAbs(currentPrice - rectTop), MathAbs(currentPrice - rectBottom));
 
                if (distance < minDistanceResist) {
                    minDistanceResist = distance;
                    resistanceTop = rectTop;
                    resistanceBottom = rectBottom;
                }
            }
 
            if (objectColor == SupportRectangleColor) {
                double rectTop = ObjectGet(objectName, OBJPROP_PRICE1);
                double rectBottom = ObjectGet(objectName, OBJPROP_PRICE2);
                double distance = MathMin(MathAbs(currentPrice - rectTop), MathAbs(currentPrice - rectBottom));
 
                if (distance < minDistanceSupport) {
                    minDistanceSupport = distance;
                    supportTop = rectTop;
                    supportBottom = rectBottom;


This is my code. It will search for the nearest object and match with object color.
Appreciated if you could advise me on my coding.
That should work fine. Just make sure you are setting correct colors for it to detect.
 

teikee

Newbie
Feb 5, 2023
7
1
4
34
That should work fine. Just make sure you are setting correct colors for it to detect.
I have tried few times and the colors setting is correct. But my EA is still not able to interact with this indicator even i have changed the object selectable to true. Not sure what goes wrong.
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,301
1,529
144
Odesa
www.earnforex.com
I have tried few times and the colors setting is correct. But my EA is still not able to interact with this indicator even i have changed the object selectable to true. Not sure what goes wrong.
The objects being selectable or not doesn't matter here. Could you please attach your entire EA here, so I could test this issue?
 

teikee

Newbie
Feb 5, 2023
7
1
4
34
The objects being selectable or not doesn't matter here. Could you please attach your entire EA here, so I could test this issue?
After recheck my EA and indicator coding, I have found the problem and have solved it finally.

The problem is on the rectangle upper value and lower value. My EA retrieve the object price1 as upper value and price2 as lower value. But the indicator's object price1 is lower value and price2 is upper value. So it caused my trade conditions cannot be met.

Thanks admin for your time to look into my issue. :)
 
  • 🎉
Reactions: Enivid

Farhad@@

Trader
Mar 5, 2024
1
0
6
27
Sir in H4 i says no resistance found.. WHat to do in that case ?. and is i to be used only in buy positions or sell too ?
Post automatically merged:

Sir is this to be used only for buying entries ? or sell to
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,301
1,529
144
Odesa
www.earnforex.com
Sir in H4 i says no resistance found.. WHat to do in that case ?. and is i to be used only in buy positions or sell too ?
Post automatically merged:

Sir is this to be used only for buying entries ? or sell to
This means that it couldn't find any recent resistances (within 1000 bars by default). This can happen in major uptrends. You can either increase that parameter or switch to a different timeframe.
 

jypax

Trader
May 21, 2024
2
0
6
22
hello, i'm not too good at programming but i just try. i made an AE but i want to include Support and resistance detection just this indicator tou provided. can anyone help me to do that? your help would be very appriciated
 

fargana

Active Trader
Nov 14, 2022
228
31
39
34
I have an EA that can open an order when the price is closed within a rectangle object (support or resistance area) which I have drawn on chart.

But the EA is not working with the support and resistance zone of this indicator. As I know, the zone is actually a rectangle object, right?

Any idea how can I go about it so the EA can work with this indicator?
Can you specify which EA and indicator are you talking about? Setting them to work in conjunction is similar but still there are some differences from EA to EA
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,301
1,529
144
Odesa
www.earnforex.com
hello, i'm not too good at programming but i just try. i made an AE but i want to include Support and resistance detection just this indicator tou provided. can anyone help me to do that? your help would be very appriciated
Please share your EA here. What have you already tried to add this S&R indicator there? What's your current problem?
 

jypax

Trader
May 21, 2024
2
0
6
22
i just wanted to combine signals from that indicator but i failed. can you please provide the snippet code of how i can get and retrieve data from the indicator?
Post automatically merged:

MQL5:
// Include the necessary libraries and indicators
#include <Trade\Trade.mqh>
#include <Indicators\Indicators.mqh>
 
// Define the input parameters
input int EntrySignalTF = 15; // Signal detection timeframe (in minutes)
input int ConfirmationTF1 = 30; // Confirmation timeframe 1 (in minutes)
input int ConfirmationTF2 = 60; // Confirmation timeframe 2 (in minutes)
input int TradeExecutionTF = 5; // Trade execution timeframe (in minutes)
input double StopLoss = 50.0; // Stop loss (in pips)
input double TakeProfit = 100.0; // Take profit (in pips)
 
// Declare global variables
CTrade trade; // Trade object
CIndicators indicators; // Indicator object
 
// Define object names for drawing lines
string buyLineObjectName = "BuyLine";
string sellLineObjectName = "SellLine";
 
// Define the ENUM_DIRECTION type
enum ENUM_DIRECTION
{
    DIRECTION_NONE,  // No specific direction
    DIRECTION_UP,    // Upward direction
    DIRECTION_DOWN   // Downward direction
};
 
void OnInit()
{
    trade.SetExpertMagicNumber(123456);  // Set a unique magic number for the expert advisor
    }
// Define the OnTick() function
void OnTick()
{
    // Check if the current bar is complete for the entry signal timeframe
    if (TimeCurrent() % (EntrySignalTF * 60) != 0)
        return;
 
    // Calculate the entry signal indicators on M15 timeframe
    double bollingerUpper = iBands(Symbol(), PERIOD_M15, 20, 0, 2.0, PRICE_CLOSE);
    double bollingerLower = iBands(Symbol(), PERIOD_M15, 20, 2, -2.0, PRICE_CLOSE);
    double rsiValue = iRSI(Symbol(), PERIOD_M15, 14, PRICE_CLOSE);
    double stochasticValue = iStochastic(Symbol(), PERIOD_M15, 5, 3, 3, MODE_SMA, STO_LOWHIGH);
    double macdHistogram = iMACD(Symbol(), PERIOD_M15, 12, 26, 9, PRICE_CLOSE);
    double rejectionTop = iHigh(Symbol(), PERIOD_M15, 1) - bollingerUpper; // Rejection from upper Bollinger Band
    double rejectionBottom = bollingerLower - iLow(Symbol(), PERIOD_M15, 1); // Rejection from lower Bollinger Band
 
    // Calculate the trend direction on M30 timeframe
    ENUM_DIRECTION trendM30 = GetTrendDirection(PERIOD_M30);
 
    // Calculate the trend direction on H1 timeframe
    ENUM_DIRECTION trendH1 = GetTrendDirection(PERIOD_H1);
 
    // Check buy entry conditions on M15 timeframe
    if (stochasticValue < 20 && rsiValue < 30 && macdHistogram < 0 && rejectionBottom > 0 && trendM30 == DIRECTION_UP && trendH1 == DIRECTION_UP)
    {
        // Check buy confirmation conditions on M5 timeframe
        bool isBuyConfirmed = CheckConfirmationConditions(PERIOD_M5, bollingerLower);
 
        if (isBuyConfirmed)
        {
            // Open a buy trade
            trade.Buy(0.01, Symbol(), 0, 0, StopLoss, TakeProfit);
 
            // Draw a line to mark the anticipated reversal point on M15 timeframe
            double reversalPrice = NormalizeDouble(bollingerLower, _Digits);
            DrawLine(buyLineObjectName, reversalPrice);
        }
    }
 
    // Check sell entry conditions on M15 timeframe
    if (stochasticValue > 80 && rsiValue > 70 && macdHistogram > 0 && rejectionTop > 0 && trendM30 == DIRECTION_DOWN && trendH1 == DIRECTION_DOWN)
    {
        // Check sell confirmation conditions on M5 timeframe
        bool isSellConfirmed = CheckConfirmationConditions(PERIOD_M5, bollingerUpper);
 
        if (isSellConfirmed)
        {
            // Open a sell trade
            trade.Sell(0.01, Symbol(), 0, 0, StopLoss,TakeProfit);
 
            // Draw a line to mark the anticipated reversal point on M15 timeframe
            double reversalPrice = NormalizeDouble(bollingerUpper, _Digits);
            DrawLine(sellLineObjectName, reversalPrice);
        }
    }
}
 
// Function to check confirmation conditions on a specified timeframe
bool CheckConfirmationConditions(ENUM_TIMEFRAMES timeframe, double bollingerLevel)
{
    double confirmationBollinger = iBands(Symbol(), timeframe, 20, 2, 0, PRICE_CLOSE);
    double confirmationClose = iClose(Symbol(), timeframe, 1);
 
    if (confirmationClose < confirmationBollinger)
    {
        // Check additional confirmationconditions on the specified timeframe, such as other indicators or price patterns.
        return true;
    }
 
    return false;
}
 
// Function to get the trend direction on a specified timeframe
ENUM_DIRECTION GetTrendDirection(ENUM_TIMEFRAMES timeframe)
{
    double maFast = iMA(Symbol(), timeframe, 50, 0, MODE_EMA, PRICE_CLOSE);
    double maSlow = iMA(Symbol(), timeframe, 200, 0, MODE_EMA, PRICE_CLOSE);
 
    if (maFast > maSlow)
    {
        return DIRECTION_UP;
    }
    else if (maFast < maSlow)
    {
        return DIRECTION_DOWN;
    }
    else
    {
        return DIRECTION_NONE;
    }
}
 
// Function to draw a line on the chart
void DrawLine(string objectName, double price)
{
    ObjectDelete(0, objectName);
    ObjectCreate(0, objectName, OBJ_HLINE, 0, 0, price);
    ObjectSetInteger(0, objectName, OBJPROP_COLOR, clrRed);
    ObjectSetInteger(0, objectName, OBJPROP_STYLE, STYLE_SOLID);
}
 
// Define the OnDeinit() function
void OnDeinit(const int reason)
{
    // Close any open trades
    //trade.CloseAll();
 
    // Delete the drawn lines
    ObjectDelete(0, buyLineObjectName);
    ObjectDelete(0, sellLineObjectName);
}
 

Attachments

  • y.mq5
    4.5 KB · Views: 0
Last edited by a moderator:

Enivid

Administrator
Staff member
Nov 30, 2008
19,301
1,529
144
Odesa
www.earnforex.com
i just wanted to combine signals from that indicator but i failed. can you please provide the snippet code of how i can get and retrieve data from the indicator?
Post automatically merged:

MQL5:
// Include the necessary libraries and indicators
#include <Trade\Trade.mqh>
#include <Indicators\Indicators.mqh>
 
// Define the input parameters
input int EntrySignalTF = 15; // Signal detection timeframe (in minutes)
input int ConfirmationTF1 = 30; // Confirmation timeframe 1 (in minutes)
input int ConfirmationTF2 = 60; // Confirmation timeframe 2 (in minutes)
input int TradeExecutionTF = 5; // Trade execution timeframe (in minutes)
input double StopLoss = 50.0; // Stop loss (in pips)
input double TakeProfit = 100.0; // Take profit (in pips)
 
// Declare global variables
CTrade trade; // Trade object
CIndicators indicators; // Indicator object
 
// Define object names for drawing lines
string buyLineObjectName = "BuyLine";
string sellLineObjectName = "SellLine";
 
// Define the ENUM_DIRECTION type
enum ENUM_DIRECTION
{
    DIRECTION_NONE,  // No specific direction
    DIRECTION_UP,    // Upward direction
    DIRECTION_DOWN   // Downward direction
};
 
void OnInit()
{
    trade.SetExpertMagicNumber(123456);  // Set a unique magic number for the expert advisor
    }
// Define the OnTick() function
void OnTick()
{
    // Check if the current bar is complete for the entry signal timeframe
    if (TimeCurrent() % (EntrySignalTF * 60) != 0)
        return;
 
    // Calculate the entry signal indicators on M15 timeframe
    double bollingerUpper = iBands(Symbol(), PERIOD_M15, 20, 0, 2.0, PRICE_CLOSE);
    double bollingerLower = iBands(Symbol(), PERIOD_M15, 20, 2, -2.0, PRICE_CLOSE);
    double rsiValue = iRSI(Symbol(), PERIOD_M15, 14, PRICE_CLOSE);
    double stochasticValue = iStochastic(Symbol(), PERIOD_M15, 5, 3, 3, MODE_SMA, STO_LOWHIGH);
    double macdHistogram = iMACD(Symbol(), PERIOD_M15, 12, 26, 9, PRICE_CLOSE);
    double rejectionTop = iHigh(Symbol(), PERIOD_M15, 1) - bollingerUpper; // Rejection from upper Bollinger Band
    double rejectionBottom = bollingerLower - iLow(Symbol(), PERIOD_M15, 1); // Rejection from lower Bollinger Band
 
    // Calculate the trend direction on M30 timeframe
    ENUM_DIRECTION trendM30 = GetTrendDirection(PERIOD_M30);
 
    // Calculate the trend direction on H1 timeframe
    ENUM_DIRECTION trendH1 = GetTrendDirection(PERIOD_H1);
 
    // Check buy entry conditions on M15 timeframe
    if (stochasticValue < 20 && rsiValue < 30 && macdHistogram < 0 && rejectionBottom > 0 && trendM30 == DIRECTION_UP && trendH1 == DIRECTION_UP)
    {
        // Check buy confirmation conditions on M5 timeframe
        bool isBuyConfirmed = CheckConfirmationConditions(PERIOD_M5, bollingerLower);
 
        if (isBuyConfirmed)
        {
            // Open a buy trade
            trade.Buy(0.01, Symbol(), 0, 0, StopLoss, TakeProfit);
 
            // Draw a line to mark the anticipated reversal point on M15 timeframe
            double reversalPrice = NormalizeDouble(bollingerLower, _Digits);
            DrawLine(buyLineObjectName, reversalPrice);
        }
    }
 
    // Check sell entry conditions on M15 timeframe
    if (stochasticValue > 80 && rsiValue > 70 && macdHistogram > 0 && rejectionTop > 0 && trendM30 == DIRECTION_DOWN && trendH1 == DIRECTION_DOWN)
    {
        // Check sell confirmation conditions on M5 timeframe
        bool isSellConfirmed = CheckConfirmationConditions(PERIOD_M5, bollingerUpper);
 
        if (isSellConfirmed)
        {
            // Open a sell trade
            trade.Sell(0.01, Symbol(), 0, 0, StopLoss,TakeProfit);
 
            // Draw a line to mark the anticipated reversal point on M15 timeframe
            double reversalPrice = NormalizeDouble(bollingerUpper, _Digits);
            DrawLine(sellLineObjectName, reversalPrice);
        }
    }
}
 
// Function to check confirmation conditions on a specified timeframe
bool CheckConfirmationConditions(ENUM_TIMEFRAMES timeframe, double bollingerLevel)
{
    double confirmationBollinger = iBands(Symbol(), timeframe, 20, 2, 0, PRICE_CLOSE);
    double confirmationClose = iClose(Symbol(), timeframe, 1);
 
    if (confirmationClose < confirmationBollinger)
    {
        // Check additional confirmationconditions on the specified timeframe, such as other indicators or price patterns.
        return true;
    }
 
    return false;
}
 
// Function to get the trend direction on a specified timeframe
ENUM_DIRECTION GetTrendDirection(ENUM_TIMEFRAMES timeframe)
{
    double maFast = iMA(Symbol(), timeframe, 50, 0, MODE_EMA, PRICE_CLOSE);
    double maSlow = iMA(Symbol(), timeframe, 200, 0, MODE_EMA, PRICE_CLOSE);
 
    if (maFast > maSlow)
    {
        return DIRECTION_UP;
    }
    else if (maFast < maSlow)
    {
        return DIRECTION_DOWN;
    }
    else
    {
        return DIRECTION_NONE;
    }
}
 
// Function to draw a line on the chart
void DrawLine(string objectName, double price)
{
    ObjectDelete(0, objectName);
    ObjectCreate(0, objectName, OBJ_HLINE, 0, 0, price);
    ObjectSetInteger(0, objectName, OBJPROP_COLOR, clrRed);
    ObjectSetInteger(0, objectName, OBJPROP_STYLE, STYLE_SOLID);
}
 
// Define the OnDeinit() function
void OnDeinit(const int reason)
{
    // Close any open trades
    //trade.CloseAll();
 
    // Delete the drawn lines
    ObjectDelete(0, buyLineObjectName);
    ObjectDelete(0, sellLineObjectName);
}
Please use code highlighting when attaching code snippets.

You can add a custom indicator using the iCustom() function. However, from what I see in the code, it looks like it has been converted from MT4 but without doing all the necessary steps. However, this would be outside of this topic's scope to discuss basic MT4->MT5 conversion.
 
  • 👍
Reactions: Jajaofopobo

Jajaofopobo

Trader
Sep 6, 2021
72
7
24
Yes, this indicator will repaint. It only shows the support and resistance zones based on the information available at the moment.
a replay option would be nice, It will help in backtesting.. saw an old demand and supply indie with one
 
Last edited:

Jajaofopobo

Trader
Sep 6, 2021
72
7
24
what does the recent candles to ignore mean? if i put 1000, does it ignore the 1000 bars and make its calculation .....can that be used for backtesting? dont get
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,301
1,529
144
Odesa
www.earnforex.com
what does the recent candles to ignore mean? if i put 1000, does it ignore the 1000 bars and make its calculation .....can that be used for backtesting? dont get
It won't use the values from the most recent N candles to calculate the S/R levels. Yes, you can use that to backtest, but you have to be accurate in that parameter's adjustments to match the current period you are looking at when backtesting.