Play sound some times and not endless

Created at 07 Oct 2019, 16:26
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!
SO

social.trading

Joined 06.10.2019

Play sound some times and not endless
07 Oct 2019, 16:26


Hello all,

what I acuatlly need is to play some sound 2-3 times when price reach certain indicator level. I'm trying a lot but not working.

Could any one please have a look into it.

I have some code below:

 if (isCrossBelow())
            {

                SellSeries[index] = MarketSeries.High[index];

                for (int i = 0; i < 5; i++)
                {
                    if (IsLastBar)
                    {
                        ChartObjects.DrawText("text1", "SELL", StaticPosition.Center, Colors.White);
                        ChartObjects.DrawText("", "" + i, StaticPosition.TopRight, Colors.White);
                        if (i == 4)
                        {
                            Notifications.PlaySound("C:....\action.mp3");
                        }
                    }
                }

 

Thank you.


@social.trading
Replies

social.trading
09 Oct 2019, 20:13

Any help from some expert here? I think it should be very simple.


@social.trading

PanagiotisCharalampous
10 Oct 2019, 08:50

Hi social.trading,

The easiest solution to this would be to use a sound file that repeats the sound as many times as you want and just play it from the cBot once.

Best Regards,

Panagiotis


@PanagiotisCharalampous

social.trading
10 Oct 2019, 09:42

Hello Panagiotis,

thank you for your feedback. The issue here how to play it once? Because the if loof will repeat. I also tried with bool true/false, but it is not working.

best regards


@social.trading

PanagiotisCharalampous
10 Oct 2019, 09:55

Hi social.trading,

First of all it would be better if you could post the complete cBot code. It is not clear what is the code supposed to do. Then give us a clear explanation of what do you expect the cBot to be doing. Why do you have a loop? Also why do you need to have the sound playing inside the loop?

Best Regards,

Panagiotis


@PanagiotisCharalampous

social.trading
10 Oct 2019, 10:38

Hello Panagiotis,

Actually, I'm trying to modifiy already exisiting indicator: https://ctrader.com/algos/indicators/show/1741

and add to it a sound notification:

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class LvAMaCross : Indicator
    {
        [Parameter("MA Type", DefaultValue = MovingAverageType.TimeSeries)]
        public MovingAverageType maType { get; set; }

        [Parameter("Slow Period", DefaultValue = 30, MinValue = 1)]
        public int slowPeriod { get; set; }

        [Parameter("Fast Period", DefaultValue = 12, MinValue = 1)]
        public int fastPeriod { get; set; }

        [Parameter("Time Frame MA", DefaultValue = "Hour")]
        public TimeFrame timeFrame { get; set; }


        [Output("Sell Point", Color = Colors.Red, PlotType = PlotType.Points, Thickness = 15)]
        public IndicatorDataSeries SellSeries { get; set; }

        [Output("Buy Point", Color = Colors.Blue, PlotType = PlotType.Points, Thickness = 15)]
        public IndicatorDataSeries BuySeries { get; set; }

        public bool alreadyPlayed = false;

        public IndicatorDataSeries FastDataSeries { get; set; }

        public IndicatorDataSeries SlowDataSeries { get; set; }


        //market series cho timeframe cần tính
        public MarketSeries selectedSeries;

        public MovingAverage fastMA;
        public MovingAverage slowMA;

        protected override void Initialize()
        {
            FastDataSeries = CreateDataSeries();
            SlowDataSeries = CreateDataSeries();

            if (timeFrame == MarketSeries.TimeFrame)
            {
                selectedSeries = MarketSeries;
                fastMA = Indicators.MovingAverage(MarketSeries.Close, fastPeriod, maType);
                slowMA = Indicators.MovingAverage(MarketSeries.Close, slowPeriod, maType);
            }
            else
            {
                selectedSeries = MarketData.GetSeries(timeFrame);
                fastMA = Indicators.MovingAverage(selectedSeries.Close, fastPeriod, maType);
                slowMA = Indicators.MovingAverage(selectedSeries.Close, slowPeriod, maType);
            }

        }

        public override void Calculate(int index)
        {


            var selectedIndex = selectedSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]);

            if (selectedIndex == -1)
            {
                return;
            }

            FastDataSeries[index] = fastMA.Result[selectedIndex];
            SlowDataSeries[index] = slowMA.Result[selectedIndex];

            if (isCrossBelow())
            {

                // sell
                SellSeries[index] = MarketSeries.High[index];

                if (IsLastBar)
                {
                    ChartObjects.DrawText("text1", "SELL", StaticPosition.Center, Colors.White);
                    if (!alreadyPlayed)
                    {
                        ChartObjects.DrawText("", "" + alreadyPlayed, StaticPosition.BottomRight, Colors.White);
                        Notifications.PlaySound("...action.mp3");
                        alreadyPlayed = true;
                      
                    }
                }
            }


            else if (isCrossAbove())
            {
                // buy 
                BuySeries[index] = MarketSeries.Low[index];
                if (IsLastBar)
                {
                    ChartObjects.DrawText("text1", "BUY", StaticPosition.Center, Colors.White);
                    if (!alreadyPlayed)
                    {
                        ChartObjects.DrawText("", "" + alreadyPlayed, StaticPosition.TopRight, Colors.White);
                        Notifications.PlaySound("...action.mp3");
                        alreadyPlayed = true;
                    }
                }
            }
        }

        #region Predicate
        public bool isCrossAbove()
        {
            return FastDataSeries.HasCrossedAbove(SlowDataSeries, 0);
        }
        public bool isCrossBelow()
        {
            return FastDataSeries.HasCrossedBelow(SlowDataSeries, 0);
        }
        #endregion
    }
}


