Announcement

Collapse
No announcement yet.

Higher Lows -- User Selectable

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

  • Higher Lows -- User Selectable

    I'm going nutzo trying to figure out why this simple code does not do what I want it to do.
    This is similar to several other codes on the bulletin board that count the number of times you have successive higher lows in place -- with one exception... The number of higher lows needs to be user-selectable.
    Here's my code. My idea is to use a global variable that is a running tally of the number of successive higher lows. When that is above a user-selectable threshold, an alert is posted.
    Seems simple enough, and a good use of global Variable should do, and this would not be CPU intensive as it it is simple incrementing, rather than using arrays or successive lookbacks.
    Any suggestions as to why this does not work as anticipated would be greatly appreciated.
    Attached Files

  • #2
    I found a minor error in my coding ( a mis-placed parenthesis).
    Here's the finished EFS that I wanted. It will alert when there are a user-selectable number of higher lows, or lower highs. And I didn't even need global variables. Mostly , I needed that little piece of code that checks for BARSTATE_ALLBARS.


    var fpArray = new Array();
    var CtHigherLows;
    var CtLowerHighs;

    function preMain() {

    setPriceStudy(true);
    setStudyTitle("Successive New HiLo's");
    setCursorLabelName("Successive New HiLo's", 0);
    askForInput();
    setComputeOnClose();

    var x=0;
    fpArray[x] = new FunctionParameter("nHigherLows", FunctionParameter.NUMBER);
    with(fpArray[x++]){
    setLowerLimit(2);
    setDefault(5);
    }
    fpArray[x] = new FunctionParameter("nLowerHighs", FunctionParameter.NUMBER);
    with(fpArray[x++]){
    setLowerLimit(2);
    setDefault(5);
    }


    }


    function main(nHigherLows, nLowerHighs) {

    // initialize upon first loading formula -- and only then
    if(getBarState() == BARSTATE_ALLBARS) {
    CtHigherLows = 0;
    CtLowerHighs = 0;
    return null;
    }


    // First look for new higher lows in a row
    if (low(0)>=low(-1)) {
    CtHigherLows = CtHigherLows + 1;
    if (CtHigherLows >= nHigherLows) {
    drawTextRelative(0,high(),"HL",Color.red,null,Text .BOTTOM|Text.CENTER|Text.BOLD,"Arial",6,"HL"+ getCurrentBarIndex());
    Alert.addToList(null, getSymbol()+" = Higher Lows", Color.red, Color.red);
    Alert.playSound( "Tap2.wav" );
    }
    }
    else if (low( 0)<low(-1)) { // Made a lower low, so re-set the counter to zero.
    CtHigherLows = 0;
    }


    // Now look for new lower highs in a row
    if (
    high(0)<=high(-1)) {
    CtLowerHighs = CtLowerHighs + 1;
    if (CtLowerHighs>= nHigherLows) {
    drawTextRelative(0,low(),"LH",Color.lime,null,Text .TOP|Text.CENTER|Text.BOLD,"Arial",6,"LH"+ getCurrentBarIndex());
    Alert.addToList(null, getSymbol()+" = Lower Highs", Color.lime, Color.lime);
    Alert.playSound( "Tap2.wav" );
    }
    }
    else if (high(0)>high(-1)) { // Made a higher high, so re-set the counter to zero.
    CtLowerHighs = 0;
    }


    return ;
    }

    Comment


    • #3
      John

      And I didn't even need global variables
      As you already found out you do not need to store the counter values in a Global Value unless you are planning to use those same values in another study that is running concurrently.

      Mostly I needed that little piece of code that checks for BARSTATE_ALLBARS
      If you want you could simplify it even further and assign the initial value to the external variable when you declare it. This way you don't need to initialize the variable inside main (unless of course you have specific reasons for doing that).
      Another thing you may want to review is the tagName. In real time getCurrentBarIndex() is always 0 on the current bar which means that while the script is running in real time only the last graphic object with that tagName will be drawn. If you want to retain all the graphic objects drawn in real time you may want to replace getCurrentBarIndex() with getCurrentBarCount() which returns a different value on every bar.
      Lastly if you would also like to see what the counts were on historical bars you may want to add the counter values to the return statement converting them to strings so that they display only in the Cursor Window. This way as you scroll the mouse the Cursor Window will display the values of the counters at that time
      In the enclosed script I implemented some of these changes including one to the else conditions
      Alex


      PHP Code:
      var fpArray = new Array();
      var 
      CtHigherLows=0;//set to 0 when declaring
      var CtLowerHighs=0;//set to 0 when declaring
       
      function preMain() {
          
      setPriceStudy(true);
          
      setStudyTitle("Successive New HiLo's");
          
      setCursorLabelName("Successive New HiLo's",0);
          
      setCursorLabelName("Successive New LoHi's",1);//added label
          
      setDefaultBarFgColor(Color.red,0);//added color
          
      setDefaultBarFgColor(Color.blue,1);//added color
          
      askForInput();
          
      setComputeOnClose();
       
          var 
      x=0;
          
      fpArray[x] = new FunctionParameter("nHigherLows"FunctionParameter.NUMBER);
          
      with(fpArray[x++]){
          
      setLowerLimit(2); 
          
      setDefault(5);
          }
          
      fpArray[x] = new FunctionParameter("nLowerHighs"FunctionParameter.NUMBER);
          
      with(fpArray[x++]){
          
      setLowerLimit(2); 
          
      setDefault(5);
          }
      }
       
      function 
      main(nHigherLowsnLowerHighs) {
       
          
      //removed BARSTATE_ALLBARS condition
       
          
      if (low(0)>=low(-1)) {
              
      CtHigherLows CtHigherLows 1;
              if (
      CtHigherLows >= nHigherLows) {
                  
      drawTextRelative(0,high(),"HL",Color.red,null,Text.BOTTOM|Text.CENTER|Text.BOLD,
                  
      "Arial",6,"HL"getCurrentBarCount());//changed to getCurrentBarCount() to draw all real time text
                  
      Alert.addToList(nullgetSymbol()+" = Higher Lows"Color.redColor.red); 
                  
      Alert.playSound"Tap2.wav" );
              }
          } else {
      //removed if(...) condition
              
      CtHigherLows 0;
          }
       
          if (
      high(0)<=high(-1)) {
              
      CtLowerHighs CtLowerHighs 1;
              if (
      CtLowerHighs>= nLowerHighs) {
                  
      drawTextRelative(0,low(),"LH",Color.lime,null,Text.TOP|Text.CENTER|Text.BOLD,
                  
      "Arial",6,"LH"getCurrentBarCount());//changed to getCurrentbarCount() to draw all real time text
                  
      Alert.addToList(nullgetSymbol()+" = Lower Highs"Color.limeColor.lime); 
                  
      Alert.playSound"Tap2.wav" );
              }
          } else {
      //removed if(...) condition 
              
      CtLowerHighs 0;
          }
       
          return new Array (
      CtHigherLows+"",CtLowerHighs+"");//values returned as strings display in Cursor Window

      Comment


      • #4
        This is an excellent Higher low, Higher High program! I was wondering if someone could help add another dimention which is as follows:

        The Lower Highs would only appear if ema(9)>ema(18) which is an uptrend

        and

        The Higher Lows would only appear if ema(9)<ema(18) which is a downtrend

        It would also be very nice if the ema numbers would appear on the fparray so that they could be easily adjusted without going into the code every time.

        Any help on this would be appreciated sincerely,

        Mike

        Comment

        Working...
        X