Announcement

Collapse
No announcement yet.

Help with loop

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

  • Help with loop

    Hi, i have been trying to understand loops and trying to get the efs to draw a line from the highest high in 'n' number of bars looking backwards. This works but for some reason it is also drawing a line underneath almost like it should be on a 'non price study'.

    Anyway here is my script and also i have shown image or the errorClick image for larger version

Name:	Efs error.png
Views:	1
Size:	38.3 KB
ID:	246824:

    function preMain(){
    setPriceStudy(true);
    setShowCursorLabel(false);
    }

    function main() {

    var Source = high(); // these are used as parameters for study
    var Length = 100; // this is bars to look backwards

    var vValue = Source.getValue(0);
    var barIndex = 0;
    for(var i = 0; i < Length; i++) {
    vValue = Math.max(vValue, Source.getValue(-i));
    if(vValue == Source.getValue(-i)){
    barIndex = getCurrentBarIndex()-i;
    drawLineRelative(barIndex, high(barIndex), 0, high(barIndex), PS_SOLID, 1, Color.red, "line1");

    }
    }
    return barIndex;
    }

  • #2
    Referring to Values in a loop

    Okay i have figured out how to use loops. However, i am having difficulty in accessing values within the loop i created e.g.

    Code:
     var Source2 = high();
     var vValue2   = Source2.getValue(15);
    
    
    for (h = -15; h < 0; h++)       {  
         vValue2 = Math.max(vValue2, Source2.getValue(-h)); 
       
     if (vValue2 == Source2.getValue(-h))        { 
            barIndex2 = getCurrentBarIndex()-h; 
           }
    }

    Now lets assume this is all fine and it returns me the values (in this case the Highest high looking forward 15 bars from entry point). However, i now wish to make conditions for the loop to process (all for backtesting purposes) such as:

    - I want to return (vValue2 +1) e.g. 'vValue2' has been returned as the highest high (15 bars) and now i want to know the value of 'vValue2 +1 bar' even if this is not the highest bar.
    - Is there a way for the loop only to return values if multiple conditions are met such as:
    i. Return the first Highest high of the 15 bars (lets say 'h') IF the bar ('h' +1) of this return value has a LOW which is less than the LOW of ('h') && ('h'+2), ('h'+3), ('h'+4), all have High less than ('h').

    Basically i want to add multiple IF conditions to start a loop?? Or am i supposed to add the IF conditions after a loop has returned its values???

    - I am not sure if the things i require are possible or must i use loop after loop to get the desired results.
    Last edited by kingmins; 09-25-2012, 06:33 AM.

    Comment


    • #3
      This works but for some reason it is also drawing a line underneath almost like it should be on a 'non price study'.
      The blue line at the bottom of your chart is the "return barIndex;" returned as a plot on the chart. The barIndex plot scale takes precedence over the price plot and since the barIndex values are negative and much less than the price values the price scale is squeezed into a small range.

      If you just want to see the values of "barIndex" in the cursor window without plotting them just replace the
      PHP Code:
      return barIndex
      to
      PHP Code:
      return barIndex+""
      Last edited by waynecd; 09-30-2012, 06:46 AM.

      Comment


      • #4
        Strictly using your code and approach:
        PHP Code:
         var Source2 high();
         var 
        vValue2   Source2.getValue(15); 
        Here .getValue(15) is looking at fifteen bars in the future.
        Change it to:
        PHP Code:
         var Source2 high();
         var 
        vValue2   Source2.getValue(-15);
         var 
        vValue3 Source2.getValue(-14); 
        Now vValue2 has the high of the bar 15 bars back from bar 0 (current bar is bar 0) and vValue3 has the high of the next bar (14 bars back).

        Maybe a better approach would be something like the following:
        PHP Code:
        //I recommend you study eSignal functions found at:
        //http://kb.esignal.com/display/2/index.aspx?c=12&cpc=ULwO0A442oKs512Q04X5j0UupP4SveI6dt2WJi7&cid=4&cat=&catURL=&r=0.856125354766846
        debugClear();
        function 
        preMain(){
            
        setPriceStudy(true);
        }
        var 
        barIndex2,vIdx3;
        function 
        main(){
            var 
        vHHV;
            
        vHHV=hhv(15high()); //returns the highest high for the last 15 bars
            
        for (= -15<= 0h++)       {  
                
        //Note: There could be more than one bar with a high of vHHV.  The loop will return values for the last bar with a high = vHHV
                
        if (high(h) == vHHV){ //to see if the bar(-h) equals the highest high to get it's index
                    
        barIndex2 h//index of the last bar with a high equal to the highest high of the last 15 bars
                    
        vIdx3=h+1//this is the index of the bar after the bar with high = vHHV
                
        }
            }
            
        //now "barIndex2" contains the bar index for the last bar with a high equal to the highest high value over the last 15 bars
            //"vIdx3" contains the bar index for the bar after "barIndex2"
            
        return new Array(high(0),barIndex2+"",vIdx3+"",vHHV+"");

        Last edited by waynecd; 09-30-2012, 07:50 AM.

        Comment

        Working...
        X