@social.trading

PanagiotisCharalampous
10 Oct 2019, 10:45

Hi social.trading,

The way you have programmed it, it will play the sound only once and then it will stop working. You will need to reset the alreadyPlayed on the change of each bar. 

Best Regards,

Panagiotis


@PanagiotisCharalampous

social.trading
10 Oct 2019, 11:06

Hello Panagiotis,

unfortunaetly even once it isn't playing it, although the alreadyPlayed is set later to true. Any idea how to improve it, maybe different coding way.

best regards


@social.trading

PanagiotisCharalampous
10 Oct 2019, 11:12

Hi social.trading,

Did you check if the specific part of the code is executed? If no, you should investigate the reason. If yes, please make sure that the sound file is in the correct location. It would be better to start with an empty indicator and try to make the sound play on each bar change. After that works successfully, add the functionality to the existing indicator.

Best Regards,

Panagiotis


@PanagiotisCharalampous

social.trading
10 Oct 2019, 11:19

Hello Panagiotis,

yes I put at the beginning of the if loop, this code:

ChartObjects.DrawText("", "" + alreadyPlayed, StaticPosition.TopRight, Colors.White);

and the return is false, so the sound should play, but it isn't.

Yes the sound and the path are all correct, because it plays endless if I don't use the bool alreadyPlayed.

I'm some coding expierence, but seems not to be enough to implement this correctly.

 

best regards


@social.trading

PanagiotisCharalampous
10 Oct 2019, 12:22

Hi social.trading

