Title:Kaufman Adaptive Moving Average for eSignal 7.x
By: Chris D. Kryza (Divergence Software, Inc.)
Email: [email protected], [email protected]
Web:http://www.sr-analyst.com/eSignal.htm
Incept: 02/18/2004
Version: 1.0.0
================================================== ===================
Fix History:
02/18/2004 - Initial Release
1.0.0
================================================== ===================
Project Description:
Kaufman Adaptive Moving Average.
Dislaimer: For educational purposes only! Obviously, no guarantees
whatsoever and use at your own risk.
************************************************** ********************/
//Global Variables
var grID = 0;
var nBarCounter= 0;
var nPeriod= 0;
var nFast= 0;
var nSlow= 0;
var aAMA= null;
var aFPArray= new Array();
var bInitialized= false;
//== PreMain function required by eSignal to set things up
function preMain() {
var x;
setPriceStudy(true);
setStudyTitle("KAMA");
setCursorLabelName("KAMA", 0);
setDefaultBarFgColor( Color.blue, 0 );
setShowTitleParameters( false );
setColorPriceBars(true);
setDefaultPriceBarColor(Color.black);
//unrem this if you don't want the labels in cursor window
//setShowCursorLabel(false);
//unrem this if you don't want the study to update on every tick
//setComputeOnClose();
grID = 0;
//initialize formula parameters
x=0;
aFPArray[x] = new FunctionParameter( "Period", FunctionParameter.NUMBER);
with( aFPArray[x] ) {
setName( "KAMA Period" );
setLowerLimit( 5 );
setUpperLimit( 125 );
setDefault( 10 );
}
x++;
aFPArray[x] = new FunctionParameter( "xFast", FunctionParameter.NUMBER);
with( aFPArray[x] ) {
setName( "Fast Len" );
setLowerLimit( 1 );
setUpperLimit( 125 );
setDefault( 2 );
}
x++;
aFPArray[x] = new FunctionParameter( "xSlow", FunctionParameter.NUMBER);
with( aFPArray[x] ) {
setName( "Slow Len" );
setLowerLimit( 1 );
setUpperLimit( 125 );
setDefault( 30 );
}
x++;
aFPArray[x] = new FunctionParameter( "lColor", FunctionParameter.COLOR);
with( aFPArray[x] ) {
setName( "Line Color" );
setDefault( Color.teal );
}
x++;
aFPArray[x] = new FunctionParameter( "lThick", FunctionParameter.NUMBER);
with( aFPArray[x] ) {
setName( "Line Thickness" );
setLowerLimit( 1 );
setUpperLimit( 10 );
setDefault( 2 );
}
}
//== Main processing function
function main( Period, xFast, xSlow, lColor, lThick ) {
var x;
var nNoise;
var nSignal;
var nSmooth;
//script is initializing
if ( getBarState() == BARSTATE_ALLBARS ) {
return null;
}
if ( bInitialized == false ) {
setDefaultBarFgColor( lColor, 0 );
setDefaultBarThickness( lThick, 0 );
nPeriod = Math.round( Period );
nFast = 2 / ( xFast + 1 );
nSlow = 2 / ( xSlow + 1 );
setStudyTitle( "KAMA (" + nPeriod + ")" );
aAMA = new Array( nPeriod );
for ( x=0; x<nPeriod; x++ ) {
aAMA[x] = 0.0;
}
bInitialized = true;
}
//called on each new bar
if ( getBarState() == BARSTATE_NEWBAR ) {
aAMA.pop();
aAMA.unshift(0);
nBarCounter++;
}
if ( nBarCounter<nPeriod+1 ) {
aAMA[0] = close();
return;
}
nSignal = Math.abs( close() - close(-Period) );
x=0;
nNoise = 0;
while( x<nPeriod ) {
nNoise += Math.abs( close(-x)-close(-(x+1)) );
x++;
}
if ( nNoise==0 ) nNoise = 1.0;
nSmooth = Math.pow( ( nSignal/nNoise ) * ( nFast - nSlow ) + nSlow , 2 );
aAMA[0] = aAMA[1] + nSmooth * ( close() - aAMA[1] );
if ( aAMA[0]>aAMA[1] ) {
setBarFgColor( Color.green, 0 );
}
else {
setBarFgColor( Color.red, 0 );
}
if(close()>aAMA[0]){
setPriceBarColor(Color.RGB (144, 238, 144),Color.RGB (144, 238, 144),true,false)
}
if(close()<aAMA[0]){
setPriceBarColor(Color.red,Color.red,true,false)
}
return( aAMA[0] );
}
/*************************************************
SUPPORT FUNCTIONS
**************************************************/
//== gID function assigns unique identifier to graphic/text routines
function gID() {
grID ++;
return( grID );
}
HULL MA INDICATOR:
function preMain() {
setPriceStudy(true);
setStudyTitle("Hull MA");
setCursorLabelName("HMA", 0);
setDefaultBarStyle(PS_SOLID, 0);
setDefaultBarFgColor(Color.green, 0);
setDefaultBarThickness(1, 0);
setPlotType(PLOTTYPE_LINE, 0);
setColorPriceBars(true)
setDefaultPriceBarColor(Color.black);
var fp1 = new FunctionParameter("Length", FunctionParameter.NUMBER);
fp1.setLowerLimit(1);
fp1.setDefault(10);
var fp2 = new FunctionParameter("Offset", FunctionParameter.NUMBER);
fp2.setDefault(0);
var fp3 = new FunctionParameter("Source", FunctionParameter.STRING);
fp3.addOption("Close");
fp3.addOption("High");
fp3.addOption("Low");
fp3.addOption("Open");
fp3.addOption("HL/2");
fp3.addOption("HLC/3");
fp3.addOption("OHLC/4");
fp3.setDefault("Close");
}
var vMA1 = null;
var vMA2 = null;
var vValue = 0.0;
var aValue = null;
var HMA = null;
function main(Length,Offset,Source) {
var Length2 = Length/2;
var LengthSqrt = Math.round(Math.sqrt(Length));
var vSum = 0.0;
var cSum = 0.0;
if (vMA1 == null) vMA1 = new MAStudy(Length2, Offset, Source, MAStudy.WEIGHTED);
if (vMA2 == null) vMA2 = new MAStudy(Length, Offset, Source, MAStudy.WEIGHTED);
var vAvg1 = vMA1.getValue(MAStudy.MA);
var vAvg2 = vMA2.getValue(MAStudy.MA);
if(vAvg1 == null || vAvg2 == null)
return;
if (aValue == null) aValue = new Array(Length);
if (getBarState() == BARSTATE_NEWBAR) {
aValue.pop();
aValue.unshift(vValue);
}
vValue = (2*vAvg1)-vAvg2;
if(vValue == null) return;
aValue[0] = vValue;
if(aValue[LengthSqrt-1] == null) return;
for(var i = 0; i < LengthSqrt; i++) {
vSum += aValue[i]*(LengthSqrt-i);
cSum += (LengthSqrt-i);
}
HMA = (vSum/cSum);
if(close()>HMA){
setPriceBarColor(Color.green,Color.green,true,fals e)
}
if(close()<HMA){
setPriceBarColor(Color.red,Color.red,true,false)
}
return HMA;
}
Comment