Download MetaTrader 5

Trendline break indicator showing OHLC break since manually created trendline object has been created

To add comments, please log in or register
Join MQL5 Cloud Network. Start earning money!
ZenTrader
52
ZenTrader 2014.05.26 00:54 

Hi everyone.

Im trying to figure out how I can detect if a manually drawn trendline has been broken with a OHLC since its creation.

ffoorr
1016
ffoorr 2014.05.26 08:27  
ObjectGetValueByShift
ZenTrader
52
ZenTrader 2014.05.26 15:07  

Thanks ffoorr

 Im working on a combination of ibarshift and ObjectGetValueByShift 

ZenTrader
52
ZenTrader 2014.05.26 17:15  

I have run into a brickwall.

On each chart (symbol) I might be drawing manual trendlines on 3 or more timeframes. The ObjectGetValueByShift on works on the current timeframe that the chart is in (I think). 

ZenTrader
52
ZenTrader 2014.05.26 23:30  

i think i have got it but its hard to get a match to the visual 

ZenTrader
52
ZenTrader 2014.09.22 14:13  

My solution. It works really well.

ZenTrader
52
ZenTrader 2014.09.22 14:14  

My solution, enjoy and let me know if it can be improved upon. 

 

//+------------------------------------------------------------------+
//|                                               TrendlineBreak.mqh |
//|                                              Christopher Barklem |
//|                                            http://www.barklem.es |
//+------------------------------------------------------------------+
#property copyright "Christopher Barklem"
#property link      "http: //www.barklem.es"
#property strict

extern bool Include_Internal_Break_Detection = true; // This option allows you to either detect breaks past time2 or the whole line.
extern bool Detect_OHLC_Break_Of_Trendline   = true; // This option allows you to limit the break to be at least one candles open, high low and close across the line.
extern bool Detect_Close_Break_Of_Trendline  = true; // This option allows you to limit the break to a close past the line.
extern bool Detect_Price_Break_Of_Trendline  = true; // This option allows you to limit the break to any price action past the line.

