Note: This indicator illustrates the ability to use either IF…THEN…ELSE or SWITCH…CASE to select one of five possible colors to paint an indicator line.
2 December, 2021: In this evening’s EasyLanguage Group Meeting, Perry asked me to help with an indicator to help count the occurrences of Price above or below or straddling a Moving Average Line.
I wound up going down a path that probably wasn’t what Perry wants, but I coded it in just a few minutes and saw several interesting things. (See the “_Above-Below-MA Counter (14)” Indicator below the Price Chart in the image below.
Notice that the indicator line is multicolored. Here is the key to the colors:
DarkGreen = Increment Line by +2 = This Bar is entirely above the Moving Average. (Trend is UP.)
Green = Increment Line by +1 = This Bar has more than 50% of its range ABOVE the Moving Average.
DarkGray = Line stays the same = This Bar is EXACTLY centered above & below the Moving Average (VERY Unlikely!)
Magenta = Increment Line by -1 = This Bar has more than 50% of its range BELOW the Moving Average.
Red = Increment Line by -2 = This Bar is entirely BELOW the Moving Average. (Trend is DOWN.)
(It is important that the Moving Average Length in the Indicator matches the Moving Average Length in the Chart.)
This is a Cumulative Indicator (Like On Balance Volume), meaning that it is possible for the line to NEVER return to Zero once it leaves that area. So, the idea is to look at the trend of the line and not its absolute value. I will leave it to the reader to decide if this Indicator has any value. The most obvious thing is to follow the trend when the line is DarkGreen or Red.
Another interesting thing is the code offered me a chance to demonstrate the difference between two Selection Statements:
1. A Compound If…Then…Else Statement, and
2. The SWITCH…CASE Statement.
Both are different versions of code to do the same thing: Select from multiple choices, in this case from 5 different alternatives (the colors of the line) based on the relationship between each Bar and the Moving Average. Compare each and decide which you prefer in this instance.
Please direct your comments, suggestions or questions to RoboticsProfessor@Gmail.com
Here is the code using the IF…THEN…ELSE Selection Statement:
[code]{#################################################
# FileName: _Above-Below-MA Counter
# Using IF...THEN...ELSE
# FileType: Indicator
# Author: Richard T. Vannoy II, For Perry,
# EL Group
# Version: v1.0
# Purpose: Show Bars above and below a Moving
# Average.
# Note: Notice the single, compound
# If...Then...Else Statement is used to
# select the five colors and
# increments.
#################################################
}
Inputs: MovingAverageLength(14); // Change to suit
// MAKE SURE YOU HAVE A SIMPLE MOVING AVERAGE IN
// YOUR CHART WITH THE EXACT LENGTH YOU SPECIFIED
// IN MovingAverageLength IN THE LINE ABOVE!
Variables:
MyMovingAverage(0),
RelativePosition(0),
RelativePositionSum(0);
MyMovingAverage = AverageFC(Close, MovingAverageLength);
{#################################################
# Compute Relative Position
#
# COLOR MOVEMENT MEANING
# DarkGreen = +2 = Entire Bar above MA
# LightGreen = +1 = More than half of Bar above
# MA
# DarkGray = 0 = Bar 50% above, 50% below MA
# Magenta = -1 = More than half of Bar below
# MA
# Red = -2 = Entire Bar below MA
#
# Note: Number is cumulative. It may stay a
# long way above or below 0 for extended
# times. Look for a change in color &
# direction, not specific values.
#################################################
}
RelativePosition = 0;
// Start with assumption of zero.
// (Hardly ever is 0!)
SetPlotColor(1, DarkGray);
// Use Compound If...Then...Else to Select
// Up/Down Line
// Increment and associated color
If Low > MyMovingAverage Then
Begin // Strong up color
RelativePosition = 2;
SetPlotColor(1, DarkGreen);
End
Else if High < MyMovingAverage then
Begin // Strong down color
RelativePosition = -2;
SetPlotColor(1, Red);
End
Else if absvalue(High - MyMovingAverage) >
absvalue(MyMovingAverage - Low) then
Begin // Medium up color
RelativePosition = 1;
SetPlotColor(1, Green);
End
Else if absvalue(High - MyMovingAverage) <
absvalue(MyMovingAverage - Low) then
Begin // Medium down color
RelativePosition = -1;
SetPlotColor(1, Magenta);
End;
// Add change caused by this Bar to the total.
RelativePositionSum =
RelativePositionSum + RelativePosition;
// Plot RealtivePositionSum
Plot1(RelativePositionSum, "SUM");
Plot2(0, "Zero");[/code]
And here is the SWITCH…CASE Version:
[code]{#################################################
# FileName: _Above-Below-MA Counter
# Using SWITCH...CASE
# FileType: Indicator
# Author: Richard T. Vannoy II, For Perry,
# EL Group
# Version: v1.0
# Purpose: Show Bars above and below a Moving
# Average.
# Note: Notice the single, compound
# If...Then...Else Statement is used to
# select the five colors and
# increments.
#################################################
}
Inputs: MovingAverageLength(14); // Change to suit
// MAKE SURE YOU HAVE A SIMPLE MOVING AVERAGE IN
// YOUR CHART WITH THE EXACT LENGTH YOU SPECIFIED
// IN MovingAverageLength IN THE LINE ABOVE!
Variables:
MyMovingAverage(0),
RelativePosition(0),
RelativePositionSum(0);
MyMovingAverage = AverageFC(Close,
MovingAverageLength);
{#################################################
# Compute Relative Position
#
# COLOR MOVEMENT MEANING
# DarkGreen = +2 = Entire Bar above MA
# LightGreen = +1 = More than half of Bar above
# MA
# DarkGray = 0 = Bar 50% above, 50% below MA
# Magenta = -1 = More than half of Bar below
# MA
# Red = -2 = Entire Bar below MA
#
# Note: Number is cumulative. It may stay a long
# way above or below 0 for extended times.
# Look for a change in color & direction,
# not specific values.
#################################################
}
RelativePosition = 0;
// Start with assumption of zero.
// (Hardly ever is 0!)
SetPlotColor(1, DarkGray);
// Use Compound If...Then...Else to Select
// Up/Down Line
// Increment and associated color
If Low > MyMovingAverage Then
begin
Switch(RelativePosition)
Case = 2:
If Low > MyMovingAverage then
SetPlotColor(1, DarkGreen);
Case = 1:
If absvalue (High - MyMovingAverage) >
absvalue(MyMovingAverage - Low) then
SetPlotColor(1, Green);
Case = -2:
If High < MyMovingAverage then
SetPlotColor(1, Red);
Case -1:
if absvalue(High - MyMovingAverage) <
absvalue(MyMovingAverage - Low) then
SetPlotColor(1, Magenta);
Default:
SetPlotColor(1, DarkGray);
// Add change caused by this Bar to the total.
RelativePositionSum =
RelativePositionSum + RelativePosition;
// Plot RealtivePositionSum
Plot1(RelativePositionSum, "SUM");
Plot2(0, "Zero");[/code]