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");
Help with Playsound()
- TJ
- Posts: 7743
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2222 times
- Alex Kramer
- Posts: 834
- Joined: 23 Feb 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;
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;