PDA

View Full Version : KAMA and HULL MA - is anybody able to covert to NINJA script?


Akros
06-19-2006, 05:00 AM
/************************************************** *******************
Title:Kaufman Adaptive Moving Average for eSignal 7.x
By: Chris D. Kryza (Divergence Software, Inc.)
Email: c.kryza@gte.net (mailto:c.kryza@gte.net), ckryza@sr-analyst.com (mailto:ckryza@sr-analyst.com)
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;
}

NinjaTrader_Ray
06-19-2006, 06:41 AM
Hi Akros,

We have implemented the Hull MA with our upcoming NT6. Hopefully someone here may be kind enough to convert the KAMA. If we have some time, we will do it for NT6.

Ray

NinjaTrader_Ray
06-20-2006, 01:26 AM
How about this?

HMA - Orange
KAMA - Green

Ray

Akros
06-20-2006, 11:48 PM
Great! You are working well

Well done!

Anynews for the Quote board?

NinjaTrader_Ray
06-21-2006, 12:11 AM
QB will be out with V6 which we anticipate should be out in beta in August.

ljoe
06-16-2007, 10:51 PM
Does Ninja have a HMA with color for trend up (Blue) and trend down (Red)?

Thanks

NinjaTrader_Ray
06-17-2007, 08:30 AM
By HMA if you mean Hull Moving Average then yes, we support it but without trend based coloring.

ljoe
06-17-2007, 10:14 AM
Thanks, what would it take to add the trending color to the indicator? I am not a programmer so if someone could accomplish this it would be great.

NinjaTrader_Ray
06-17-2007, 07:42 PM
Maybe someone will be kind enough to help you out. If not, you can contact one of our consultants.

http://www.ninjatrader.com/webnew/partners_onlinetrading_NinjaScript.htm

whitmark
06-18-2007, 03:07 PM
Here you go ljoe, feel free to modify the colors to suit . . . enjoy!

Regards,

Whitmark

ljoe
06-18-2007, 10:22 PM
Thank you very much for the indicator.

ljoe
06-19-2007, 06:23 PM
I tried to compile the cs file but keep getting an error "error on generating Indicator." Any suggestion?

Thanks

whitmark
06-20-2007, 04:45 AM
ljoe . . . I've pm'd you with my contact info. Please contact me directly and I will be happy to help.

Regards,

Whitmark

ljoe
06-20-2007, 09:29 PM
Just wanted to thank Whitmark for all of his assistance and provide me the cs file with the indicator. Finally figured out that one of my templates was causing all the problemd with compiling the indicator. Deleted template and Whitmark's color indicator compiled with out any problem. Indicator is working great, thanks again to Whitmark.

Larry

whitmark
06-20-2007, 10:08 PM
My pleasure, and thanks for posting a follow-up!

NinjaTrader_Ray
06-21-2007, 06:55 AM
ljoe,

What do you mean by template? A chart template?