Announcement

Collapse
No announcement yet.

EFS2 function to return bar index from highest() or lowest() ?

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

  • EFS2 function to return bar index from highest() or lowest() ?

    The lowest() and highest() functions are nice editions to EFS2. However, it would be nice if there were complimentary functions where, instead of returning the highest or lowest number in a series, I could know the INDEX of the highest or lowest number.

    For what I'm doing, I don't care what the actual high or low number is. I just want to initiate an action at the bar index where that event has occurred. Right now, it looks like I've got to do things the hard way: find the highest/lowest value in a range, and then start all over again in a loop to find out which bar index has tht value.

    Steve

  • #2
    Looking over the docs, I see a catchall function called dsBarsSince() that I could play with to try and get what I want, but it seems like overkill. When one finds the highest() or lowest() value in a range, at that very point in time, the function knows what index it's on in the series.

    Just to give a tangible example, I'm thinking about finding the highest and lowest value in a bar range and drawing a line on an indicator chart connecting those points in the chart. For that, I need to know the bar indices of those high and low values. I can do this "the old fashioned way" by doing the looping through the range myself but was hoping the EFS2 library was versatile enough to do this for me.

    Comment


    • #3
      SteveH,

      The nice thing about efs2 is that you could write a function that does exactly what you want, and stick it in a library and use it just like the one you mentioned.

      However, if you were to simply look at the dsUtilities Library, you may find the dsSwingHighBar() and dsSwingLowBar() functions will work for you.

      Comment


      • #4
        SteveH
        Not sure what "old fashioned way" means but as Steve said you can very easily write a function that will return the bar index and that can be used any time you want with a syntax similar to highest().
        Save the enclosed script as example.efsLib in the FunctionLibrary folder.

        PHP Code:
        function highestBarNdx(Length,Source) {
            var 
        vValue   Source.getValue(0);
            var 
        barIndex 0;
            for(var 
        0Lengthi++) {
                
        vValue Math.max(vValueSource.getValue(-i));
                if(
        vValue == Source.getValue(-i)){
                    
        barIndex getCurrentBarIndex()-i;
                }
            }
            return 
        barIndex;

        Then run the following efs on any chart and it will write the bar index of the highest value in the last 50 bars and draw a line from that bar. The result is shown in the image below.

        PHP Code:
        function preMain(){
            
        setPriceStudy(true);
            
        setShowCursorLabel(false);
        }

        var 
        Library addLibrary("example.efsLib");//call the library

        function main(){

            var 
        hhvBI Library.highestBarNdx(50,high());//find the bar index
            
        drawTextRelative(0high(), hhvBIColor.bluenullText.BOTTOM|Text.BOLD"Arial"11"hhvBI");
            
        drawLineRelative(hhvBIhigh(hhvBI), 0high(hhvBI), PS_SOLID2Color.red"line1");

            return;

        If you want you can also very easily determine the bar index of the highest bar on a different interval. Set the chart for example to 1 minute and then replace Library.highestBarNdx(50,high()); with Library.highestBarNdx(50,high(inv(5))); and it will return the bar index on the 5 min chart.
        Alex

        Comment


        • #5
          Alexis, stevehare, thanks for your replies.

          "Old fashioned way" means I have to write what I want instead of expecting it to be a part of the basic libraries that eSignal provides. [sorry for the dry humor]

          I will just write my own version of highest() and lowest() where the function returns an array containing the highest/lowest value and the bar index from which it occurred. No biggie.

          Steve

          Comment

          Working...
          X