// Downloaded From https://www.WiseStockTrader.com
/*********************************************************
Alexis C. Montenegro © April 2005                         
Use and/or modify this code freely. If you redistribute it
please include this and/or any other comment blocks and a 
description of any changes you make.                      
**********************************************************/

//This efs plots the indicator developed by Andrew Abraham 
//in the Trading the Trend article of TASC September 1998  

var fpArray = new Array();

function preMain(){
    setPriceStudy(true);
    setStudyTitle("Trend Trader");
    setCursorLabelName("Limit")
    setDefaultBarFgColor(Color.red,0);
    setColorPriceBars(true);
    setDefaultPriceBarColor(Color.black);
    var x=0;
    fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);		
        setDefault(21);
    }
    fpArray[x] = new FunctionParameter("Multiplier", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0.01);		
        setDefault(3);
    }
	fpArray[x] = new FunctionParameter("Interval", FunctionParameter.STRING);
	with(fpArray[x++]){
        setDefault();
    }
    fpArray[x] = new FunctionParameter("Params", FunctionParameter.BOOLEAN);
	with(fpArray[x++]){
        setName("Show Parameters");
        setDefault(false);
    }
}

var bInit = false;
var xInterval = null
var avgTR = null;
var highestC = null;
var lowestC = null;
var xClose= null;
var Signal1 = null;

function main(Length,Multiplier,Interval,Params){
    if(bInit==false){
        if(Interval==null) Interval = getInterval();
        xInterval = Interval+"";
        avgTR      = wma(Length,atr(1,inv(xInterval)));
        highestC   = upperDonchian(Length,close(inv(xInterval)));
        lowestC    = lowerDonchian(Length,close(inv(xInterval)));
        xClose = close(inv(xInterval));
        setShowTitleParameters(eval(Params));
        bInit=true;
    }
    var Close1    = xClose.getValue(-1);
    var highestC1 = highestC.getValue(-1);
    var lowestC1  = lowestC.getValue(-1);
    var avgTR1    = avgTR.getValue(-1);
    if(Close1 == null || highestC1 == null || lowestC1 == null || avgTR1 == null) return;
    if(getBarStateInterval(xInterval)==BARSTATE_NEWBAR){
        var hiLimit1 = highestC1-(avgTR1*Multiplier);
        var loLimit1 = lowestC1+(avgTR1*Multiplier);
        
        if(Close1 > hiLimit1 && Close1 > loLimit1) Signal1 = hiLimit1;
        if(Close1 < loLimit1 && Close1 < hiLimit1) Signal1 = loLimit1;    
    }
    if(Signal1 == null) return;
    if(Close1 > Signal1) setPriceBarColor(Color.blue);
    if(Close1 < Signal1) setPriceBarColor(Color.red);
    return Signal1;
}