MACD Crossover with Colors (v2)

by njardim in category Oscilator at 15/12/2015
Description

This MACD Crossover with Colors indicator allows you to set colors for both positive and negative values of the Histogram.

Based on the original MACD Crossover indicator from cTrade.

v2

+ Adaptive (auto-color): Allows the option to display adaptive auto-colors changing according the trend.

+ Verbose: Allows the option to display the current Long or Short cycle and it's value.

v1

Public release

 

 

Download
3875 downloads
How to install
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader
// -------------------------------------------------------------------------------------------------
//
//    This code is a cAlgo API MACD Crossover indicator provided by njardim@email.com on Dec 2015.
//
//    Based on the original MACD Crossover indicator from cTrade.
//
// -------------------------------------------------------------------------------------------------

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MACDCrossover : Indicator
    {

        public MacdCrossOver MacdCrossOver;

        [Parameter()]

        public DataSeries Source { get; set; }

        [Parameter("Adaptive (auto-color)", DefaultValue = false)]
        public bool OnAdaptive { get; set; }

        [Parameter("Verbose", DefaultValue = true)]
        public bool OnVerbose { 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; }

        [Output("Histogram Up", PlotType = PlotType.Histogram, Color = Colors.DodgerBlue)]
        public IndicatorDataSeries HistogramPositive { get; set; }

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

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

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

        protected override void Initialize()
        {

            MacdCrossOver = Indicators.MacdCrossOver(Source, LongCycle, ShortCycle, Periods);

        }

        public override void Calculate(int index)
        {

            if (OnAdaptive)
            {

                if (MacdCrossOver.Histogram[index] > MacdCrossOver.Histogram[index - 1])
                {
                    HistogramPositive[index] = MacdCrossOver.Histogram[index];
                }

                else if (MacdCrossOver.Histogram[index] < MacdCrossOver.Histogram[index - 1])
                {
                    HistogramNegative[index] = MacdCrossOver.Histogram[index];
                }
            }
            else
            {
                if (MacdCrossOver.Histogram[index] > 0)
                {
                    HistogramPositive[index] = MacdCrossOver.Histogram[index];
                }

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

            if (OnVerbose)
            {

                if (MacdCrossOver.MACD[index] > -0.0001 && MacdCrossOver.MACD[index] < 0.0001)
                {
                    if (MacdCrossOver.MACD[index] > MacdCrossOver.Signal[index])
                    {
                        ChartObjects.DrawText("Position", "Long: 0", StaticPosition.BottomCenter, Colors.DodgerBlue);
                    }
                    else
                    {
                        ChartObjects.DrawText("Position", "Short: 0", StaticPosition.BottomCenter, Colors.Red);
                    }
                }

                else if (MacdCrossOver.MACD[index] > MacdCrossOver.Signal[index])
                {
                    var nMacd = String.Format("{0}", Math.Round(MacdCrossOver.MACD[index], Symbol.Digits));
                    ChartObjects.DrawText("Position", "Long: " + nMacd, StaticPosition.BottomCenter, Colors.DodgerBlue);
                }

                else if (MacdCrossOver.MACD[index] < MacdCrossOver.Signal[index])
                {
                    var nMacd = String.Format("{0}", Math.Round(MacdCrossOver.MACD[index], Symbol.Digits));
                    ChartObjects.DrawText("Position", "Short: " + nMacd, StaticPosition.BottomCenter, Colors.Red);
                }
            }

            Signal[index] = MacdCrossOver.Signal[index];

            MACD[index] = MacdCrossOver.MACD[index];

        }
    }
}
Comments

Trader4ever - May 07, 2018 @ 10:55

Hi,my friend, thank you for your work, and could you please publish another version which will show arrows on the chart when crossover issued?

That will be perfect:)

 

Best Regards!

jjcolacicco - November 26, 2018 @ 12:36

Hello and thanks for the indicator.  it is such a visual releif..

Can an alert, pop up box, be created for when the histogram croses the centre line?

 

Kind regards

James

5