Announcement

Collapse
No announcement yet.

Help needed in plotting the Bollinger Bands for Hull moving average

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

  • Help needed in plotting the Bollinger Bands for Hull moving average

    I have an efs which computes the Hull Moving Average. Now i want to take the moving average as the input and plot the bollinger bands for the Moving average. I modified the HullMA efs and added a bit of my code. But the result is not as intended, please help me. The code in green is the addition done by me.

    //-------------------------------------

    var fpArray = new Array();

    function preMain() {
    setPriceStudy(true);
    setStudyTitle("HullMABBs");
    setCursorLabelName("HMABBs", 0);
    //setDefaultBarStyle(PS_SOLID, 0);
    //setDefaultBarFgColor(Color.blue, 0);
    //setPlotType(PLOTTYPE_LINE, 0);
    //setDefaultBarThickness(1, 0);
    var x=0;
    fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
    with(fpArray[x++]){
    setLowerLimit(1);
    setDefault(10);
    }
    fpArray[x] = new FunctionParameter("Source", FunctionParameter.STRING);
    with(fpArray[x++]){
    addOption("open");
    addOption("high");
    addOption("low");
    addOption("close");
    addOption("hl2");
    addOption("hlc3");
    addOption("ohlc4");
    setDefault("close");
    }
    fpArray[x] = new FunctionParameter("Symbol", FunctionParameter.STRING);
    with(fpArray[x++]){
    setDefault();
    }
    fpArray[x] = new FunctionParameter("Interval", FunctionParameter.STRING);
    with(fpArray[x++]){
    setDefault();
    }
    fpArray[x] = new FunctionParameter("Offset", FunctionParameter.NUMBER);
    with(fpArray[x++]){
    setDefault(0);
    }
    fpArray[x] = new FunctionParameter("Params", FunctionParameter.BOOLEAN);
    with(fpArray[x++]){
    setName("Show Parameters");
    setDefault(false);
    }
    fpArray[x] = new FunctionParameter("K", FunctionParameter.NUMBER);
    with(fpArray[x++]){
    setLowerLimit(1);
    setDefault(2);
    }

    }

    var bInit = false;
    var vSymbol = null;
    var vSource = null;
    var xHMA = null;



    function main(Length,Source,Symbol,Interval,Offset,Params,K ) {
    var avg = 0;
    var sigma = 0;
    var uplevel=0;
    var bottomlevel=0;


    if(getCurrentBarCount()<Length*2) return;
    if(bInit == false){
    if(Symbol == null) Symbol = getSymbol();
    if(Interval == null) Interval = getInterval();
    vSymbol = Symbol+","+Interval;
    vSource = eval(Source);
    xHMA = getSeries(wma(Math.round(Math.sqrt(Length)),efsInt ernal("calcHMA",Length,Offset,vSource(sym(vSymbol) ))));
    setShowTitleParameters(eval(Params));
    bInit = true;
    avg = 0;
    sigma = 0;
    for(var i=0 ; i < Length; i++){
    avg = avg+xHMA.getValue(0-i);
    }
    avg = avg/Length;
    for(var i=0;i<Length ; i++){
    sigma = sigma+(avg-xHMA.getValue(0-i))^2;
    }
    sigma = Math.sqrt(sigma/Length);
    uplevel = xHMA.getValue(0)+K*sigma;
    bottomlevel = xHMA.getValue(0)-K*sigma;

    }

    return new Array(uplevel, xHMA, bottomlevel);

    }

    var xAvg1 = null;
    var xAvg2 = null;

    function calcHMA(length,offset,source){
    if(xAvg1==null) xAvg1 = offsetSeries(wma(length/2,source),offset);
    if(xAvg2==null) xAvg2 = offsetSeries(wma(length,source),offset);
    var Avg1 = xAvg1.getValue(0);
    var Avg2 = xAvg2.getValue(0);
    if(Avg1 == null || Avg2 == null) return;
    return (2*Avg1)-Avg2;
    }
    //------------------------------------

  • #2
    indus383
    You have a number of errors in your script.
    - efsInt ernal(… ) in line 73 should be one word ie efsInternal(…)
    - The following lines of code
    avg = 0;
    sigma = 0;
    for(var i=0 ; i < Length; i++){
    avg = avg+xHMA.getValue(0-i);
    }
    avg = avg/Length;
    for(var i=0;i<Length ; i++){
    sigma = sigma+(avg-xHMA.getValue(0-i))^2;
    }
    sigma = Math.sqrt(sigma/Length);
    uplevel = xHMA.getValue(0)+K*sigma;
    bottomlevel = xHMA.getValue(0)-K*sigma;

    should be placed outside of the bInit routine else they will be executed once only when the formula is loaded (or reloaded)
    - The syntax of the following line of code
    sigma = sigma+(avg-xHMA.getValue(0-i))^2;
    is invalid if as I am assuming you want to elevate to the power of 2. To do this you need to use the pow() method of the Math Object (see this article in the EFS KnowledgeBase for information regarding the Math Object). If OTOH that is not what you are trying to do then you need to explain it as it is not apparent.
    Once you resolve these issues you will get the formula to return values.
    As an aside this line of code
    avg = avg+xHMA.getValue(0-i);
    can also be written in the following way
    avg += xHMA.getValue(-i);
    and the same applies to the sum in your second for loop
    Lastly I would suggest that you learn to use debug statements in your script as those will help you in determining where the issues are
    Alex


    Originally posted by indus383 View Post
    I have an efs which computes the Hull Moving Average. Now i want to take the moving average as the input and plot the bollinger bands for the Moving average. I modified the HullMA efs and added a bit of my code. But the result is not as intended, please help me. The code in green is the addition done by me.

    //-------------------------------------

    var fpArray = new Array();

    function preMain() {
    setPriceStudy(true);
    setStudyTitle("HullMABBs");
    setCursorLabelName("HMABBs", 0);
    //setDefaultBarStyle(PS_SOLID, 0);
    //setDefaultBarFgColor(Color.blue, 0);
    //setPlotType(PLOTTYPE_LINE, 0);
    //setDefaultBarThickness(1, 0);
    var x=0;
    fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
    with(fpArray[x++]){
    setLowerLimit(1);
    setDefault(10);
    }
    fpArray[x] = new FunctionParameter("Source", FunctionParameter.STRING);
    with(fpArray[x++]){
    addOption("open");
    addOption("high");
    addOption("low");
    addOption("close");
    addOption("hl2");
    addOption("hlc3");
    addOption("ohlc4");
    setDefault("close");
    }
    fpArray[x] = new FunctionParameter("Symbol", FunctionParameter.STRING);
    with(fpArray[x++]){
    setDefault();
    }
    fpArray[x] = new FunctionParameter("Interval", FunctionParameter.STRING);
    with(fpArray[x++]){
    setDefault();
    }
    fpArray[x] = new FunctionParameter("Offset", FunctionParameter.NUMBER);
    with(fpArray[x++]){
    setDefault(0);
    }
    fpArray[x] = new FunctionParameter("Params", FunctionParameter.BOOLEAN);
    with(fpArray[x++]){
    setName("Show Parameters");
    setDefault(false);
    }
    fpArray[x] = new FunctionParameter("K", FunctionParameter.NUMBER);
    with(fpArray[x++]){
    setLowerLimit(1);
    setDefault(2);
    }

    }

    var bInit = false;
    var vSymbol = null;
    var vSource = null;
    var xHMA = null;



    function main(Length,Source,Symbol,Interval,Offset,Params,K ) {
    var avg = 0;
    var sigma = 0;
    var uplevel=0;
    var bottomlevel=0;


    if(getCurrentBarCount()<Length*2) return;
    if(bInit == false){
    if(Symbol == null) Symbol = getSymbol();
    if(Interval == null) Interval = getInterval();
    vSymbol = Symbol+","+Interval;
    vSource = eval(Source);
    xHMA = getSeries(wma(Math.round(Math.sqrt(Length)),efsInt ernal("calcHMA",Length,Offset,vSource(sym(vSymbol) ))));
    setShowTitleParameters(eval(Params));
    bInit = true;
    avg = 0;
    sigma = 0;
    for(var i=0 ; i < Length; i++){
    avg = avg+xHMA.getValue(0-i);
    }
    avg = avg/Length;
    for(var i=0;i<Length ; i++){
    sigma = sigma+(avg-xHMA.getValue(0-i))^2;
    }
    sigma = Math.sqrt(sigma/Length);
    uplevel = xHMA.getValue(0)+K*sigma;
    bottomlevel = xHMA.getValue(0)-K*sigma;

    }

    return new Array(uplevel, xHMA, bottomlevel);

    }

    var xAvg1 = null;
    var xAvg2 = null;

    function calcHMA(length,offset,source){
    if(xAvg1==null) xAvg1 = offsetSeries(wma(length/2,source),offset);
    if(xAvg2==null) xAvg2 = offsetSeries(wma(length,source),offset);
    var Avg1 = xAvg1.getValue(0);
    var Avg2 = xAvg2.getValue(0);
    if(Avg1 == null || Avg2 == null) return;
    return (2*Avg1)-Avg2;
    }
    //------------------------------------

    Comment


    • #3
      Thanks a lot Alexis...

      Comment


      • #4
        indus383
        You are welcome
        Alex


        Originally posted by indus383 View Post
        Thanks a lot Alexis...

        Comment

        Working...
        X