TrendDetectionIndex.efs
EFSLibrary - Discussion Board
File Name: TrendDetectionIndex.efs
Description:
Trend Detection Index
Formula Parameters:
- Length: 20
- Thickness: 2
- Line Color: Green
- Display Cursor Labels: True
Notes:
The related article is copyrighted material. If you are not a subscriber of Stocks & Commodities, please visit www.traders.com.
Download File:
TrendDetectionIndex.efs
EFS Code:
/********************************* Provided By: eSignal (Copyright c eSignal), a division of Interactive Data Corporation. 2008. All rights reserved. This sample eSignal Formula Script (EFS) is for educational purposes only and may be modified and saved under a new file name. eSignal is not responsible for the functionality once modified. eSignal reserves the right to modify and overwrite this EFS file with each new release. Description: Trend Detection Index Version: 1.0 01/12/2009 Formula Parameters: Default: Length 20 Thickness 2 Line Color Green Display Cursor Labels True Notes: The related article is copyrighted material. If you are not a subscriber of Stocks & Commodities, please visit www.traders.com. **********************************/ var fpArray = new Array(); var bInit = false; function preMain() { setPriceStudy(false); setShowCursorLabel(false); setShowTitleParameters( false ); setStudyTitle("Trend Detection Index"); setCursorLabelName("Position", 0); setDefaultBarFgColor(Color.green, 0); setPlotType(PLOTTYPE_LINE, 0); setDefaultBarThickness(2, 0); setStudyMax(1.1); setStudyMin(-1.1); askForInput(); var x=0; fpArray[x] = new FunctionParameter("LineColor", FunctionParameter.COLOR); with(fpArray[x++]){ setName("Line Color"); setDefault(Color.green); } fpArray[x] = new FunctionParameter("ViewValue", FunctionParameter.BOOLEAN); with(fpArray[x++]){ setName("Display Cursor Labels"); setDefault(false); } fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER); with(fpArray[x++]){ setLowerLimit(1); setDefault(20); } fpArray[x] = new FunctionParameter("Thickness", FunctionParameter.NUMBER); with(fpArray[x++]){ setName("Line Thickness"); setLowerLimit(1); setDefault(2); } } var xMom = null; var xMomAbs = null; var nMomSum_Prev = 0; var nMomSum = 0; var nMktPos_Prev = 0; var nMktPos = 0; var nTDL_Prev = 0; var nTDL = 0; function main(Length, Thickness, LineColor, ViewValue) { var nLength2 = 2 * Length; var nMomSumAbs = 0; var nMomAbsSum = 0; var nMomAbsSum2 = 0; var nState = getBarState(); if ( bInit == false ) { setDefaultBarFgColor(LineColor, 0); setDefaultBarThickness(Thickness, 0); setShowCursorLabel(ViewValue); xMom = efsInternal("Calc_Mom", Length); xMomAbs = efsInternal("Calc_MomAbs", xMom); bInit = true; } if (getCurrentBarCount() < Length) return; if (nState == BARSTATE_NEWBAR) { nMomSum_Prev = nMomSum; nMktPos_Prev = nMktPos; nTDL_Prev = nTDL; } nMomSum = Summation1(Length); nMomSumAbs = Math.abs(nMomSum); nMomAbsSum = Summation2(Length); nMomAbsSum2 = Summation2(nLength2); nTDL = nMomSumAbs - (nMomAbsSum2 - nMomAbsSum); if (nTDL_Prev > 0 ) { if( nMomSum_Prev > 0) nMktPos = 1; else nMktPos = (-1); } else { nMktPos = nMktPos_Prev; } return nMktPos; } function Calc_MomAbs(xMom){ var nRes = 0; nRes = Math.abs( xMom.getValue(0)); if (nRes == null) nRes = 1; return nRes; } function Calc_Mom(nLength){ var nRes = 0; nRes = close(0) - close(-nLength); if (nRes == null) nRes = 1; return nRes; } function Summation1(nLength){ var nRes = 0; if (xMom.getValue(-nLength) == null) return 1; for (var i = nLength - 1; i >= 0; i--) { nRes += xMom.getValue(-i); } return nRes; } function Summation2(nLength){ var nRes = 0; if (xMomAbs.getValue(-nLength) == null) return 1; for (var i = nLength - 1; i >= 0; i--) { nRes += xMomAbs.getValue(-i); } return nRes; }