Bar duration / seconds between bars

Questions about MultiCharts and user contributed studies.
sts
Posts: 4
Joined: 12 Mar 2012
Has thanked: 3 times

Bar duration / seconds between bars

Postby sts » 19 Mar 2012

I need to calculate the duration of the bars on a "volume bars" chart.
I tried to use the "time_s" function for that purpose:

Code: Select all

LastDurationSeconds= (time_s - time_s[1]);
But there is an obstacle the market is opened at 10:00 and closed at 18:30.
So when the market is opened in the morning I need to re-start calculating the duration.
Otherwise the indicator (depending on my two very novice codes) plots some strange numbers:
It either plots a huge number (18:30 to 10:00 becomes the 1st bar's duration)
Or it posts some negative value... which is funny...
Can someone help me with this?

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

Re: Bar duration / seconds between bars

Postby Henry MultiСharts » 20 Mar 2012

Hello Sts,

In case you need to restart the calculation on the next day you can use "Date" code word.
For example:

Code: Select all

if Date = Date[1] then
LastDurationSeconds= (time_s - time_s[1]);
This signal will be calculated if the date of the current bar is equal to the date of the previous bar.

sts
Posts: 4
Joined: 12 Mar 2012
Has thanked: 3 times

Re: Bar duration / seconds between bars

Postby sts » 20 Mar 2012

Thank you Henry... here is the code and seem like it works.
I added the volume threshold and the session starting time:

Code: Select all

inputs: ConstantVolume( 1000 ) ;

variables:
VolumeSize( ConstantVolume ),
LastDurationSeconds(0);

Condition2 = Date <> Date[1];

if condition2 then
LastDurationSeconds= (time_s - 100000)
else
LastDurationSeconds= (time_s - time_s[1]);

Plot1 (LastDurationSeconds, "Last Duration")


Return to “MultiCharts”