Announcement

Collapse
No announcement yet.

Help adding a round-up/down element

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

  • Help adding a round-up/down element

    All

    Two questions...

    1. Is it possible to add a round-up and round-down element into the code?

    For example (using the E-mini market), if the upper channel reads 871.92 can the EFS round up to the nearest .25 increment and read 872.00?

    Same thing for the lower channel but rounded down i.e., 871.92 would read 871.75

    ----------------------------------------------

    2. Along with the above rounding is it possible to also add a preset increase to the upper channel and a preset decrease to the lower channel?

    Example, if the upper channel is supposed to read 872.00 could there be a place in the code I could adjust an "Add .25 or .75" so the display would read 872.25 or 872.75, respectively?

    Same for the lower channel except subtracting the preset amount.

    In volatile markets this preset could be ratcheted up.
    ------------------------------------------------

    /************************************************** *******
    Alexis C. Montenegro © July 2003
    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.
    ************************************************** ********/

    var vMA = null;
    vUpper = null;
    vLower = null;

    function preMain() {
    setPriceStudy(true);
    setStudyTitle("PointsChannel");
    setCursorLabelName("Short", 0);
    setCursorLabelName("MA", 1);
    setCursorLabelName("Long", 2);
    setDefaultBarFgColor(Color.red, 0);
    setDefaultBarFgColor(Color.black, 1);
    setDefaultBarFgColor(Color.lime, 2);
    setPlotType(PLOTTYPE_LINE,0);
    setPlotType(PLOTTYPE_LINE,1);
    setPlotType(PLOTTYPE_LINE,2);
    setDefaultBarThickness(1,0);
    setDefaultBarThickness(1,1);
    setDefaultBarThickness(1,2);
    checkVersion(1,"http://share.esignal.com/ContentRoot/ACM%20Test/Formulas/basicMA&Channel.efs"); //Comment this line out if modifying the code

    var fp1 = new FunctionParameter("Length", FunctionParameter.NUMBER);
    fp1.setLowerLimit(1);
    fp1.setDefault(1); //Edit this value to set a new default

    var fp2 = new FunctionParameter("Offset", FunctionParameter.NUMBER);
    fp2.setDefault(0); //Edit this value to set a new default

    var fp3 = new FunctionParameter("Source", FunctionParameter.STRING);
    fp3.setName("Source");
    fp3.addOption("Close");
    fp3.addOption("High");
    fp3.addOption("Low");
    fp3.addOption("Open");
    fp3.addOption("HL/2");
    fp3.addOption("HLC/3");
    fp3.addOption("OHLC/4");
    fp3.setDefault("HL/2"); //Edit this value to set a new default

    var fp4 = new FunctionParameter("Type", FunctionParameter.STRING);
    fp4.setName("Type");
    fp4.addOption("MAStudy.SIMPLE");
    fp4.addOption("MAStudy.EXPONENTIAL");
    fp4.addOption("MAStudy.WEIGHTED");
    fp4.addOption("MAStudy.VOLUMEWEIGHTED");
    fp4.setDefault("MAStudy.SIMPLE"); //Edit this value to set a new default

    var fp5 = new FunctionParameter("Value", FunctionParameter.STRING);
    fp5.setName("Value");
    fp5.addOption("Points");
    fp5.addOption("Percent");
    fp5.setDefault("Points");

    var fp6 = new FunctionParameter("Points", FunctionParameter.NUMBER);
    fp6.setName("Points");
    fp6.setLowerLimit(-2);
    fp6.setDefault(.5);

    var fp7 = new FunctionParameter("RangeLength", FunctionParameter.NUMBER);

    fp7.setLowerLimit(1);

    fp7.setDefault(3); //Edit this value to set a new default

    }

    function main(Length,Offset,Source,Type,Value,Points,RangeL ength) {

    if (vMA == null) vMA = new MAStudy(Length, Offset, Source, eval(Type));
    var PointsEFS = efsExternal("maofhlrange.efs", RangeLength);

    /*****************************************
    Insert your code following this text block
    Use vMA.getValue(MAStudy.MA) for your code
    ******************************************/

    if(Value=="Points"){

    var vUpper = vMA.getValue(MAStudy.MA)+PointsEFS.getValue(-1);

    var vLower = vMA.getValue(MAStudy.MA)-PointsEFS.getValue(-1);

    }

    else if(Value == "Percent"){

    var vUpper = vMA.getValue(MAStudy.MA)*(1+(PointsEFS.getValue(-1)/100));

    var vLower = vMA.getValue(MAStudy.MA)*(1-(PointsEFS.getValue(-0)/100));

    }


    return new Array (vUpper,vMA.getValue(MAStudy.MA),vLower);
    }

  • #2
    I'm sure there are other ways to accomplish this, but this is the code I've created to handle your needs.

    Just drop the function at the end of your code (or in a LIBRARY) and call it as needed, like..

    PHP Code:
    var Round2 0.25;

    var 
    RoundedPrice RndQTR(1close(), 0);  //  This rounds UP
    var RoundedPrice RndQTR(-1close(), 0);  //  This rounds DOWN 
    Here is the function ...

    PHP Code:
    function RndQTR(DirectionPriceOffset) {

      var 
    Whole Math.floor(Price);
      var 
    Fraction Price-Whole;
      
    //  Round to nearest QUARTER
     
    if (Round2 == 0.25) { 
      if (
    Direction == 0) {
        if ( (
    Fraction >= 0.625) && (Fraction <= 0.875)) {  Fraction 0.75
        } else if ( (
    Fraction >= 0.375) && (Fraction 0.625)) {  Fraction 0.5
        } else if ( (
    Fraction >= 0.125) && (Fraction 0.375)) {  Fraction 0.25
        } else if ( (
    Fraction 0.125)) {  Fraction 0.0;
        } else if ( (
    Fraction 0.875)) {  Fraction 1.0;   }
       return ((
    Whole+Fraction)+Offset);
      }
      if (
    Direction 0) {
        if (
    Fraction 0.76) {  Fraction 1.0
        } else if (
    Fraction 0.51) {  Fraction 0.75
        } else if (
    Fraction 0.26) {  Fraction 0.5
        } else if (
    Fraction >= 0.0) {  Fraction 0.25;  }
       return ((
    Whole+Fraction)+Offset);
      }
      if (
    Direction 0) {
        if (
    Fraction 0.24) {  Fraction 0.0
        } else if (
    Fraction 0.49) {  Fraction 0.25
        } else if (
    Fraction 0.74) {  Fraction 0.5
        } else if (
    Fraction <= 0.99) {  Fraction 0.75;  }
       return ((
    Whole+Fraction)-Offset);
      }
     } else

    //  Round to nearest 1/10th of a whole pt
     
    if (Round2 == 0.1) { 
      if (
    Direction >= 0) {
       return (
    Price.toFixed(1)*1);  //((Whole+Fraction)+Offset).toFixed(2);
      
    }
      if (
    Direction 0) {
       return (
    Price.toFixed(1)*1);  //((Whole+Fraction)+Offset).toFixed(2);
      
    }
     } else

    //  Round to nearest PENNY
     
    if (Round2 == 0.01) { 
      if (
    Direction >= 0) {
       return (
    Price.toFixed(2)*1);  //((Whole+Fraction)+Offset).toFixed(2);
      
    }
      if (
    Direction 0) {
       return (
    Price.toFixed(2)*1);  //((Whole+Fraction)+Offset).toFixed(2);
      
    }
     } else {
       return 
    Price;
     }

    With regards to your second request, you simply need to add two new INPUTS in PREMAIN for your offsets, then ADD or SUBTRACT these values from the RETURN items.
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #3
      Let me see if I can explain this...

      x = 545.55

      x/0.25 = 2182.2

      2182.2%1 = 2182

      2182*0.25 = 545.5 which rounds down to the nearest 0.25

      to round up, just add 0.25

      fyi, % is the modulo function, %1 strips the decimal part off.

      so

      (x/0.25)%1*0.25 is the round down, i belevie modulo is done before *, other wise use a few more parentheses

      Comment


      • #4
        Where to place call function?

        You guys need to keep in mind that you are dealing with a complete idiot.

        I think I placed the function correctly (please check) but where do I place the call function to get it to actually work?

        PHP:



        var Round2 = 0.25;



        var RoundedPrice = RndQTR(1, close(), 0); // This rounds UP

        var RoundedPrice = RndQTR(-1, close(), 0); // This rounds DOWN

        --------------------------------------------


        /************************************************** *******
        Alexis C. Montenegro © July 2003
        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.
        ************************************************** ********/

        var vMA = null;
        vUpper = null;
        vLower = null;

        function preMain() {
        setPriceStudy(true);
        setStudyTitle("PointsChannel");
        setCursorLabelName("Short", 0);
        setCursorLabelName("MA", 1);
        setCursorLabelName("Long", 2);
        setDefaultBarFgColor(Color.red, 0);
        setDefaultBarFgColor(Color.black, 1);
        setDefaultBarFgColor(Color.lime, 2);
        setPlotType(PLOTTYPE_LINE,0);
        setPlotType(PLOTTYPE_LINE,1);
        setPlotType(PLOTTYPE_LINE,2);
        setDefaultBarThickness(1,0);
        setDefaultBarThickness(1,1);
        setDefaultBarThickness(1,2);
        checkVersion(1,"http://share.esignal.com/ContentRoot/ACM%20Test/Formulas/basicMA&Channel.efs"); //Comment this line out if modifying the code

        var fp1 = new FunctionParameter("Length", FunctionParameter.NUMBER);
        fp1.setLowerLimit(1);
        fp1.setDefault(1); //Edit this value to set a new default

        var fp2 = new FunctionParameter("Offset", FunctionParameter.NUMBER);
        fp2.setDefault(0); //Edit this value to set a new default

        var fp3 = new FunctionParameter("Source", FunctionParameter.STRING);
        fp3.setName("Source");
        fp3.addOption("Close");
        fp3.addOption("High");
        fp3.addOption("Low");
        fp3.addOption("Open");
        fp3.addOption("HL/2");
        fp3.addOption("HLC/3");
        fp3.addOption("OHLC/4");
        fp3.setDefault("HL/2"); //Edit this value to set a new default

        var fp4 = new FunctionParameter("Type", FunctionParameter.STRING);
        fp4.setName("Type");
        fp4.addOption("MAStudy.SIMPLE");
        fp4.addOption("MAStudy.EXPONENTIAL");
        fp4.addOption("MAStudy.WEIGHTED");
        fp4.addOption("MAStudy.VOLUMEWEIGHTED");
        fp4.setDefault("MAStudy.SIMPLE"); //Edit this value to set a new default

        var fp5 = new FunctionParameter("Value", FunctionParameter.STRING);
        fp5.setName("Value");
        fp5.addOption("Points");
        fp5.addOption("Percent");
        fp5.setDefault("Points");

        var fp6 = new FunctionParameter("Points", FunctionParameter.NUMBER);
        fp6.setName("Points");
        fp6.setLowerLimit(-2);
        fp6.setDefault(.5);

        var fp7 = new FunctionParameter("RangeLength", FunctionParameter.NUMBER);

        fp7.setLowerLimit(1);

        fp7.setDefault(3); //Edit this value to set a new default


        }

        function main(Length,Offset,Source,Type,Value,Points,RangeL ength) {

        if (vMA == null) vMA = new MAStudy(Length, Offset, Source, eval(Type));
        var PointsEFS = efsExternal("maofhlrange.efs", RangeLength);


        /*****************************************
        Insert your code following this text block
        Use vMA.getValue(MAStudy.MA) for your code
        ******************************************/

        if(Value=="Points"){

        var vUpper = vMA.getValue(MAStudy.MA)+PointsEFS.getValue(-1);

        var vLower = vMA.getValue(MAStudy.MA)-PointsEFS.getValue(-1);

        }

        else if(Value == "Percent"){

        var vUpper = vMA.getValue(MAStudy.MA)*(1+(PointsEFS.getValue(-1)/100));

        var vLower = vMA.getValue(MAStudy.MA)*(1-(PointsEFS.getValue(-0)/100));

        function RndQTR(Direction, Price, Offset) {



        var Whole = Math.floor(Price);

        var Fraction = Price-Whole;



        // Round to nearest QUARTER

        if (Round2 == 0.25) {

        if (Direction == 0) {

        if ( (Fraction >= 0.625) && (Fraction <= 0.875)) { Fraction = 0.75;

        } else if ( (Fraction >= 0.375) && (Fraction < 0.625)) { Fraction = 0.5;

        } else if ( (Fraction >= 0.125) && (Fraction < 0.375)) { Fraction = 0.25;

        } else if ( (Fraction < 0.125)) { Fraction = 0.0;

        } else if ( (Fraction > 0.875)) { Fraction = 1.0; }

        return ((Whole+Fraction)+Offset);

        }

        if (Direction > 0) {

        if (Fraction > 0.76) { Fraction = 1.0;

        } else if (Fraction > 0.51) { Fraction = 0.75;

        } else if (Fraction > 0.26) { Fraction = 0.5;

        } else if (Fraction >= 0.0) { Fraction = 0.25; }

        return ((Whole+Fraction)+Offset);

        }

        if (Direction < 0) {

        if (Fraction < 0.24) { Fraction = 0.0;

        } else if (Fraction < 0.49) { Fraction = 0.25;

        } else if (Fraction < 0.74) { Fraction = 0.5;

        } else if (Fraction <= 0.99) { Fraction = 0.75; }

        return ((Whole+Fraction)-Offset);

        }

        } else



        // Round to nearest 1/10th of a whole pt

        if (Round2 == 0.1) {

        if (Direction >= 0) {

        return (Price.toFixed(1)*1); //((Whole+Fraction)+Offset).toFixed(2);

        }

        if (Direction < 0) {

        return (Price.toFixed(1)*1); //((Whole+Fraction)+Offset).toFixed(2);

        }

        } else



        // Round to nearest PENNY

        if (Round2 == 0.01) {

        if (Direction >= 0) {

        return (Price.toFixed(2)*1); //((Whole+Fraction)+Offset).toFixed(2);

        }

        if (Direction < 0) {

        return (Price.toFixed(2)*1); //((Whole+Fraction)+Offset).toFixed(2);

        }

        } else {

        return Price;

        }

        }

        }


        return new Array (vUpper,vMA.getValue(MAStudy.MA),vLower);
        }

        Comment


        • #5
          RndDown = (Price/0.25)%1*0.25;

          RndUp = (Price/0.25)%1*0.25 +0.25;

          is all you really need in the Function main() section.

          If you arnt trying to round a variable named Price, just change Price to the variable you want to round up or down.

          I know you may not have a clue what I mean, sorry.

          Comment


          • #6
            Thanks for trying

            David and Brad

            Thanks for trying but I cant seem to get it to work.

            Comment


            • #7
              edit the file to look like this near the end of the file.

              }

              vUpper= (vUpper/0.25)%1*0.25;//add this line

              vLower= (vLower/0.25)%1*0.25;//and this one

              //leave ALL of the rest the same

              return new Array (vUpper,vMA.getValue(MAStudy.MA),vLower);
              }

              Comment


              • #8
                David, Thanks for another attempt.

                But I still can't get it to work. Would you mind posting the entire code as its supposed to be?

                Comment

                Working...
                X