Description
This MACD Histogram with Colors indicator allows you to set colors for both positive and negative values of the Histogram.
Based on the original MACD Histogram 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 Histogram indicator provided by njardim@email.com on Feb 2016.
//
// Based on the original MACD Histogram 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 MACDHistogram : Indicator
{
public MacdHistogram MacdHistogram;
[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("Signal", Color = Colors.DodgerBlue, LineStyle = LineStyle.Lines)]
public IndicatorDataSeries Signal { get; set; }
protected override void Initialize()
{
MacdHistogram = Indicators.MacdHistogram(Source, LongCycle, ShortCycle, Periods);
}
public override void Calculate(int index)
{
if (OnAdaptive)
{
if (MacdHistogram.Histogram[index] > MacdHistogram.Histogram[index - 1])
{
HistogramPositive[index] = MacdHistogram.Histogram[index];
}
else if (MacdHistogram.Histogram[index] < MacdHistogram.Histogram[index - 1])
{
HistogramNegative[index] = MacdHistogram.Histogram[index];
}
}
else
{
if (MacdHistogram.Histogram[index] > 0)
{
HistogramPositive[index] = MacdHistogram.Histogram[index];
}
else if (MacdHistogram.Histogram[index] < 0)
{
HistogramNegative[index] = MacdHistogram.Histogram[index];
}
}
if (OnVerbose)
{
if (MacdHistogram.Histogram[index] > -0.0001 && MacdHistogram.Histogram[index] < 0.0001)
{
if (MacdHistogram.Histogram[index] > MacdHistogram.Signal[index])
{
ChartObjects.DrawText("Position", "Long: 0", StaticPosition.BottomCenter, Colors.DodgerBlue);
}
else
{
ChartObjects.DrawText("Position", "Short: 0", StaticPosition.BottomCenter, Colors.Red);
}
}
else if (MacdHistogram.Histogram[index] > MacdHistogram.Signal[index])
{
var nMacd = String.Format("{0}", Math.Round(MacdHistogram.Histogram[index], Symbol.Digits));
ChartObjects.DrawText("Position", "Long: " + nMacd, StaticPosition.BottomCenter, Colors.DodgerBlue);
}
else if (MacdHistogram.Histogram[index] < MacdHistogram.Signal[index])
{
var nMacd = String.Format("{0}", Math.Round(MacdHistogram.Histogram[index], Symbol.Digits));
ChartObjects.DrawText("Position", "Short: " + nMacd, StaticPosition.BottomCenter, Colors.Red);
}
}
Signal[index] = MacdHistogram.Signal[index];
}
}
}
NJ
njardim
Joined on 06.12.2015
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: MACD Histogram.algo
- Rating: 0
- Installs: 5726
- Modified: 13/10/2021 09:54
Note that publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section, please use the Copyright Infringement Notification form to submit a claim.
Comments
Log in to add a comment.
Great coding, but how about keeping the entry and exit positions simple
i.e.
public override void Calculate(int index)
{
if(MacdHistogram.Histogram.IsFalling())
{
HistogramNegative[index] = MacdHistogram.Histogram[index];
}
if (MacdHistogram.Histogram.IsRising())
{
HistogramPositive[index] = MacdHistogram.Histogram[index];
}
// comment : instead of
//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];
}
then use the if (%K > %D) while macdhistogam isRising() {position.long} else if ( %D>%K) while macdhistogam isFalling() {position.short}