Code to delete all trend lines at bar close

Questions about MultiCharts and user contributed studies.
PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Code to delete all trend lines at bar close

Postby PD Quig » 04 Feb 2015

As a small piece of a routine that draws a varying number of trend lines at the close of each bar, I want to delete all existing trend lines on the close of a bar. No matter what I try, I can only get one TL to delete. Here's what I thought would work:

Code: Select all

variables:
ID (0);

if LastBarOnChart and barstatus = 2 then begin

ID = TL_GetFirst(3); { tl of any origin }

while ID > 0 begin

TL_Delete(ID);
ID = TL_GetNext(ID, 3);
end;
end;
When I draw three manual trend lines on the chart and step through in playback mode, only a single line is deleted per bar. Print statements inserted into this snippet reveal that after the TL_GetNext statement the value of ID = -2 after each trend line is deleted. Why isn't it getting the 2nd or 3rd trendline ID?

I must be missing something obvious.

Thanks in advance.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Code to delete all trend lines at bar close

Postby Henry MultiСharts » 05 Feb 2015

Hello PD Quig,

Please try this code:

Code: Select all

variables:
ID (0);

if LastBarOnChart and barstatus = 2 then begin

once ID = tl_getfirst(3);
While TL_Exist(ID) = true begin
tl_delete(ID);
ID = tl_getfirst(3);
end;
end;

PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Re: Code to delete all trend lines at bar close

Postby PD Quig » 05 Feb 2015

Henry,

Your code works as desired when "once" is removed from the statement:

once ID = tl_getfirst(3);

Otherwise, it only works one time ;-)

Thanks!

-PDQ


Return to “MultiCharts”