How to automatic draw HLine from given VLine!!!

 

Dear folks,

A part of my custom indicator requires draw HLine with close price of the bar which is defined by current VLine. Maybe there will be about 6 VLine on the chart. And one more thing, can indicator also automatically deletes all Vline and Hline which is not be seen in the lastest 50 bars window?

Any suggestion would be much appreciated, Thanks !!!


 

You can use the V line properties to find which bar it is on then use Close or iClose to get the close price of that bar.

To clean up the old H and V lines create a function to look through all the V lines read their datetime and if older than 50 bars ago remove them and their associated H Lines.

This should help: https://docs.mql4.com/objects & https://docs.mql4.com/objects/ObjectGet & https://docs.mql4.com/constants/objects/properties for V lines you use OBJPROP_TIME1

 
RaptorUK:

You can use the V line properties to find which bar it is on then use Close or iClose to get the close price of that bar.

To clean up the old H and V lines create a function to look through all the V lines read their datetime and if older than 50 bars ago remove them and their associated H Lines.

This should help: https://docs.mql4.com/objects & https://docs.mql4.com/objects/ObjectGet & https://docs.mql4.com/constants/objects/properties for V lines you use OBJPROP_TIME1


Thank for your quick reply :-)

I tried this code but not work. Can you please take a look in it? BIG THANKS :)


int    obj_total = ObjectsTotal();
   string name;
   for(int i = 0; i < obj_total; i++)
     {
       name = ObjectName(i);
       if(ObjectType(name) == OBJ_VLINE)
         {
       datetime time1=ObjectGet(name, OBJPROP_TIME1);
       int shift=ObjectGetShiftByValue(name, time1);
  ObjectCreate("hline", OBJ_HLINE, 0, 0, iClose(NULL,0,shift));
  ObjectSet("hline", OBJPROP_COLOR, Blue);
  ObjectSet("hline", OBJPROP_STYLE, STYLE_SOLID);
  ObjectSet("hline", OBJPROP_WIDTH, 2);
         }
         }
 

OK, you aren't far off, well done . . . try this:

int shift=ObjectGetShiftByValue(name, time1);

//  this instead

int shift = iBarShift(NULL, 0, ObjectGet(name, OBJPROP_TIME1));
 
RaptorUK:

OK, you aren't far off, well done . . . try this:


It worked so nicely but seems only with ONE VLine. Is there any way for it to draw all existing VLine, RaptorUK?
 
InvestDad:

It worked so nicely but seems only with ONE VLine. Is there any way for it to draw all existing VLine, RaptorUK?

Yes, you need to change the name for the other H lines . . . I would suggest you maybe use the name of the V Line and add a "h" before it . .

string hlinename = StringConcatenate("h", name); 

then use hlinename instead of "hline"

 
RaptorUK:

Yes, you need to change the name for the other H lines . . . I would suggest you maybe use the name of the V Line and add a "h" before it . .

then use hlinename instead of "hline"


Hello RaptorUK, I don't quite understand your idea. It seems only draw ONE HLine for the first drawed VLine but not all of existing VLines. I think if I use

datetime time1=ObjectGet(name, OBJPROP_TIME1);

Maximum number of VLines would be 3 only. Please be more specific on your ideas. Thanks ./.

 
InvestDad:


Hello RaptorUK, I don't quite understand your idea. It seems only draw ONE HLine for the first drawed VLine but not all of existing VLines. I think if I use

Maximum number of VLines would be 3 only. Please be more specific on your ideas. Thanks ./.

OK, your code . . .

 ObjectCreate("hline", OBJ_HLINE, 0, 0, iClose(NULL,0,shift));

. . . creates a OBJ_HLINE Object called "hline". It draws this at the Close of the bar where the first V line is . . . . then for the next Vline it creates a OBJ_HLINE Object called . . . . "hline" . . . then for the next Vline it creates a OBJ_HLINE Object called . . . . "hline" . . . . . then for the next Vline it creates a OBJ_HLINE Object called . . . . "hline"

Do you see the problem . . . . ? hence . . . "Yes, you need to change the name for the other H lines"

 
RaptorUK:

OK, your code . . .

. . . creates a OBJ_HLINE Object called "hline". It draws this at the Close of the bar where the first V line is . . . . then for the next Vline it creates a OBJ_HLINE Object called . . . . "hline" . . . then for the next Vline it creates a OBJ_HLINE Object called . . . . "hline" . . . . . then for the next Vline it creates a OBJ_HLINE Object called . . . . "hline"

Do you see the problem . . . . ? hence . . . "Yes, you need to change the name for the other H lines"


So nice RaptorUK. I figured it out at last :)

Here is the working code:

datetime time1=ObjectGet(name, OBJPROP_TIME1);
         int shift = iBarShift(NULL, 0, ObjectGet(name, OBJPROP_TIME1));
         string hlinename = StringConcatenate("h", name); 
         ObjectCreate(hlinename, OBJ_HLINE, 0, 0, iClose(NULL,0,shift));
         ObjectSet(hlinename, OBJPROP_COLOR, Red);
         ObjectSet(hlinename, OBJPROP_STYLE, STYLE_SOLID);
         ObjectSet(hlinename, OBJPROP_WIDTH, 0);

Btw, still one last problem. Is there a way I can definite all VLines which are 50 hourly bars behind from current bar? I don't know how to use datetime calculations so pls help !!!

BIG THANKS SO FAR :)

 
InvestDad:


Btw, still one last problem. Is there a way I can definite all VLines which are 50 hourly bars behind from current bar? I don't know how to use datetime calculations so pls help !!!

BIG THANKS SO FAR :)

You are most welcome.

To address the delete V and H lines that are past 50 hours . . . you can add the code into your loop . . you already have the Barshift for the V line (int shift = iBarShift(NULL, 0, ObjectGet(name, OBJPROP_TIME1));) so if this is greater than 50 delete the object called name to remove the V line and the one called StringConcatenate("h", name) to get rid of the corresponding H line . . .

Have a go, if you need specific help ask . . :-) I'll be here.

 
RaptorUK:

You are most welcome.

To address the delete V and H lines that are past 50 hours . . . you can add the code into your loop . . you already have the Barshift for the V line (int shift = iBarShift(NULL, 0, ObjectGet(name, OBJPROP_TIME1));) so if this is greater than 50 delete the object called name to remove the V line and the one called StringConcatenate("h", name) to get rid of the corresponding H line . . .

Have a go, if you need specific help ask . . :-) I'll be here.


Yup.. Still one more problem please :)

The current corresponding VLines and HLines, I need to definite the highest price HLine and the lowest one. From this, I must know "shift value" of the bar which has highest price HLine. How do you come up with a solution for this comparison problem? :P

Reason: