How to create a bar timer

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
Posts: 177
Joined: 25 Dec 2010
Has thanked: 40 times
Been thanked: 83 times

How to create a bar timer

Postby NW27 » 09 Feb 2011


I'm trying to create a bar timer that will play a WAV file 30seconds before the bar completes. This is to get my attention.

I have tried the following code but it does not work correctly. The idea is that it plays once at 30 seconds and again at 10 seconds.
Looking at the two flags FirstAlarmPlayed and SecondAlarmPlayed with the Print statement, they are getting set an cleared at the correct times but the Playsound is not doing anything.
If I remove the checking of the flags in the IF statement, it does play the sound multiple times until a new bar is created.

By the way, I'm using 5 min bars.

Code: Select all

   Type - Indicator
   Name - NWT Bar Timer
          Desc - This indicator plays sounds to indicate an alarm prior to a new bar being displayed
   Written by Neil Wrightson

   Version    Date          Reason
   1      08/02/2011   Start

Inputs: En_FirstAlarm(1),FirstAlarm(30),FirstAlarm_WAV_File("C:\Sounds\Alert4.WAV");
Inputs: En_SecondAlarm(1),SecondAlarm(10),SecondAlarm_WAV_File("C:\Sounds\Alert2.WAV");
Variables : FirstAlarmPlayed(0),SecondAlarmPlayed(0);
Variables : L_AlarmTime(0),L_Currenttime(0);

L_Currenttime = el_timetodatetime_s(currenttime_s) ;
L_AlarmTime = (el_timetodatetime_s(time_s) - el_timetodatetime_s(FirstAlarm)) ;

if (L_Currenttime > L_AlarmTime) AND (FirstAlarmPlayed=0) then
    FirstAlarmPlayed = FirstAlarmPlayed + 1 ;

if el_timetodatetime_s(currenttime_s) < (el_timetodatetime_s(time_s) - el_timetodatetime_s(FirstAlarm)) then
   FirstAlarmPlayed = 0;

if (el_timetodatetime_s(currenttime_s) > (el_timetodatetime_s(time_s) - el_timetodatetime_s(SecondAlarm))) AND (SecondAlarmPlayed=0) then
    SecondAlarmPlayed = SecondAlarmPlayed + 1 ;

if el_timetodatetime_s(currenttime_s) < (el_timetodatetime_s(time_s) - el_timetodatetime_s(SecondAlarm)) then
   SecondAlarmPlayed = 0;
print(currenttime_s,FirstAlarmPlayed,SecondAlarmPlayed,"    ",L_AlarmTime:1:10,"    ",L_Currenttime:1:10)   ;

User avatar
Dave Masalov
Posts: 1712
Joined: 16 Apr 2010
Has thanked: 51 times
Been thanked: 485 times

Re: How to create a bar timer

Postby Dave Masalov » 15 Feb 2011

Dear NW27,

Please try to declare the variables the following way:

Variables : intrabarpersist FirstAlarmPlayed(0), intrabarpersist SecondAlarmPlayed(0);

Then the values before the next calculation won't be reset to the previous bar values for these variables.

Posts: 177
Joined: 25 Dec 2010
Has thanked: 40 times
Been thanked: 83 times

Re: How to create a bar timer

Postby NW27 » 16 Feb 2011


Thanks Dave.


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

Re: How to create a bar timer

Postby TJ » 16 Feb 2011

nice code

Return to “User Contributed Studies and Indicator Library”