Saturday, September 04, 2010

Center Of Gravity [COG]

Descripton
Summary:
This indicator script is a smoothed oscillator with essentially zero lag, it measures the center of gravity price for a window of bar prices.

Calculation:
Complex, see script.

Developer:
John F. Ehlers

Interpretation: 
1. Upwards Trend: When the indicator script is moving upwards, the underlying indicator is in an upwards trend.
2. Downwards Trend: When the indicator script is moving downwards, the underlying indicator is in a downwards trend.
3. Positive Divergence:  When the underlying indicator is moving downwards, but the indicator script is moving upwards, the underlying indicator is likely to reverse direction and move upwards.
4. Negative Divergence:  When the underlying indicator is moving upwards, but the indicator script is moving downwards, the underlying indicator is likely to reverse direction and move downwards.
5. Overbought:  When the underlying indicator is moving upwards, but the indicator script is approaching its upper bound, the underlying indicator is likely to reverse direction and move downwards.
6. Oversold: When the underlying indicator is moving downwards, but the indicator script is approaching its lower bound, the underlying indicator is likely to reverse direction and move upwards.



Variables
TypeIdentifierDescription
Integer_indicatorKeyUse for the underlying indicator key on which to calculate the Center Of Gravity.
Integer_periodsUse for the number of periods to include in the Center Of Gravity calculation.

OnInitialize
Function Parameters
TypeIdentifierDescription
IndicatorindicatorKeyUse for the underlying indicator key on which to calculate the Center Of Gravity.
IntegerperiodsUse for the number of periods to include in the Center Of Gravity calculation. [Default 10]
Implementation
	 ' Assign the parameters to script variables.
	_indicatorKey = indicatorKey
	_periods = periods

OnValues
Function Parameters
TypeIdentifierDescription
IntegerbarIndex
Integerlength
Implementation
	 ' Get the values of the underlying indicator, indexed by bar index.
	Define values() As Number = IndicatorValues(_indicatorKey, barIndex, length + _periods)
	 ' Use for the indicator formula numerator value.
	Define numerator As Number
	 ' Use for the indicator formula denominator value.
	Define denominator As Number
	 ' Use for the Center Of Gravity values, indexed by bar index.
	Define results(length - 1) As Number
	 ' Calculate the Center Of Gravity values for the specified bar range.
	For i As Integer = length - 1 To 0 Step -1
		numerator = 0
		denominator = 0
		 ' Calculate the Center Of Gravity value for the current bar.
		For j As Integer = i + _periods - 1 To i Step -1
			numerator += values(j) * (j - i + 1)
			denominator += values(j)
		Next
		 ' Assign the Center Of Gravity value for the current bar.
		results(i) = -1 * (numerator / denominator)
	Next
	Return results

Copyright © 2010 IQBroker, LLC. All rights reserved.