Category Oscilators  Published on 04/02/2016

MACD Crossover with Colors (v2)

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

 

 


// -------------------------------------------------------------------------------------------------
//
//    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];

        }
    }
}


NJ
njardim

Joined on 06.12.2015

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: MACD Crossover.algo
  • Rating: 5
  • Installs: 9727
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
CT
ctid4619599 · 3 years ago

Thanks. Because of this nice indicator I throw out both EMA´s and Stochastic. It´s just a clearer picture.

AL
alvesneto.eduardo · 4 years ago

Thanks!!! 

JJ
jjcolacicco · 6 years ago

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

Traderfofever's avatar
Traderfofever · 6 years ago

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!