Announcement

Collapse
No announcement yet.

Keltner Channel within BB

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

  • Keltner Channel within BB

    I am trying to place a condition that will fire each time BB is within the Keltner Channel. I am combining a script for Keltner Channel (which is working) with the conditions, however, the script with the conditions does not work. What am I missing? I am not good at writing scripts, but I am quite good at modifying existing scripts. Thank you kindly.



    var fpArray = new Array();
    var bInit = false;

    function preMain(){
    setPriceStudy(true);
    setShowCursorLabel(true);
    setShowTitleParameters(false);
    setStudyTitle("Keltner ATR Band & BB");
    setCursorLabelName("Up Band", 0);
    setPlotType(PLOTTYPE_LINE, 0);
    setDefaultBarFgColor(Color.red, 0);
    setCursorLabelName("Dn Band", 1);
    setPlotType(PLOTTYPE_LINE, 1);
    setColorPriceBars(true);
    setDefaultBarFgColor(Color.red, 1);
    var x = 0;
    fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
    with(fpArray[x++]) {
    setLowerLimit(1);
    setDefault(20);
    }
    fpArray[x] = new FunctionParameter("ATRMult", FunctionParameter.NUMBER);
    with(fpArray[x++]) {
    setLowerLimit(1);
    setDefault(2);
    }
    }

    var xKUp = null;
    var xKDn = null;

    function main(Length, ATRMult) {
    var nBarState = getBarState();
    var nKU = 0;
    var nKL = 0;
    if (nBarState == BARSTATE_ALLBARS) {
    if(Length == null) Length = 20;
    if(ATRMult == null) ATRMult = 2;
    }
    if (bInit == false) {
    xKUp = efsInternal("Calc_KeltnerBand", Length, ATRMult);
    xKDn = getSeries(xKUp, 1);
    bInit = true;
    }
    nKU = xKUp.getValue(0);
    nKL = xKDn.getValue(0);
    if (nKU == null || nKL == null) return;
    return new Array(nKU, nKL);
    }

    var bSecondInit = false;
    var xMA = null;
    var xATR = null;

    function Calc_KeltnerBand(Length, ATRMult) {
    var nResUp = 0;
    var nResDn = 0;
    var nMA = 0;
    var nATR = 0;
    if (bSecondInit == false) {
    xMA = sma(Length);
    xATR = atr(Length);
    bSecondInit = true;
    }
    nMA = xMA.getValue(0);
    nATR = xATR.getValue(0);
    if (nMA == null || nATR == null) return;
    nResUp = nMA + ATRMult * nATR;
    nResDn = nMA - ATRMult * nATR;
    return new Array(nResUp, nResDn);

    }

    function calculate() {

    var nResDn;
    var nResUp;




    If ((nResUp < upperBB(20,2)) && (nResDn > lowerBB(20,2))); {
    drawShape(Shape.DIAMOND, AboveBar1, Color.blue);
    Alert.playSound("buzz.wav");

    }




    return;
    }

  • #2
    Re: Keltner Channel within BB

    mzafka
    There are several issues in your script.
    1. The calculate function [and as a consequence the drawing and alert functions contained therein] is never executed as it is not called at any time from the main or the Calc_KeltnerBand functions
    2. In the calculate function the if in the conditional statement is incorrectly written If. JavaScript is a case sensitive language
    3. You inserted a semi-colon at the end of the afore mentioned conditional statement. In JavaScript semi-colons are statement separators hence in your specific example the commands included in the curly brackets that follow the conditional statement are separated from the conditional statement itself and therefore would execute regardless of the fact that the conditional statement is true or false
    4. In the calculate function you are declaring nResUp and nResDown as local variables but are not assigning any values to them. While variables with the same names do exist in the Calc_KeltnerBand function and a value is assigned to them in that function they are local to that function so they are not available to other functions unless you pass them through a call or you make them global.
    The simplest solution is to copy your entire conditional statement and related commands to be executed if the former is true and paste them in the main function. In doing this you also need to make the corrections to the errors explained at the above points 2 and 3 and replace nResUp and nResDn in your conditional statement with nKU and nKL which are in effect the values of the upper and lower Keltner [see enclosed image for an illustration of these changes]. Once you have done this you can delete the entire calculate function as it is of no further use.
    5. In preMain you have set the script to color the price bars but have omitted to define a default color for these.
    Lastly search the forum for the keyword squeeze and you will find several examples of this indicator as the topic has been covered before
    Alex




    Originally posted by mzafka
    I am trying to place a condition that will fire each time BB is within the Keltner Channel. I am combining a script for Keltner Channel (which is working) with the conditions, however, the script with the conditions does not work. What am I missing? I am not good at writing scripts, but I am quite good at modifying existing scripts. Thank you kindly.



    var fpArray = new Array();
    var bInit = false;

    function preMain(){
    setPriceStudy(true);
    setShowCursorLabel(true);
    setShowTitleParameters(false);
    setStudyTitle("Keltner ATR Band & BB");
    setCursorLabelName("Up Band", 0);
    setPlotType(PLOTTYPE_LINE, 0);
    setDefaultBarFgColor(Color.red, 0);
    setCursorLabelName("Dn Band", 1);
    setPlotType(PLOTTYPE_LINE, 1);
    setColorPriceBars(true);
    setDefaultBarFgColor(Color.red, 1);
    var x = 0;
    fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
    with(fpArray[x++]) {
    setLowerLimit(1);
    setDefault(20);
    }
    fpArray[x] = new FunctionParameter("ATRMult", FunctionParameter.NUMBER);
    with(fpArray[x++]) {
    setLowerLimit(1);
    setDefault(2);
    }
    }

    var xKUp = null;
    var xKDn = null;

    function main(Length, ATRMult) {
    var nBarState = getBarState();
    var nKU = 0;
    var nKL = 0;
    if (nBarState == BARSTATE_ALLBARS) {
    if(Length == null) Length = 20;
    if(ATRMult == null) ATRMult = 2;
    }
    if (bInit == false) {
    xKUp = efsInternal("Calc_KeltnerBand", Length, ATRMult);
    xKDn = getSeries(xKUp, 1);
    bInit = true;
    }
    nKU = xKUp.getValue(0);
    nKL = xKDn.getValue(0);
    if (nKU == null || nKL == null) return;
    return new Array(nKU, nKL);
    }

    var bSecondInit = false;
    var xMA = null;
    var xATR = null;

    function Calc_KeltnerBand(Length, ATRMult) {
    var nResUp = 0;
    var nResDn = 0;
    var nMA = 0;
    var nATR = 0;
    if (bSecondInit == false) {
    xMA = sma(Length);
    xATR = atr(Length);
    bSecondInit = true;
    }
    nMA = xMA.getValue(0);
    nATR = xATR.getValue(0);
    if (nMA == null || nATR == null) return;
    nResUp = nMA + ATRMult * nATR;
    nResDn = nMA - ATRMult * nATR;
    return new Array(nResUp, nResDn);

    }

    function calculate() {

    var nResDn;
    var nResUp;




    If ((nResUp < upperBB(20,2)) && (nResDn > lowerBB(20,2))); {
    drawShape(Shape.DIAMOND, AboveBar1, Color.blue);
    Alert.playSound("buzz.wav");

    }




    return;
    }

    Comment


    • #3
      Thank you Alex for the detailed explanation! I followed you instructions to the best of my ability, however, I still did something wrong. The Keltner channel shows up on the chart, however, the condition does not. Here is the modified script.


      var fpArray = new Array();
      var bInit = false;

      function preMain(){
      setPriceStudy(true);
      setShowCursorLabel(true);
      setShowTitleParameters(false);
      setStudyTitle("Keltner ATR Band & BB");
      setCursorLabelName("Up Band", 0);
      setPlotType(PLOTTYPE_LINE, 0);
      setDefaultBarFgColor(Color.red, 0);
      setCursorLabelName("Dn Band", 1);
      setPlotType(PLOTTYPE_LINE, 1);
      setColorPriceBars(false);
      //setDefaultPriceBarColor(Color.RGB(48,156,250));
      setDefaultBarFgColor(Color.red, 1);
      var x = 0;
      fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
      with(fpArray[x++]) {
      setLowerLimit(1);
      setDefault(13);
      }
      fpArray[x] = new FunctionParameter("ATRMult", FunctionParameter.NUMBER);
      with(fpArray[x++]) {
      setLowerLimit(1);
      setDefault(2);
      }
      }

      var xKUp = null;
      var xKDn = null;

      function main(Length, ATRMult) {
      var nBarState = getBarState();
      var nKU = 0;
      var nKL = 0;
      if (nBarState == BARSTATE_ALLBARS) {
      if(Length == null) Length = 20;
      if(ATRMult == null) ATRMult = 1.30;
      }
      if (bInit == false) {
      xKUp = efsInternal("Calc_KeltnerBand", Length, ATRMult);
      xKDn = getSeries(xKUp, 1);
      bInit = true;
      }
      nKU = xKUp.getValue(0);
      nKL = xKDn.getValue(0);
      if (nKU == null || nKL == null) return;
      return new Array(nKU, nKL);
      }

      var bSecondInit = false;
      var xMA = null;
      var xATR = null;

      function Calc_KeltnerBand(Length, ATRMult) {
      var nResUp = 0;
      var nResDn = 0;
      var nMA = 0;
      var nATR = 0;
      if (bSecondInit == false) {
      xMA = sma(Length);
      xATR = atr(Length);
      bSecondInit = true;
      }
      nMA = xMA.getValue(0);
      nATR = xATR.getValue(0);
      if (nMA == null || nATR == null) return;
      nResUp = nMA + ATRMult * nATR;
      nResDn = nMA - ATRMult * nATR;
      return new Array(nResUp, nResDn);



      if (nKU == null || nKL == null) return;




      if ((nKU > upperBB(20,2)) && (nKL < lowerBB(20,2))) {
      drawShape(Shape.DIAMOND, AboveBar1, Color.blue);
      Alert.playSound("buzz.wav");

      }




      return new Array(nKU, nKL);
      }

      Comment


      • #4
        mzafka
        You pasted the condition and related commands in the Calc_KeltnerBand function instead of in the main function as I indicated in my previous reply [and illustrated in the image enclosed with it]
        As an aside you should implement the same solution in your other thread in which you asked me a somewhat similar question
        Alex


        Originally posted by mzafka
        Thank you Alex for the detailed explanation! I followed you instructions to the best of my ability, however, I still did something wrong. The Keltner channel shows up on the chart, however, the condition does not. Here is the modified script.


        var fpArray = new Array();
        var bInit = false;

        function preMain(){
        setPriceStudy(true);
        setShowCursorLabel(true);
        setShowTitleParameters(false);
        setStudyTitle("Keltner ATR Band & BB");
        setCursorLabelName("Up Band", 0);
        setPlotType(PLOTTYPE_LINE, 0);
        setDefaultBarFgColor(Color.red, 0);
        setCursorLabelName("Dn Band", 1);
        setPlotType(PLOTTYPE_LINE, 1);
        setColorPriceBars(false);
        //setDefaultPriceBarColor(Color.RGB(48,156,250));
        setDefaultBarFgColor(Color.red, 1);
        var x = 0;
        fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
        with(fpArray[x++]) {
        setLowerLimit(1);
        setDefault(13);
        }
        fpArray[x] = new FunctionParameter("ATRMult", FunctionParameter.NUMBER);
        with(fpArray[x++]) {
        setLowerLimit(1);
        setDefault(2);
        }
        }

        var xKUp = null;
        var xKDn = null;

        function main(Length, ATRMult) {
        var nBarState = getBarState();
        var nKU = 0;
        var nKL = 0;
        if (nBarState == BARSTATE_ALLBARS) {
        if(Length == null) Length = 20;
        if(ATRMult == null) ATRMult = 1.30;
        }
        if (bInit == false) {
        xKUp = efsInternal("Calc_KeltnerBand", Length, ATRMult);
        xKDn = getSeries(xKUp, 1);
        bInit = true;
        }
        nKU = xKUp.getValue(0);
        nKL = xKDn.getValue(0);
        if (nKU == null || nKL == null) return;
        return new Array(nKU, nKL);
        }

        var bSecondInit = false;
        var xMA = null;
        var xATR = null;

        function Calc_KeltnerBand(Length, ATRMult) {
        var nResUp = 0;
        var nResDn = 0;
        var nMA = 0;
        var nATR = 0;
        if (bSecondInit == false) {
        xMA = sma(Length);
        xATR = atr(Length);
        bSecondInit = true;
        }
        nMA = xMA.getValue(0);
        nATR = xATR.getValue(0);
        if (nMA == null || nATR == null) return;
        nResUp = nMA + ATRMult * nATR;
        nResDn = nMA - ATRMult * nATR;
        return new Array(nResUp, nResDn);



        if (nKU == null || nKL == null) return;




        if ((nKU > upperBB(20,2)) && (nKL < lowerBB(20,2))) {
        drawShape(Shape.DIAMOND, AboveBar1, Color.blue);
        Alert.playSound("buzz.wav");

        }




        return new Array(nKU, nKL);
        }

        Comment


        • #5
          I missed that point Alex. Thank you Alex it works! Cheers and have a great day, Mario

          Comment


          • #6
            Mario
            You are welcome
            Alex


            Originally posted by mzafka
            I missed that point Alex. Thank you Alex it works! Cheers and have a great day, Mario

            Comment

            Working...
            X