Announcement

Collapse
No announcement yet.

Average Daily Range Bands by Chris Kryza

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Average Daily Range Bands by Chris Kryza

    Wanted to know if I could adapt CK's ADR bands to plot weekly rather than daily. I've muddled through several attempts by simply changing "day" in all its forms to "week" but it doesn't work.

    Normally the bands are set to look back 5 days and compute the average but I'd like to adapt them to look back 5 weeks and plot for the weekly average for the week ahead instead.

    Is this possible ? . . . I'm a little clueless when it comes to EFS however much I read and try.

    Thank you for any help . . . EFS below.


    //External Variables
    var grID = 0;
    var nBarCounter = 0;
    var nHHigh = null;
    var nLLow = null;
    var nRTime = null;
    var nFirstBar = null;
    var sSymbol = null;
    var nDailyRange = null;
    var nRawTime = null;
    var nRawTime_1 = null;
    var aFPArray = new Array();
    var bInitialized = false;


    //== PreMain function required by eSignal to set things up
    function preMain() {
    var x;

    setPriceStudy(true);
    setStudyTitle("ADR Bands");
    setCursorLabelName("HiBand", 0 );
    setCursorLabelName("MidBand", 1 );
    setCursorLabelName("LoBand", 2 );
    setCursorLabelName("ADR", 3 );
    setDefaultBarFgColor( Color.green, 0 );
    setDefaultBarFgColor( Color.navy, 1 );
    setDefaultBarFgColor( Color.red, 2 );
    setDefaultBarFgColor( Color.black, 3 );
    setShowTitleParameters( false );

    //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( "fADRPeriod", FunctionParameter.NUMBER);
    with( aFPArray[x] ) {
    setName( "ADR Period" );
    setLowerLimit( 1 );
    setUpperLimit( 125 );
    setDefault( 5 );
    }
    x++;
    aFPArray[x] = new FunctionParameter( "fUBColor", FunctionParameter.COLOR);
    with( aFPArray[x] ) {
    setName( "Upper Band Color" );
    setDefault( Color.green );
    }
    x++;
    aFPArray[x] = new FunctionParameter( "fMColor", FunctionParameter.COLOR);
    with( aFPArray[x] ) {
    setName( "Mid Band Color" );
    setDefault( Color.navy );
    }
    x++;
    aFPArray[x] = new FunctionParameter( "fLBColor", FunctionParameter.COLOR);
    with( aFPArray[x] ) {
    setName( "Lower Band Color" );
    setDefault( Color.red );
    }
    x++;
    aFPArray[x] = new FunctionParameter( "fThick", FunctionParameter.NUMBER);
    with( aFPArray[x] ) {
    setName( "Line Thickness" );
    setLowerLimit( 1 );
    setUpperLimit( 10 );
    setDefault( 2 );
    }


    }
    var bInit = false;
    var xRange = null;

    var bCurrentDay = false;

    function main(xStart,xEnd,xInterval,xDisplay) {

    if(getBarState()==BARSTATE_ALLBARS){
    bCurrentDay=false;
    }
    if (bCurrentDay == false && getDay(0) != getDay(-1)){
    var xTime = getValue("time");
    var xDate = new Date();
    var sTime = (xTime.getMonth()+1+"/"+xTime.getDate()+"/"+xTime.getFullYear());
    var sToday = (xDate.getMonth()+1+"/"+xDate.getDate()+"/"+xDate.getFullYear());
    if ( sTime == sToday ) {
    bCurrentDay = true;
    }
    }
    if (bCurrentDay==false){
    return;
    }


    if(bInit==false){
    xRange = efsInternal("calcOR",xStart,xEnd,xDisplay,inv(xInt erval));
    bInit=true;
    }

    return new Array(getSeries(xRange,0),getSeries(xRange,1));
    }


    //== Main processing function
    function main( fADRPeriod, fUBColor, fMColor, fLBColor, fThick ) {
    var x;
    var nMid, nLower, nUpper;

    //script is initializing
    if ( getBarState() == BARSTATE_ALLBARS ) {
    return null;
    }

    if ( !isIntraday() ) return;

    if ( bInitialized == false ) {

    sSymbol = getSymbol() + ",D";

    sString = " (Using "+Math.round( fADRPeriod )+"-day ADR.)";
    setStudyTitle( "ADR Bands" + sString );

    setDefaultBarFgColor( fUBColor, 0 );
    setDefaultBarFgColor( fLBColor, 2 );
    setDefaultBarFgColor( fMColor, 1 );

    setDefaultBarThickness( fThick, 0 );
    setDefaultBarThickness( fThick, 1 );
    setDefaultBarThickness( fThick, 2 );

    bInitialized = true;
    }

    //called on each new bar
    if ( getBarState() == BARSTATE_NEWBAR ) {

    nBarCounter++;

    nRTime = getValue( "rawtime" );

    if( nRTime == null )
    return;


    nRawTime = Math.floor( nRTime / RawTime.DAY );

    if ( nRawTime != nRawTime_1 && isIntraday() ) {
    nHHigh = -9999999999999.0;
    nLLow = 9999999999999.0;
    nDailyRange = computeRange( sSymbol, Math.round( fADRPeriod ), nRTime );
    }

    nRawTime_1 = nRawTime;

    }

    if ( nDailyRange==null ) return;

    nHHigh = Math.max( nHHigh, high(0) );
    nLLow = Math.min( nLLow, low(0) );

    nLower = nHHigh - nDailyRange;
    nUpper = nLLow + nDailyRange;
    nMid = ( nLower + nUpper ) / 2;

    //return the values to be plotted and display the current ADR
    //reading in the Cursor Window
    return new Array( nUpper, nMid, nLower, ""+nDailyRange );

    }


    /*************************************************
    SUPPORT FUNCTIONS
    **************************************************/

    //== Compute the Average Daily Range for selected period
    function computeRange( sSymb, nDays, nTime ) {
    var x=0;
    var nTmp=0;
    var nH, nL;
    var vIndex = null;

    //get index to offset (minus one)
    vIndex = getFirstBarIndexOfDay( nTime, sSymb )-1;

    //collect data for the correct number of days
    while( x<nDays ) {
    nH = getValueAbsolute( "high", vIndex, sSymb );
    nL = getValueAbsolute( "low", vIndex, sSymb );
    if ( nH==null || nL==null ) return(null);
    nTmp += (nH-nL);
    vIndex--;
    x++;
    }

    //return the average of the daily ranges
    return( nTmp/nDays );
    }

    //== gID function assigns unique identifier to graphic/text routines
    function gID() {
    grID ++;
    return( grID );
    }

  • #2
    The light bulb came on and I figured it out . . . always the way !!

    Comment

    Working...
    X