//+------------------------------------------------------------------+
//| Detect is price has created an OHLC break of a trendline         |
//+------------------------------------------------------------------+
bool CheckTrendlineBreak(string obj_name)
{
    bool     trendline_broken = false;
    string   trend_direction;

    double   obj_price1     = ObjectGet(obj_name, OBJPROP_PRICE1);
    double   obj_price2     = ObjectGet(obj_name, OBJPROP_PRICE2);
    datetime obj_time1      = ObjectGet(obj_name, OBJPROP_TIME1);
    datetime obj_time2      = ObjectGet(obj_name, OBJPROP_TIME2);
    int      obj_timeframes = ObjectGet(obj_name, OBJPROP_TIMEFRAMES);

    // Correct trendline drawing. Trendline should be drawn right to left.

    datetime temp_time1, temp_time2;
    if (obj_time1 > obj_time2)
    {
        temp_time1 = obj_time2;
        temp_time2 = obj_time1;
        obj_time1  = temp_time1;
        obj_time2  = temp_time2;
    }

    // Assume manual uptrend or downtrend
    if (obj_price1 > obj_price2)
    {
        trend_direction = "DOWN";
    }    // DOWN
    else if (obj_price2 > obj_price1)
    {
        trend_direction = "UP";
    }   // UP
    else if (obj_price2 == obj_price1)
    {
        trend_direction = "HORIZONTAL"; return false;
    }    // No trend to check for break.

    int    objectTimeFrame = visToPeriod(obj_timeframes);

    double tslope, tp3, ClosePrice, OpenPrice, HighPrice, LowPrice;
    int    candleAbove, candleBelow;

    int    tp1shift = iBarShift(NULL, objectTimeFrame, obj_time1, true);
    int    tp2shift = iBarShift(NULL, objectTimeFrame, obj_time2, true);

    int    shiftcheck;

    if (Include_Internal_Break_Detection == true)
    {
        shiftcheck = tp1shift;
    }
    else
    {
        shiftcheck = tp2shift;
    }

    if (trend_direction == "UP" && tp1shift - tp2shift > 0)
    {
        tslope      = (obj_price2 - obj_price1) / (tp1shift - tp2shift);
        candleBelow = 0; candleBelow = 0;

        for (int ishift = shiftcheck - 1; ishift > 0; ishift--)
        {
            ClosePrice = iClose(NULL, objectTimeFrame, ishift);
            OpenPrice  = iOpen(NULL, objectTimeFrame, ishift);
            HighPrice  = iHigh(NULL, objectTimeFrame, ishift);
            LowPrice   = iLow(NULL, objectTimeFrame, ishift);

            if (ishift > tp2shift)
            {
                tp3 = (tslope * (tp1shift - ishift)) + obj_price1;
            }
            else if (ishift == tp2shift)
            {
                tp3 = obj_trendline_array[ID].price2;
            }
            else
            {
                tp3 = (tslope * (tp2shift - ishift)) + obj_.price2;
            }

            if (Detect_OHLC_Break_Of_Trendline)
            {
                if (ClosePrice > tp3 && OpenPrice > tp3 && HighPrice > tp3 && LowPrice > tp3)
                {
                    candleAbove++;
                }
                if (ClosePrice < tp3 && OpenPrice < tp3 && HighPrice < tp3 && LowPrice < tp3)
                {
                    candleBelow++;
                }
            }
            if (Detect_Close_Break_Of_Trendline)
            {
                if (ClosePrice > tp3)
                {
                    candleAbove++;
                }
                if (ClosePrice < tp3)
                {
                    candleBelow++;
                }
            }
            if (Detect_OHLC_Break_Of_Trendline)
            {
                if (ClosePrice > tp3 || OpenPrice > tp3 || HighPrice > tp3 || LowPrice > tp3)
                {
                    candleAbove++;
                }
                if (ClosePrice < tp3 || OpenPrice < tp3 || HighPrice < tp3 || LowPrice < tp3)
                {
                    candleBelow++;
                }
            }
        }
        if (candleBelow > 0)
            trendline_broken = true;
        if (candleBelow == 0)
            trendline_broken = false;
    }

    if (trend_direction == "DOWN" && tp1shift - tp2shift > 0)
    {
        tslope      = (obj_.price1 - obj_price2) / (tp1shift - tp2shift);
        candleBelow = 0; candleBelow = 0;

        for (int ishift = shiftcheck - 1; ishift > 0; ishift--)
        {
            ClosePrice = iClose(NULL, objectTimeFrame, ishift);
            OpenPrice  = iOpen(NULL, objectTimeFrame, ishift);
            HighPrice  = iHigh(NULL, objectTimeFrame, ishift);
            LowPrice   = iLow(NULL, objectTimeFrame, ishift);

            // tp3 = tp2-((tp2shift-ishift)*tslope);
            if (ishift > tp2shift)
            {
                tp3 = obj_price1 - (tslope * (tp1shift - ishift));
            }
            else if (ishift == tp2shift)
            {
                tp3 = obj_price2;
            }
            else
            {
                tp3 = obj_price2 - (tslope * (tp2shift - ishift));
            }

            if (Detect_OHLC_Break_Of_Trendline)
            {
                if (ClosePrice > tp3 && OpenPrice > tp3 && HighPrice > tp3 && LowPrice > tp3)
                {
                    candleAbove++;
                }
                if (ClosePrice < tp3 && OpenPrice < tp3 && HighPrice < tp3 && LowPrice < tp3)
                {
                    candleBelow++;
                }
            }
            if (Detect_Close_Break_Of_Trendline)
            {
                if (ClosePrice > tp3)
                {
                    candleAbove++;
                }
                if (ClosePrice < tp3)
                {
                    candleBelow++;
                }
            }
            if (Detect_OHLC_Break_Of_Trendline)
            {
                if (ClosePrice > tp3 || OpenPrice > tp3 || HighPrice > tp3 || LowPrice > tp3)
                {
                    candleAbove++;
                }
                if (ClosePrice < tp3 || OpenPrice < tp3 || HighPrice < tp3 || LowPrice < tp3)
                {
                    candleBelow++;
                }
            }
        }
        if (candleAbove > 0)
            trendline_broken = true;
        if (candleAbove == 0)
            trendline_broken = false;
    }

    return(trendline_broken);
}

// Objects return an object timeframe like OBJ_PERIOD_M5, but functions like iBarShift want the chart period timeframe like PERIOD_M5.

int visToPeriod(int objectVisTime)
{
    switch (objectVisTime)
    {
    case OBJ_PERIOD_M5:
        return(PERIOD_M5);
        break;
    case OBJ_PERIOD_M15:
        return(PERIOD_M15);
        break;
    case OBJ_PERIOD_M30:
        return(PERIOD_M30);
        break;
    case OBJ_PERIOD_H1:
        return(PERIOD_H1);
        break;
    case OBJ_PERIOD_H4:
        return(PERIOD_H4);
        break;
    case OBJ_PERIOD_D1:
        return(PERIOD_D1);
        break;
    case OBJ_PERIOD_W1:
        return(PERIOD_W1);
        break;
    case OBJ_PERIOD_MN1:
        return(PERIOD_MN1);
        break;
    }
    return(0);
}
ffoorr
1016
ffoorr 2014.09.23 07:06  

 start() is missing in your indicator.

How will you detect the line ? the name of the line is needed, so when drawing the line give it a name, so you can find the line with his name :

if( ObjectFind( name_of_the_line ) > -1 ) mean the line has been found

 don't know if lines will be found on other timeframe or chart.

double  ObjectGetValueByShift(
   string   object_name,   // object name
   int      shift          // bar index
   );


if(   (ObjectGetValueByShift( name_of_the_line, Shift )  > Low[Shift] ) &&  
      (ObjectGetValueByShift( name_of_the_line, Shift )  < High[Shift] ) 

Something like that, you need the Shift, the "i", so you need the start()

To add comments, please log in or register