Playing sound, was: Alertif doesn't work

if (BuyL AND BuyVar != BuyVar1)
{
brd.PlaceOrder(exchange,symbol,"" ,"Buy"+symbol, 1,qty,1,0, 0, "MKT", "CNC", "XT1234","DAY");
StaticVarSetText("Buy",BuyVar1);
AlertIf( 1, "SOUND C:\\Windows\\Media\\Windows Notify.wav", "Audio alert", 1 );
}
if (SellL AND SellVar != SellVar1)
{
brd.PlaceOrder(exchange,symbol, "","Sell"+symbol, 2,qty,1,0, 0, "MKT", "CNC", "XT1234","DAY");
StaticVarSetText("Sell",SellVar1);
AlertIf( 1, "SOUND C:\\Windows\\Media\\Windows Notify.wav", "Audio alert", 2);

}```

brd.placeorder function is getting triggered at the right times but the alertif is not making any  
sound.

You are using AlertIf incorrectly. AlertIf works perfectly fine but it has internal logic that is state machine that prevents repeated signals that you simply don't understand (careful reading is required: http://www.amibroker.com/f?alertif)

For playing sound unconditionally you should use PlaySound function: http://www.amibroker.com/f?playsound

1 Like

Hi Tomasz, I've followed the instructions on the linked page and find my audio alerts aren't triggering with bar replay. My PC's audio is ok and there is a file named Alarm07 Windows which also works. Can you suggest something further I need to check please?

AlertIf( Buy, "C:\Windows\Media\Alarm07.wav", "Audio alert", 2 );

Hey you have to write like this:
AlertIf( Buy, "C:\Windows\Media\Alarm07.wav", "Audio alert", 2,2 );

It worked for me like this!

Thanks but that gives me "error 54 incorrect \ espace sequence"

I added double backspaces which removes the error but the audio alert
still doesn't work.

AlertIf( 1, "SOUND C:\Windows\Media\Windows Notify.wav", "Audio alert", 1 ,2);

Type like this, You forgot the sound command too.

Still nothing with:

AlertIf( Buy, "SOUND C:\Windows\Media\Alarm07.wav", "Audio alert", 2 ,2);

[double backslash don't show on forum for anyone else reading]

Are you sure you are getting the buy signals ?

yes, both on backtest and bar replay.

PlaySound JUST WORKS, if it doesn't it means that the path to the file is wrong. There must be double backslashes in the path and the file must exist. Just copy-paste example from the MANUAL:

PlaySound("c:\\windows\\media\\ding.wav"); // DOUBLE backslashes

printf("Error description: %s", GetLastOSError() );

If it does not work, GetLastOSError function would give you answer what you did wrong.

For GetLastOSError() you need to use latest AmIBroker version (6.28).

2 Likes

Thanks Tomasz. I can hear it if I use "Alarm", ("ding" was barely audible even with speakers turned up).

It's going off with every bar, regardless of Buy signal or not. I tried:

IIF( Buy, PlaySound("c:\windows\media\Alarm07.wav"),null);

Iif() is NOT flow control statement, it is a function, see: IIf function versus if statement

You need to use flow control statement http://www.amibroker.com/guide/keyword/if.html

Basic mistakes: http://www.amibroker.com/guide/a_mistakes.html

1 Like

Finally. This works.

if (SelectedValue(Buy)==1) Say("are you sure you want to buy this? I mean... really?");

1 Like

@Tomasz the alert doesnt come when the amibroker is minimized.

By default charts are not refreshed (and your code is not executed) when AB is minimized to conserve computer's resources. Here you can find more information and possible solutions:

2 Likes