Page 1 of 1

Help with Playsound()

Posted: Oct 17 2006
by TJ
I have a 200 period moving average on 2401 volume bar chart.
I would like to add a sound signal when the price cross the MA line.

I have added a playsound() statement to the code.
It plays the sound signal alright. The problem is, it plays the sound multiple times (on every tick), until another new bar is created. This happens even if I turn off the "Update on every tick" box.

What can I do to make it play the sound only once per bar?


here's the code:
inputs:
Price( Close ),
Length( 50 ) ;

variables:
Avg( 0 ) ;

Avg = AverageFC( Price, Length ) ;

Plot1[1]( Avg, "Avg" ) ;

{ Alert criteria }
if Price > Avg and Avg > Avg[1] and Avg[1] <= Avg[2] then
PlaySound("c:/soundfiles/alarm.wav")

else if Price < Avg and Avg < Avg[1] and Avg[1] >= Avg[2] then
PlaySound("c:/soundfiles/alarm.wav");

Posted: Oct 18 2006
by Alex Kramer
Please try this out:

inputs:
Price( Close ),
Length( 50 ) ;

variables:
Avg( 0 ) ;

var : intrabarpersist last_played_bar(0);

Avg = AverageFC( Price, Length ) ;

Plot1[1]( Avg, "Avg" ) ;

{ Alert criteria }
if Price > Avg and Avg > Avg[1] and Avg[1] <= Avg[2] then begin
if last_played_bar<>currentbar then begin
last_played_bar=currentbar;
PlaySound("c:/soundfiles/alarm.wav");
end;
end
else
if Price < Avg and Avg < Avg[1] and Avg[1] >= Avg[2] then begin
if last_played_bar<>currentbar then begin
last_played_bar=currentbar;
PlaySound("c:/soundfiles/alarm.wav");
end;
end;