Allocate dedicated reserved word for horizontal lines

Questions about MultiCharts and user contributed studies.
seneca
Posts: 87
Joined: 02 Apr 2012
Has thanked: 36 times
Been thanked: 22 times

Allocate dedicated reserved word for horizontal lines

Postby seneca » 08 Oct 2012

Hello there,

since I want to automatically calculate and display the lock limits defined for some futures contracts (e.g. commodity futures at CME), I use "tl_new" to calculate and display those lock limits (generic example):

Code: Select all

input:
lock_limit_up(10),
lock_limit_down(-10),
settlement_time(1430);

var:
settlement_price(0),
id.tl.c_up_1(-1),
id.tl.c_up_2(-1),
id.tl.c_dn_1(-1),
id.tl.c_dn_2(-1);

if time=settlement_time then begin
   settlement_price=close;
end;


if time > sessionstarttime(0,1) then begin
   id.tl.c_up_1 = tl_new(date, sessionstarttime(0,1), settlement_price+lock_limit_up, date,
   2355, settlement_price+lock_limit_up);
   
   id.tl.c_dn_1 = tl_new(date, sessionstarttime(0,1), settlement_price+lock_limit_down, date,
   2355, settlement_price+lock_limit_down);

   tl_setcolor(id.tl.c_up_1,blue);
   tl_setcolor(id.tl.c_dn_1,yellow);
   tl_setstyle(id.tl.c_up_1,5);
   tl_setstyle(id.tl.c_dn_1,5);

   end;


if time < settlement_time then begin
   id.tl.c_up_2 = tl_new(date, 0005, settlement_price+lock_limit_up, date,
   sessionendtime(0,1), settlement_price+lock_limit_up);
   
   id.tl.c_dn_2 = tl_new(date, 0005, settlement_price+lock_limit_down, date,
   sessionendtime(0,1), settlement_price+lock_limit_down);

   tl_setcolor(id.tl.c_up_2,blue);
   tl_setcolor(id.tl.c_dn_2,yellow);
   tl_setstyle(id.tl.c_up_2,5);
   tl_setstyle(id.tl.c_dn_2,5);   
   
   end;



In this generic code, the lock limit is defined based on the daily settlement price.
Unfortunately, executing this codes results in a noticeable slow down of my system - and this just to plot some horizontal lines! Computing power shouldn't be the issue (System spec: Quadcore @ 4,5 Ghz, 16 GB RAM, Windows 7 64bit).

Obviously, the trendline calculation consumes a lot of computing power, even if it is utilized to calculate just a horizontal "trend" line.

Wouldn't it be worth thinking about the introduction of a dedicated reserved word for just drawing lines (horizontal, vertical, ....)?

Or is the example provided just badly coded? If so, any suggestions on how to improve it are appreciated.

bowlesj3
Posts: 1975
Joined: 21 Jul 2007
Has thanked: 153 times
Been thanked: 395 times

Re: Allocate dedicated reserved word for horizontal lines

Postby bowlesj3 » 08 Oct 2012

This code should not slow your system down. There is hardly anything to it and I do not see any loop. However, I am trying to figure out if you are drawing many lines over top of each other. I am thinking you might be. In this case put a count in with the reserve word "intrabarpersist" and only execute on count=1 of course. or better yet move the line as mentioned below.

If you are putting a lot of lines out and deleting some and putting more out, it is generally better to ceate them at currentbar=1 then hide them and move them into place when you want to see them. This also saves you having to deal with the fact that studies that run on every tick will delete and redraw (lines, arrows, text) until the barstatus=2. This saves processing power as well (but probably not noticeable).
viewtopic.php?f=1&t=10571

See the move trend line function I wrote which will save a lot of headaches when moving trend lines. It almost never fails to do a correct move.
viewtopic.php?f=5&t=7748
These users thanked the author bowlesj3 for the post (total 2):
senecaJoshM

seneca
Posts: 87
Joined: 02 Apr 2012
Has thanked: 36 times
Been thanked: 22 times

