Problems with Vertical lines indicator

 
Hello, I have indicator which plots vertical line (thanks jellybean for help). I want this indicator to plot vertical lines on regular basis (for example, every day at 12:30 GMT - USA news). My programing language know are quite weak... Could You help me ? Thanks, Edward
Files:
 

Hi Edward


1. You need to construct the time parameter for each VLINE. There is no access to GMT so you need to construct it. There are several ways to do it, but I suggest an input for the number of hours after the server day-change time (00:00) where you want your lines to be, then add it to the time of the daily bar. For example,

int Hours = 12.5;
datetime LineTime;
LineTime = iTime( NULL, PERIOD_D1, 0 ) + Hours*3600;     // because datetime variables are in seconds

2. Because you will be adding an increasing number of lines to your chart, your deinit() will need to delete an unknown number of them. However, you probably don't want to use ObjectsDeleteAll() because it will delete things you want. One technique for achieving this is to make your object names have a common prefix that is specific to this indicator and add a variable part to it so you don't have the same name for all of them. You can then use that prefix to select the ones for deletion. For example, create your object names like this...

LineName = "Vertical Line " + DoubleToStr( LineTime, 0 );

Then you can use this in your deinit() to delete them.

for(int i = ObjectsTotal() - 1; i >= 0; i--)
{
    LineName = ObjectName(i);
    if( StringSubstr( LineName, 0, 13 ) == "Vertical Line" )
       ObjectDelete( LineName );   
}

3. You will need some logic to draw the line when the correct time passes. You can test the current time against LineTime above.

Cheers

Jellybean

 
Yes, I did it :) Now indicator plots Vertical lines on regular periods (period changeable). How could I make indicator to draw lines only on some timeframe, for example PERIOD_M1, PERIOD_M5, PERIOD_M15 ? And maybe to change value of (Hour) if timeframe will be higher period ? Thanks, Edward
Files:
 

I just want to give you a quick tip. when deleting you lines, you may miss some using the loop as given, because after you delete, the ObjectsTotal will change and you can actually skip over an object, so do something like this instead:


int objTotal = ObjectsTotal();
int i=0;

while(i<objTotal)
{
LineName = ObjectName(i);

    if( StringSubstr( LineName, 0, 5 ) == "VLine" )
ObjectDelete( LineName );
else i++;
}

I didn't test , but the idea is don't increment or decrement i if we deleted an object.


 
edas:
Yes, I did it :) Now indicator plots Vertical lines on regular periods (period changeable). How could I make indicator to draw lines only on some timeframe, for example PERIOD_M1, PERIOD_M5, PERIOD_M15 ? And maybe to change value of (Hour) if timeframe will be higher period ? Thanks, Edward

Hi Edward

It is an indicator so you can set it to show only in certain timeframes when you attach it to a chart. When you attach it to a chart you will see the Cutom Indicator - VLine... window appear. Click the Visualization tab and uncheck the "All Timeframes" box. You can then select the timeframes that you want the indicator to appear.

The function Period() allows you to determine what timeframe the chart has, so you can use it to change the indicator's behavior.

For example, you can put your whole for() loop inside an if( Period() <=15 ) {} statement to cause the lines to show only on certain timeframes.

Also, you can use the same function and assign values to Hours dependent on the timeframe.


One final comment. I added the variable "Unused" to your code to make the point that the 5th parameter in the ObjectCreate() function is not used. It's simpler and easier to read if you remove the variable and just make the 5th parameter 0.


Cheers

Jellybean

 
circlesquares:

I just want to give you a quick tip. when deleting you lines, you may miss some using the loop as given, because after you delete, the ObjectsTotal will change and you can actually skip over an object, so do something like this instead:


Well noted. Thanks.

Jellybean