Announcement

Collapse
No announcement yet.

Help with candlestick body range test

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

  • Help with candlestick body range test

    I'm new to this, but want to know what might be wrong with this script. Most of the time it seems to work, but then i get a false positive: a candlestick is designated as have a wider body then the previous 3, when in fact one of the previous 3 has an equal body height. This error does not occur for body heights greater or less than.

    Any help detecting the error appreciated

    // Test for whether a candlestick has wider body range than the previous three candlesticks
    // This test is performed on whichever candlestick the cursor is placed

    var vLastAlert = -1;
    var o;
    var c;
    var BodyHeight;
    var prevBodyHeight1;
    var prevBodyHeight2;
    var prevBodyHeight3;

    function preMain() {

    setPriceStudy(true);
    setStudyTitle("WRB");
    setCursorLabelName("WRB =", 0);

    }

    function main() {

    c=close();
    o=open();
    BodyHeight=Math.abs(c-o);
    prevBodyHeight1=Math.abs(close(-1)-open(-1));
    prevBodyHeight2=Math.abs(close(-2)-open(-2));
    prevBodyHeight3=Math.abs(close(-3)-open(-3));

    onAction1();

    if (
    BodyHeight>prevBodyHeight1 &&

    BodyHeight>prevBodyHeight2 &&

    BodyHeight>prevBodyHeight3
    )


    return "YES";


    }

    function postMain() {
    /**
    * The postMain() function is called only once, when the EFS is no longer used for
    * the current symbol (ie, symbol change, chart closing, or application shutdown).
    */
    }

    function onAction1() {
    vLastAlert = 1;
    }
    Les

  • #2
    Les
    The enclosed revision of the efs should fix the error you are seeing.
    For information on the function formatPriceNumber() I used in the efs click here.
    If you want to see why you were getting that error replace in your formula the line
    return "YES";
    with
    return "YES"+" "+BodyHeight;
    and you should see that the values sometimes may have extra decimals.
    Alex

    PHP Code:
    var o;
    var 
    c;
    var 
    BodyHeight;
    var 
    prevBodyHeight1;
    var 
    prevBodyHeight2;
    var 
    prevBodyHeight3;

    function 
    preMain() {

        
    setPriceStudy(true);
        
    setStudyTitle("WRB");
        
    setCursorLabelName("WRB ="0);
    }

    function 
    main() {

        
    c=close(0);
        
    o=open(0);
        
    BodyHeight=formatPriceNumber(Math.abs(c-o));
        
    prevBodyHeight1=formatPriceNumber(Math.abs(close(-1)-open(-1)));
        
    prevBodyHeight2=formatPriceNumber(Math.abs(close(-2)-open(-2)));
        
    prevBodyHeight3=formatPriceNumber(Math.abs(close(-3)-open(-3)));


        if (
    BodyHeight>prevBodyHeight1 &&
            
    BodyHeight>prevBodyHeight2 &&
            
    BodyHeight>prevBodyHeight3)

        return 
    "YES";

    Comment


    • #3
      Many thanks for your help on this. Seems to be working ok.
      Les

      Comment


      • #4
        Les
        You are most welcome.
        FWIW there is also another solution that uses toFixed() to set the decimal precision (in the example below it is set to 2 decimals)
        Alex

        PHP Code:
        var o;
        var 
        c;
        var 
        BodyHeight;
        var 
        prevBodyHeight1;
        var 
        prevBodyHeight2;
        var 
        prevBodyHeight3;

        function 
        preMain() {

            
        setPriceStudy(true);
            
        setStudyTitle("WRB");
            
        setCursorLabelName("WRB ="0);
        }

        function 
        main() {

            
        c=close(0);
            
        o=open(0);
            
        BodyHeight=Math.abs(c-o).toFixed(2);
            
        prevBodyHeight1=Math.abs(close(-1)-open(-1)).toFixed(2);
            
        prevBodyHeight2=Math.abs(close(-2)-open(-2)).toFixed(2);
            
        prevBodyHeight3=Math.abs(close(-3)-open(-3)).toFixed(2);


            if (
        BodyHeight>prevBodyHeight1 &&
                
        BodyHeight>prevBodyHeight2 &&
                
        BodyHeight>prevBodyHeight3)

            return 
        "YES";

        Comment

        Working...
        X