Help with Playsound()

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

Help with Playsound()

Postby TJ » 17 Oct 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: 23 Feb 2006

Postby Alex Kramer » 18 Oct 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;


Return to “MultiCharts”