The below seems to work fine for me

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.FileSystem)]
    public class LvAMaCross : Indicator
    {
        [Parameter("MA Type", DefaultValue = MovingAverageType.TimeSeries)]
        public MovingAverageType maType { get; set; }

        [Parameter("Slow Period", DefaultValue = 30, MinValue = 1)]
        public int slowPeriod { get; set; }

        [Parameter("Fast Period", DefaultValue = 12, MinValue = 1)]
        public int fastPeriod { get; set; }

        [Parameter("Time Frame MA", DefaultValue = "Hour")]
        public TimeFrame timeFrame { get; set; }


        [Output("Sell Point", Color = Colors.Red, PlotType = PlotType.Points, Thickness = 15)]
        public IndicatorDataSeries SellSeries { get; set; }

        [Output("Buy Point", Color = Colors.Blue, PlotType = PlotType.Points, Thickness = 15)]
        public IndicatorDataSeries BuySeries { get; set; }

        public bool alreadyPlayed = false;

        public IndicatorDataSeries FastDataSeries { get; set; }

        public IndicatorDataSeries SlowDataSeries { get; set; }


        //market series cho timeframe cần tính
        public MarketSeries selectedSeries;

        public MovingAverage fastMA;
        public MovingAverage slowMA;
        int _previousIndex;
        protected override void Initialize()
        {
            FastDataSeries = CreateDataSeries();
            SlowDataSeries = CreateDataSeries();

            if (timeFrame == MarketSeries.TimeFrame)
            {
                selectedSeries = MarketSeries;
                fastMA = Indicators.MovingAverage(MarketSeries.Close, fastPeriod, maType);
                slowMA = Indicators.MovingAverage(MarketSeries.Close, slowPeriod, maType);
            }
            else
            {
                selectedSeries = MarketData.GetSeries(timeFrame);
                fastMA = Indicators.MovingAverage(selectedSeries.Close, fastPeriod, maType);
                slowMA = Indicators.MovingAverage(selectedSeries.Close, slowPeriod, maType);
            }

        }

        public override void Calculate(int index)
        {
            var selectedIndex = selectedSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]);

            if (selectedIndex == -1)
            {
                return;
            }
            if (selectedIndex != _previousIndex)
            {
                _previousIndex = selectedIndex;
                alreadyPlayed = false;
            }
            FastDataSeries[index] = fastMA.Result[selectedIndex];
            SlowDataSeries[index] = slowMA.Result[selectedIndex];

            if (isCrossBelow())
            {

                // sell
                SellSeries[index] = MarketSeries.High[index];

                if (IsLastBar)
                {
                    ChartObjects.DrawText("text1", "SELL", StaticPosition.Center, Colors.White);
                    if (!alreadyPlayed)
                    {
                        ChartObjects.DrawText("", "" + alreadyPlayed, StaticPosition.BottomRight, Colors.White);
                        Notifications.PlaySound("C:\\Windows\\Media\\notify.wav");
                        Print("Play Sell Sound");
                        alreadyPlayed = true;

                    }
                }
            }


            else if (isCrossAbove())
            {
                // buy 
                BuySeries[index] = MarketSeries.Low[index];
                if (IsLastBar)
                {
                    ChartObjects.DrawText("text1", "BUY", StaticPosition.Center, Colors.White);
                    if (!alreadyPlayed)
                    {
                        ChartObjects.DrawText("", "" + alreadyPlayed, StaticPosition.TopRight, Colors.White);
                        Notifications.PlaySound("C:\\Windows\\Media\\notify.wav");
                        Print("Play Buy Sound");
                        alreadyPlayed = true;
                    }
                }
            }
        }

        #region Predicate
        public bool isCrossAbove()
        {
            return FastDataSeries.HasCrossedAbove(SlowDataSeries, 0);
        }
        public bool isCrossBelow()
        {
            return FastDataSeries.HasCrossedBelow(SlowDataSeries, 0);
        }
        #endregion
    }
}

Best Regards,

Panagiotis


@PanagiotisCharalampous

social.trading
10 Oct 2019, 12:44 ( Updated at: 21 Dec 2023, 09:21 )

RE:

Hello Panagiotis,

I tried your code, but as you see from the screenshot, it is entering the if loop, printing false, but not playing the sound or printing Print("Play Buy Sound");

best regards


@social.trading

PanagiotisCharalampous
10 Oct 2019, 12:47

Hi social.trading,

UPDATE: Sorry, sceenshot just appeared. Can you send a screenshot of the log as well?

Best Regards,

Panagiotis


@PanagiotisCharalampous

social.trading
10 Oct 2019, 12:54

Hello Panagiotis,

sorry, where can I find the log?

best regards


@social.trading

PanagiotisCharalampous
10 Oct 2019, 14:52 ( Updated at: 21 Dec 2023, 09:21 )

Hi social.trading,

See below. Do you get these entries whenever a dot is drawn on the chart?

Best Regards,

Panagiotis


@PanagiotisCharalampous

social.trading
10 Oct 2019, 15:57 ( Updated at: 21 Dec 2023, 09:21 )

Hello Panagiotis,

yes there I was looking, but I don't have it (pic attached). I also looked how to add but failed.

best regards


@social.trading

PanagiotisCharalampous
10 Oct 2019, 15:59

Hi social.trading,

This tab is available only in cTrader Automate tab.

Best Regards,

Panagiotis


@PanagiotisCharalampous