Re: Allocate dedicated reserved word for horizontal lines

Postby seneca » 10 Oct 2012

Thanks for your answer. You were totally right, the problem was that the code plotted many lines on top of each other. As suggested, I've added two counters to prevent this:

Code: Select all

input:
lock_limit_up(10),
lock_limit_down(-10),
settlement_time(1430);

var:
{intrabarpersist} count_1(0),
{intrabarpersist} count_2(0),
settlement_price(0),
id.tl.c_up_1(-1),
id.tl.c_up_2(-1),
id.tl.c_dn_1(-1),
id.tl.c_dn_2(-1);

if time=settlement_time then begin
   settlement_price=close;
   count_1=1;
   count_2=1;
end;


if time > sessionstarttime(0,1) then begin
   if count_1=1 then begin
   count_1=count_1+1;

   id.tl.c_up_1 = tl_new(date, sessionstarttime(0,1), settlement_price+lock_limit_up, date,
   2355, settlement_price+lock_limit_up);
   
   id.tl.c_dn_1 = tl_new(date, sessionstarttime(0,1), settlement_price+lock_limit_down, date,
   2355, settlement_price+lock_limit_down);

   tl_setcolor(id.tl.c_up_1,blue);
   tl_setcolor(id.tl.c_dn_1,yellow);
   tl_setstyle(id.tl.c_up_1,5);
   tl_setstyle(id.tl.c_dn_1,5);

   end;
end;

if time < settlement_time then begin
   if count_2=1 then begin
   count_2=count_2+1;

   id.tl.c_up_2 = tl_new(date, 0005, settlement_price+lock_limit_up, date,
   sessionendtime(0,1), settlement_price+lock_limit_up);
   
   id.tl.c_dn_2 = tl_new(date, 0005, settlement_price+lock_limit_down, date,
   sessionendtime(0,1), settlement_price+lock_limit_down);

   tl_setcolor(id.tl.c_up_2,blue);
   tl_setcolor(id.tl.c_dn_2,yellow);
   tl_setstyle(id.tl.c_up_2,5);
   tl_setstyle(id.tl.c_dn_2,5);   
   
   end;
end;


With these additional counters, the code is by far less ressource demanding.
However, I do not fully understand the benefit of declaring the counter variables as "intrabarpersist"? As of now, I've put "intrabarpersist" in curly brackets, and do not notice any difference. Maybe you can provide some further insight?

bowlesj3
Posts: 1975
Joined: 21 Jul 2007
Has thanked: 153 times
Been thanked: 395 times

Re: Allocate dedicated reserved word for horizontal lines

Postby bowlesj3 » 10 Oct 2012

Hi seneca,

If your code above is not executing on every tick then IntraBarPersist is not needed. However, if you are executing any study/indicator on every tick it is very important to learn and understand Intrabarpersist as soon as possible so your code does not give you major headaches down the road. Here are the links to study.

Test Studies to help Learn IntraBarPersist
viewtopic.php?f=5&t=6871

Simple variables and the intrabarpersist
See post #2
viewtopic.php?f=5&t=6871

You should also be aware of the potential "Hidden Recalculate".
viewtopic.php?f=5&t=7362
Related to the Hidden Recalculate is a recalculate command and also RecalcPersist
viewtopic.php?f=5&t=10371

Note: these are all mentioned in the FAQ thread.
"MC/EL frequently triggered traps (how to avoid)"
viewtopic.php?f=16&t=7665
These users thanked the author bowlesj3 for the post:
JoshM

User avatar
TJ
Posts: 6518
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 957 times
Been thanked: 1883 times

Re: Allocate dedicated reserved word for horizontal lines

Postby TJ » 10 Oct 2012

seneca wrote:Hello there,

since I want to automatically calculate and display the lock limits defined for some futures contracts (e.g. commodity futures at CME), I use "tl_new" to calculate and display those lock limits (generic example):
...

Or is the example provided just badly coded? If so, any suggestions on how to improve it are appreciated.

For your purpose, it would be easier if you use PLOT.


Return to “MultiCharts”