PlaySound does not work if called from Initialize

Created at 01 Nov 2013, 19:40
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
BP

bp2012

Joined 31.08.2013

PlaySound does not work if called from Initialize
01 Nov 2013, 19:40


Please see the code below. It seems that Notifications.PlaySound does not work if called within the initialize method. However uncommenting the code in the Calculate method results in a sound every tick. 

A recurring pattern in some of my indicators is to paint during the initialize phase and then repaint every x minutes or bars. If alert criteria is met during that first paint inside the initialize method, the notification sound does not play.

using System;
using System.IO;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false)]
    public class NewIndicator : Indicator
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }


        protected override void Initialize()
        {
            // Initialize and create nested indicators
            var windowsFolder = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
            Notifications.PlaySound(Path.Combine(windowsFolder, "Media", "tada.wav"));
            SendAlert();
        }

        public override void Calculate(int index)
        {
            //SendAlert();

        }

        void SendAlert()
        {
            var windowsFolder = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
            Notifications.PlaySound(Path.Combine(windowsFolder, "Media", "tada.wav"));
        }

    }
}

 


@bp2012
Replies

Cerunnos
01 Nov 2013, 20:00

Are the following options enabled:
cAlgo / cTrader -> preferences -> sounds
                         -> notifications -> show all


@Cerunnos

bp2012
01 Nov 2013, 20:11

Yes. I checked the notifications and sound options in both cAlgo and cTrader.

Does the code sample work for you? I hear nothing for the initialize, but if I uncomment the SendAlert call in the calculate method, I hear the sound file.


@bp2012

Cerunnos
01 Nov 2013, 20:33

RE:

bp2012 said:

Yes. I checked the notifications and sound options in both cAlgo and cTrader.

Does the code sample work for you? I hear nothing for the initialize, but if I uncomment the SendAlert call in the calculate method, I hear the sound file.

Strange. But it is important that it works with Calculate () :-)
With OnStart() it works too...


@Cerunnos

bp2012
01 Nov 2013, 21:12

Must be a defect. Thanks very much for checking!


@bp2012

Spotware
04 Nov 2013, 17:13

RE:

bp2012 said:

Please see the code below. It seems that Notifications.PlaySound does not work if called within the initialize method. However uncommenting the code in the Calculate method results in a sound every tick. 

A recurring pattern in some of my indicators is to paint during the initialize phase and then repaint every x minutes or bars. If alert criteria is met during that first paint inside the initialize method, the notification sound does not play.

In Indicators it is only possible to call this method from the Calculate method.


@Spotware

bp2012
01 Dec 2013, 21:22

Is there a specific design reason for this behavior? Otherwise, I'd say that it is a defect. Whether or not it is a priority for you to fix is of course completely up to you. It's a low priority for me personally since messageboxes can be displayed in the initialize method.


@bp2012

Spotware
02 Dec 2013, 16:06

Initialize is called every time the Timeframe or Symbol of a chart is changed and when new historical data is received. When you open the chart cached data is first shown and calculated indicators. Then actual data is downloaded and the indicator is calculated one more time.
Why do you need to play sound in Intialize?


@Spotware

bp2012
02 Dec 2013, 16:27

It's definitely not a big priority. However, I have a moderately complex indicator that performs the historical heavy lifting in the initialize method. Performing the code on each index in the calculate method would cause a big delay on load. So I use isRealTime in the calculate method to exit the method if it is being call on historical data. In a nutshell, I can quickly run the code on the data needed in the initialize method and then process the new data as it comes through in the calculate method.

The calculate method will alert when specific triggers are met from that time forward. However, when just starting up it is good to alert the user if a trigger was met in the last x bars. 

Thanks for the follow up!


@bp2012