Play sound some times and not endless

07 Oct 2019, 16:26Play sound some times and not endless#1
social.tradingposts: 9since: 06 Oct 2019

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.

09 Oct 2019, 20:13#2
social.tradingposts: 9since: 06 Oct 2019

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

10 Oct 2019, 08:50#3
Panagiotis Charalampousposts: 3545since: 13 Jan 2017

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


Head of Community Management at cTrader
10 Oct 2019, 09:42#4
social.tradingposts: 9since: 06 Oct 2019

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

10 Oct 2019, 09:55#5
Panagiotis Charalampousposts: 3545since: 13 Jan 2017

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


Head of Community Management at cTrader
10 Oct 2019, 10:38#6
social.tradingposts: 9since: 06 Oct 2019

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
    }
}

10 Oct 2019, 10:45#7
Panagiotis Charalampousposts: 3545since: 13 Jan 2017

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


Head of Community Management at cTrader
10 Oct 2019, 11:06#8
social.tradingposts: 9since: 06 Oct 2019

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

10 Oct 2019, 11:12#9
Panagiotis Charalampousposts: 3545since: 13 Jan 2017

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


Head of Community Management at cTrader
10 Oct 2019, 11:19#10
social.tradingposts: 9since: 06 Oct 2019

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