Saturday, September 04, 2010

Forecast Oscillator [FO]

Descripton
Summary:
This indicator script measures the percentage difference between the actual underlying indicator value and its linear regression line value. 

Calculation:
Subtract the last linear regression forecast from the last close and divide the result by the last close.

Range:
0 To 100

Developer:
Tushar Chande

Interpretation:
1. Positive Crossover: When the indicator script crosses above zero, the underlying indicator is likely to move upwards.
2. Negative Crossover: When the indicator script crosses below zero, the underlying indicator is likely to move downwards.


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

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

OnValues
Function Parameters
TypeIdentifierDescription
IntegerbarIndex
Integerlength
Implementation
	 ' Use for the underlying indicator values, indexed by bar index.
	Define values() As Number = IndicatorValues(_indicatorKey, barIndex, length + _periods + 1)
	 ' Use for the sum of X times Y, sum of X, sum of Y, sum of X^2.
	Define sumXY,sumX,sumY,sumXPower,LR  As Number
	 ' Use for the calculated indicator values, indexed by bar index.
	Define results(length - 1) As Number 
	 ' Calculate the indicator values for the specified bar range.
	For i As Integer = length - 1 To 0 Step -1 
		 ' Calculate the indicator value for the current bar.
		sumXY = 0
		sumX = 0
		sumY = 0
		sumXPower = 0
		For j As Integer = i + _periods To i + 1 Step -1
			sumXY += j * values(j)
			sumX += j
			sumY += values(j)
			sumXPower += j * j 
		Next
		LR = ((_periods * sumXY) - (sumX * sumY)) / (_periods * sumXPower - sumX * sumX)
		LR = (sumY - LR * sumX) / _periods
		results(i) = 100 * ((values(i) - LR) / values(i))
	Next 
	Return results 

Copyright © 2010 IQBroker, LLC. All rights reserved.