Professional Documents
Culture Documents
MACD and Histogram Divergence Detection
MACD and Histogram Divergence Detection
//
// MACD divergence system
//
// This system detects MACD and MACD-H positive and negative divergences, and
// crossovers.
//---------------------------------------------------------------------------------
-
//------
// Setup
//------
// Parameters
PeriodEMA = 13;
MACDIndicatorRange = 50;
// Volume filter
VolumeFilter = Param( "Volume MA filter", 100000, 50000, 500000, 100000 );
Timeframe = Interval(2);
// Minimum width of negative projecting wave between two positive MACD-H waves,
// otherwise two positive waves will be considered as one single wave. This
// minimises invalid divergences, to ensure that "back of bears is broken".
// The same applies for a positive wave between two negative waves.
HistMinWidth = Param("Histogram min width", 4, 1, 10, 1 );
// Other parameters
OpenPositions = 10;
ATRPeriod = 5;
InitialCapital = 100000;
// Get displayed min and max value of MACD and MACD-H, to rescale it for better
visibility
scMACDMax = LastValue(HHV(Max(MACDInd, sigInd),
BarsSince( Status("barvisible") AND NOT
Ref(Status("barvisible"),-1) )));
scMACDMin = LastValue(LLV(Min(MACDInd, sigInd),
BarsSince( Status("barvisible") AND NOT
Ref(Status("barvisible"),-1) )));
scaleMACD = Max( abs(scMACDMax), abs(scMACDMin) );
scHistMax = LastValue(HHV(HistInd,
BarsSince( Status("barvisible") AND NOT Ref(Status("barvisible"),-
1) )));
scHistMin = LastValue(LLV(HistInd,
BarsSince( Status("barvisible") AND NOT Ref(Status("barvisible"),-
1) )));
scaleHist = Max( abs(scHistMax), abs(scHistMin) );
//--------------------------
// Positive MACD divergences
//--------------------------
// Get array containing for each element, when the MACD's lowest
// value occur, within the specified number of bars
MACDLowBars = LLVBars( MACDInd, MACDDivMinWidth );
// Get array containing MACD low bar value at bar where MACD
// low occured, all other bars filled with preceding MACD
// low value (chandelier)
MACDLowSteps = ValueWhen( Ref(MACDMinCond,0),
Ref(MACDInd,-1), 1 );
// Divergence signal
MACDPosDivergence = // MACDLowDiffs > 0 AND
MACDLowVal < 0
AND MACDLowVal > LLV(MACDLowVal, MACDIndicatorRange )
AND PriceMACDLowVal < LLV(Ref(PriceMACDLowVal,-1), MACDIndicatorRange )
AND PriceMACDLowDiffs < 0
AND MACDLowBarDiffs < MACDIndicatorRange
AND VolumeMABool
AND C > 1.0
// AND WeekBuyBool
;
//--------------------------
// Negative MACD divergences
//--------------------------
// Get array containing for each element, when the MACD's highest
// value occur, within the specified number of bars
MACDHighBars = HHVBars( MACDInd, MACDDivMinWidth );
// Get array containing MACD high bar value at bar where MACD
// high occured, all other bars filled with preceding MACD
// high value (chandelier)
MACDHighSteps = ValueWhen( Ref(MACDMaxCond,0),
Ref(MACDInd,-1), 1 );
// Divergence signal
MACDNegDivergence = // MACDHighDiffs < 0 AND
MACDHighVal > 0
AND PriceMACDHighVal > HHV( Ref(PriceMACDHighVal,-1), MACDIndicatorRange )
//------------------------------------
// Positive MACD Histogram divergences
//------------------------------------
// Get arrays containing for each element, when the MACD-H lowest
// values occur, within the specified number of bars
HistLowBars = LLVBars( HistInd, MACDDivMinWidth );
// Get array defining if the MACD-H previous bar was the minimum
// AND if that MACD-H value was < 0 or > 0
HistMinCond = HistLowBars > 0 AND
BarsSinceNegCross >= HistLowBars AND
Ref(HistLowBars,-1) == 0 AND
Ref(HistInd,-1) < 0;
// Get array containing MACD-H low bar value at bar where MACD-H
// low occured, all other bars filled with preceding MACD-H
// low value (chandelier)
HistLowSteps = ValueWhen( Ref(HistMinCond,0), Ref(HistInd,-1), 1 );
// Get array containing differences between MACD-H low bar value and the value
// at previous MACD-H zero positive crossing
// HistLowDiffs = HistLowSteps - ValueWhen( HistPosCrossover, HistLowSteps, 1 );
HistLowDiffs = HistLowSteps - Ref(HistLowSteps,-1);
// Get array containing MACD-H min value when MACD-H < 0, all other
// bars filled with 0
BarsSincePrevNegCross = ValueWhen( HistNegCrossover,
Ref(BarsSinceNegCross ,-1), 1 );
BarsSinceWideNegCross = IIf( HistInd < 0 AND
BarsSincePosCross - BarsSinceNegCross <
HistMinWidth,
BarsSincePrevNegCross + BarsSinceNegCross + 1,
BarsSinceNegCross );
HistMinSteps = IIf( !BarsSinceWideNegCross,
HistInd,
LLV( HistInd , BarsSinceWideNegCross + 1) );
// Get array containing price low minimum value when MACD-H < 0,
// all other bars filled with 0
PriceHistMinSteps = IIf( !BarsSinceNegCross,
L,
LLV( L , BarsSinceNegCross) );
//------------------------------------
// Negative MACD Histogram divergences
//------------------------------------
// Get arrays containing for each element, when the MACD-H highest values
// occur, within the specified number of bars
HistHighBars = HHVBars( HistInd, MACDDivMinWidth );
// Get array defining if the MACD-H previous bar was the maximum,
// AND if that MACD-H value was < 0 OR > 0
HistMaxCond = HistHighBars > 0
AND BarsSincePosCross >= HistHighBars
AND Ref(HistHighBars,-1) == 0
AND Ref(HistInd,-1) > 0
;
// Get array containing MACD-H high bar value at bar where MACD-H
// high occured, all other bars filled with preceding MACD-H
// high value (chandelier)
HistHighSteps = ValueWhen( Ref(HistMaxCond,0),
Ref(HistInd,-1), 1 );
// Get array containing MACD-H max value when MACD-H > 0, all other
// bars filled with 0
BarsSincePrevPosCross = ValueWhen( HistPosCrossover,
Ref(BarsSincePosCross ,-1), 1 );
BarsSincePrevNegCross = ValueWhen( HistNegCrossover,
Ref(BarsSinceNegCross ,-1), 1 );
BarsSinceWidePosCross = IIf( HistInd > 0 AND
BarsSinceNegCross - BarsSincePosCross <
HistMinWidth,
BarsSincePrevPosCross + BarsSincePosCross + 1,
BarsSincePosCross );
HistMaxSteps = IIf( !BarsSinceWidePosCross,
HistInd,
HHV( HistInd , BarsSinceWidePosCross + 1) );
// Get array containing MACD-H max value when MACD-H > 0, all other
// bars filled with 0
PriceHistMaxSteps = IIf( !BarsSincePosCross,
H,
HHV( H , BarsSincePosCross) );
// Divergence signal
HistNegDivergence = // HistHighDiffs < 0 AND
// AND PriceHistHighDiffs > 0
HistMaxSteps < PrevHistMaxSteps
AND PriceHistMaxSteps > PrevPriceHistMaxSteps
AND HistHighVal > 0
AND VolumeMABool
AND C > 1.0
;
//-----------------------
// MACD crossover signals
//-----------------------
// Plot signals
PlotShapes( IIf( HistPosDivergence , shapeSmallUpTriangle, shapeNone ),
colorBlue, 0, 0 , -12 );
PlotShapes( IIf( HistNegDivergence , shapeSmallDownTriangle, shapeNone ),
colorBlue, 0, 0, -12 );
PlotShapes( IIf( MACDPosDivergence , shapeUpArrow , shapeNone ),
colorBlue, 0, Graph1, -12 );
PlotShapes( IIf( MACDNegDivergence , shapeDownArrow, shapeNone ),
colorBlue, 0, Graph1, -12 );
PlotShapes( IIf( PosCrossover , shapeSmallCircle, shapeNone ),
colorBlue, 0, Graph1 , 0 );
PlotShapes( IIf( NegCrossover , shapeSmallCircle, shapeNone ),
colorBlue, 0, Graph1 , 0 );
Filter = HistPosDivergence
OR HistNegDivergence
OR PosCrossover
OR NegCrossover
OR MACDPosDivergence
OR MACDNegDivergence
;
_SECTION_END();