macd signal line cross alert

Created at 22 Dec 2021, 21:33
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!
IR

IRCtrader

Joined 17.06.2021

macd signal line cross alert
22 Dec 2021, 21:33


i find this code for macd alert.

cTDN Forum - Adding a Crossover alert (ctrader.com)

i dont' know how to add signal line cross histogram to it?

other problem is about this library. i add it to indicator and now it has 10 megabyte. is there any way to reduce size of this indicator? i just need pop up alert without telegram and email.

how can i add sound to alert?

using cAlgo.API;
using cAlgo.API.Alert;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class MacdHistogramAlert : Indicator
    {
        #region Fields

        private MacdHistogram _macdHistogram;

        private int _lastAlertBarIndex;

        #endregion Fields

        #region Parameters

        [Parameter()]
        public DataSeries Source { get; set; }

        [Parameter("Long Cycle", DefaultValue = 26)]
        public int LongCycle { get; set; }

        [Parameter("Short Cycle", DefaultValue = 12)]
        public int ShortCycle { get; set; }

        [Parameter("Signal Periods", DefaultValue = 9)]
        public int Periods { get; set; }

        #endregion Parameters

        #region Outputs

        [Output("Histogram > 0", PlotType = PlotType.Histogram, Color = Colors.Green, Thickness = 2)]
        public IndicatorDataSeries HistogramPositive { get; set; }

        [Output("Histogram < 0", PlotType = PlotType.Histogram, Color = Colors.Red, Thickness = 2)]
        public IndicatorDataSeries HistogramNegative { get; set; }

        [Output("Signal", Color = Colors.Purple, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries Signal { get; set; }

        #endregion Outputs

        #region Methods

        protected override void Initialize()
        {
            _macdHistogram = Indicators.MacdHistogram(Source, LongCycle, ShortCycle, Periods);
        }

        public override void Calculate(int index)
        {
            if (_macdHistogram.Histogram[index] > 0)
            {
                HistogramPositive[index] = _macdHistogram.Histogram[index];
            }

            if (_macdHistogram.Histogram[index] < 0)
            {
                HistogramNegative[index] = _macdHistogram.Histogram[index];
            }

            Signal[index] = _macdHistogram.Signal[index];

            if (_macdHistogram.Histogram[index - 2] <= 0 && _macdHistogram.Histogram[index - 1] > 0)
            {
                TriggerAlert(TradeType.Buy, index);
            }
            else if (_macdHistogram.Histogram[index - 2] >= 0 && _macdHistogram.Histogram[index - 1] < 0)
            {
                TriggerAlert(TradeType.Sell, index);
            }
        }

        private void TriggerAlert(TradeType tradeType, int index)
        {
            if (index == _lastAlertBarIndex || !IsLastBar)
            {
                return;
            }

            string comment = tradeType == TradeType.Buy ? "Histogram is above 0" : "Histogram is below 0";

            Notifications.ShowPopup(TimeFrame, Symbol, MarketSeries.Close[index], "MACD Histogram Alert", tradeType, comment);
        }

        #endregion Methods
    }
}

 


@IRCtrader
Replies

amusleh
23 Dec 2021, 09:08

Hi,

What do you mean by: "i dont' know how to add signal line cross histogram to it?"

Can you show this on a chart screenshot?

Not sure about the exact size of library but if you use the newer version library the size might reduce.

You don't need that library to show a popup alert or play a sound alert.

cTrader automate API has the email and sound alert itself under Notifications: cAlgo API Reference - INotifications Interface (ctrader.com)

 


@amusleh

IRCtrader
23 Dec 2021, 21:07

RE:

amusleh said:

Hi,

What do you mean by: "i dont' know how to add signal line cross histogram to it?"

Can you show this on a chart screenshot?

Not sure about the exact size of library but if you use the newer version library the size might reduce.

You don't need that library to show a popup alert or play a sound alert.

cTrader automate API has the email and sound alert itself under Notifications: cAlgo API Reference - INotifications Interface (ctrader.com)

 

i make it. thanks for your amazing library.

it is 10 megabyte with source and i couldn't upload in algorithms.

using cAlgo.API;
using cAlgo.API.Alert;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class MacdHistogramAlert : Indicator
    {
        #region Fields

        private MacdHistogram _macdHistogram;

        private int _barIndex;

        #endregion Fields

        #region Parameters

        [Parameter()]
        public DataSeries Source { get; set; }

        [Parameter("Long Cycle", DefaultValue = 26)]
        public int LongCycle { get; set; }

        [Parameter("Short Cycle", DefaultValue = 12)]
        public int ShortCycle { get; set; }

        [Parameter("Signal Periods", DefaultValue = 9)]
        public int Periods { get; set; }

        [Parameter("Histogram Alert", DefaultValue = true)]
        public bool macdh { get; set; }

        [Parameter("Signal Alert", DefaultValue = true)]
        public bool macds { get; set; }
        #endregion Parameters

        #region Outputs

        [Output("Macd Up", LineColor = "Green", PlotType = PlotType.Histogram, Thickness = 2)]
        public IndicatorDataSeries HistogramPositive { get; set; }

        [Output("Macd Down", LineColor = "Red", PlotType = PlotType.Histogram, Thickness = 2)]
        public IndicatorDataSeries HistogramNegative { get; set; }

        [Output("Signal", LineColor = "Blue", LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries Signal { get; set; }

        #endregion Outputs

        #region Methods

        protected override void Initialize()
        {
            _macdHistogram = Indicators.MacdHistogram(Source, LongCycle, ShortCycle, Periods);
        }

        public override void Calculate(int index)
        {
            if (_macdHistogram.Histogram[index] > _macdHistogram.Signal[index])
            {
                HistogramPositive[index] = _macdHistogram.Histogram[index];
            }

            if (_macdHistogram.Histogram[index] < _macdHistogram.Signal[index])
            {
                HistogramNegative[index] = _macdHistogram.Histogram[index];
            }

            Signal[index] = _macdHistogram.Signal[index];

            if (macdh)
            {

                if (_macdHistogram.Histogram[index - 2] <= 0 && _macdHistogram.Histogram[index - 1] > 0)
                {
                    TriggerAlert(TradeType.Buy, index);
                }
                else if (_macdHistogram.Histogram[index - 2] >= 0 && _macdHistogram.Histogram[index - 1] < 0)
                {
                    TriggerAlert(TradeType.Sell, index);
                }
            }

            if (macds)
            {

                if (_macdHistogram.Histogram[index - 2] <= _macdHistogram.Signal[index - 2] && _macdHistogram.Histogram[index - 1] > _macdHistogram.Signal[index - 1])
                {
                    TriggerAlert2(TradeType.Buy, index);
                }
                else if (_macdHistogram.Histogram[index - 2] >= _macdHistogram.Signal[index - 2] && _macdHistogram.Histogram[index - 1] < _macdHistogram.Signal[index - 1])
                {
                    TriggerAlert2(TradeType.Sell, index);
                }
            }

        }

        private void TriggerAlert(TradeType tradeType, int index)
        {
            if (_barIndex != index && IsLastBar)
            {
                _barIndex = index;

                string comment = tradeType == TradeType.Buy ? "Histogram is above 0" : "Histogram is below 0";
                Notifications.ShowPopup(TimeFrame, Symbol, tradeType, "MACD Alert", Bars.ClosePrices[index], comment);
            }

        }

        private void TriggerAlert2(TradeType tradeType, int index)
        {
            if (_barIndex != index && IsLastBar)
            {
                _barIndex = index;

                string comment = tradeType == TradeType.Buy ? "Histogram is above signal" : "Histogram is below signal";
                Notifications.ShowPopup(TimeFrame, Symbol, tradeType, "MACD Alert", Bars.ClosePrices[index], comment);
            }

        }

        #endregion Methods
    }
}

 


@IRCtrader