Search code examples
mql4

How create static trendline object on chart by MQL4?


I want a down trend line whenever the highest candle in the previous 500 bars is higher than latest 5 candles highest bar. However the trend line is created the first time but does not update.

int highestCandle=iHighest(Symbol(),0,MODE_HIGH,500,0);
if(High[highestCandle]>High[5])
{
ObjectCreate(0,"HT",OBJ_TREND,0,Time[highestCandle],High[highestCandle],Time[0],High[0]);
ObjectSetInteger(0,"HT",OBJPROP_COLOR,clrBlue); ObjectSetInteger(0,"HT",OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,"HT",OBJPROP_RAY,true);
}

I do not want to delete this object and recreate on every new tick, I would just like it to be updated on the chart. enter image description here


Solution

  • Because you have already created your object, trying to create it again will not move the coordinates. You need to use ObjectMove. Try the following code:

    int highestCandle=iHighest(Symbol(),0,MODE_HIGH,500,0);
    if(High[highestCandle]>High[5])
    {
       ObjectCreate(0,"HT",OBJ_TREND,0,Time[highestCandle],High[highestCandle],Time[0],High[0]);
       ObjectSetInteger(0,"HT",OBJPROP_COLOR,clrBlue);
       ObjectSetInteger(0,"HT",OBJPROP_STYLE,STYLE_SOLID); 
       ObjectSetInteger(0,"HT",OBJPROP_RAY,true);
       ObjectMove("HT",0,Time[highestCandle],High[highestCandle]);
       ObjectMove("HT",1,Time[0],High[0]);
    }