Help with Playsound()

Questions about MultiCharts and user contributed studies.
User avatar
TJ
Posts: 7765
Joined: Aug 29 2006
Location: Global Citizen
Has thanked: 1036 times
Been thanked: 2232 times

Oct 17 2006

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");

User avatar
Alex Kramer
Posts: 834
Joined: Feb 23 2006

Oct 18 2006

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;