Announcement

Collapse
No announcement yet.

Test of Price "Excursion"

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

  • Test of Price "Excursion"

    Please see the attached chart in the next post.
    I'm writing a script that will show three things.
    1. Each 30-min interval where the price crosses long or short is noted. Price level is pre-determined.
    2. When higher low is made, note level of previous lowest price excursion below the pre-defined level. Continue tracking "excursion" until a higher-high is observed.
    3. Note price levels between previous lows and newer highs which would hit a trailing stop to optimize the trailing stop levels.

    I'm trying to run this to obtain statistical levels comparing various intervals throughout the day (and night with forex).

    I'm new to loops in efs and was wondering if these would be the best manner to run portions of this code.
    Any help you could give would be sincerely appreciated.
    Kirk
    Last edited by zeller4; 02-21-2008, 09:01 PM.

  • #2
    Price Excursion

    attached chart
    Attached Files
    Last edited by zeller4; 02-21-2008, 08:58 PM.

    Comment


    • #3
      here's a new chart with efs
      PHP Code:
      function preMain() {
          
      setPriceStudy(true);
          
      setStudyTitle("Excursion");
          
      setCursorLabelName("vExcursion"0);
          
      setDefaultBarFgColor(Color.green0);
          
          
      setCursorLabelName("vLongBot"1);
          
      setDefaultBarStyle(PS_SOLID1);
          
      setDefaultBarFgColor(Color.blue1);
          
      setDefaultBarThickness(21);
          
      setPlotType(PLOTTYPE_FLATLINES1);
          
          
      setCursorLabelName("vShortTop"2);
          
      setDefaultBarStyle(PS_SOLID2);
          
      setDefaultBarFgColor(Color.red2);
          
      setDefaultBarThickness(22);
          
      setPlotType(PLOTTYPE_FLATLINES2);

          
      setCursorLabelName("nCounter"3);
          
      setDefaultBarFgColor(Color.green3);
          
          
      setCursorLabelName("vStart"4);
          
      setDefaultBarStyle(PS_DASH4);
          
      setDefaultBarFgColor(Color.black4);
          
      setDefaultBarThickness(24);
          
      setPlotType(PLOTTYPE_FLATLINES4);

          
      }
      var 
      bInit false;
      var 
      xA0 null;     // 1-Day Peak series
      var nCounter 0;
      var 
      vFlag 0;
      var 
      vHigh null;
      var 
      vLow null;
      var 
      vExcursion 0;

      function 
      main() {

      if( 
      bInit == false) {
          
      xA0 efsInternal("calcPeak"1);// from APE.efs
          
      bInit true;
          }
      if(
      getCurrentBarIndex()==){
              if( (
      minute(0) % 5) == 0  && vFlag == 0)
              {
              
      vFlag 1;
              
              }
          }    
          if(
      vHigh == null || vLow == null || vStart == null) {
          var 
      vHigh high(inv(5));//iInterval));   
          
      var vLow low(inv(5));//iInterval));  
          
      var vStart close(-1,inv(30));//iInterval));
      }
      //var vLongBot    = rnd(vStart + iOffset1,  iDecimals, 2) ; 
      var vLongBot    rnd(vStart 1.1,  22) ; 
      var 
      vShortTop    rnd(vStart 1.1,  22) ; 
          
          var 
      nLength 7;
          var 
      curHigh high()
          var 
      curLow low()
          if (
      curHigh == null || curLow == null)
              return;
          var 
      pastHighs high(0, -nLength);
          if (
      pastHighs == null)
              return;
          var 
      pastLows low(0, -nLength);
          if (
      pastLows == null)
              return;
              
          var 
      curRange = (curHigh curLow);
          
           
      drawTextRelative(2,+vLongBot,+vLongBot.toFixed(2),Color.blue,null,Text.BOLD Text.VCENTER,"Arial",12,"vLongBot");
      drawTextRelative(2,+vShortTop,+vShortTop.toFixed(2),Color.red,null,Text.BOLD Text.VCENTER,"Arial",12,"vShortTop");

         if( 
      getBarState() == BARSTATE_NEWBAR){
          if( 
      open(0) <= vLongBot &&
              
      high(0) >= vLongBot )
          {
          
      nCounter 0;
          
      vFlag 1;//long condition
          
      vExcursion curHigh vLongBot;
          }
          
      // not sure how to start the counter to record until vExcursion is max due to lower high
          //then record max counter and max vExcursion
          
      nCounter;
          for (
      030; ++i) {
              if( 
      curHigh pastHighs)  {
              
      vExcursion curHigh vLongBot;
              
      nCounter 1;
              }
              else if( 
      curHigh pastHighs
              
      ){
                  
      vExcursion prevHigh vLongBot;
                  
              }
          
          }


      return new Array( 
      vExcursionvLongBotvShortTopnCountervStart);

      }


      var 
      xHighest null;
      var 
      xLowest null;

      function 
      calcPeak(nDay) {// not sure how to use this from APE.efs
          
      if(xHighest==nullxHighest highest(nDayhigh());
          if(
      xLowest==nullxLowest lowest(nDaylow());
          var 
      nO open(-nDay+1);
          if (
      nO == null) return null;
          
          var 
      nH Math.abs((xHighest.getValue(0) - nO) / nO)*100;
          var 
      nL Math.abs((xLowest.getValue(0)  - nO) / nO)*100;
          
      debugClear() ;

      debugPrintln("nDay  "+nDay );//
      debugPrintln("nL  "+nL );//
      debugPrintln("nH  "+nH );//
      debugPrintln("nO  "+nO );//
      debugPrintln("xLowest.getValue(0)  "+xLowest.getValue(0) );//
      debugPrintln("xHighest.getValue(0)  "+xHighest.getValue(0) );//

          
      return Math.max(nHnL);
      }
          }
          
      // rnd function - round to iDecimals places
      function rnd(valueiDecimals ) {  

      value =  value Math.pow(10iDecimals);

          return 
      Math.round(valueiDecimals) / Math.pow(10iDecimals);

      any help on the counter and recording of max vExcursion will be much appreciated.
      Attached Files

      Comment


      • #4
        Originally posted by zeller4
        PHP Code:
        function main() {

        if( 
        bInit == false) {
            
        xA0 efsInternal("calcPeak"1);// from APE.efs
            
        bInit true;
            }

        //  this code only runs on the CURRENT BAR IN REAL TIME
        if(getCurrentBarIndex()==){
                if( (
        minute(0) % 5) == 0  && vFlag == 0)
                {
                
        vFlag 1;
                
                }
            }    
            if(
        vHigh == null || vLow == null || vStart == null) {
            var 
        vHigh high(inv(5));//iInterval));   
            
        var vLow low(inv(5));//iInterval));  
            
        var vStart close(-1,inv(30));//iInterval));
        }  // End if getCurrentBarIndex == 0

        //var vLongBot    = rnd(vStart + iOffset1,  iDecimals, 2) ; 
        var vLongBot    rnd(vStart 1.1,  22) ; 
        var 
        vShortTop    rnd(vStart 1.1,  22) ; 
            
            var 
        nLength 7;
            var 
        curHigh high()
            var 
        curLow low()
            if (
        curHigh == null || curLow == null)
                return;
            var 
        pastHighs high(0, -nLength);
            if (
        pastHighs == null)
                return;
            var 
        pastLows low(0, -nLength);
            if (
        pastLows == null)
                return;
                
            var 
        curRange = (curHigh curLow);
            
             
        drawTextRelative(2,+vLongBot,+vLongBot.toFixed(2),Color.blue,null,Text.BOLD Text.VCENTER,"Arial",12,"vLongBot");
        drawTextRelative(2,+vShortTop,+vShortTop.toFixed(2),Color.red,null,Text.BOLD Text.VCENTER,"Arial",12,"vShortTop");

        //  This code only runs ONCE, when a new bar is formed!!
           
        if( getBarState() == BARSTATE_NEWBAR){
            if( 
        open(0) <= vLongBot &&
                
        high(0) >= vLongBot )
            {
            
        nCounter 0;
            
        vFlag 1;//long condition
            
        vExcursion curHigh vLongBot;
            }
            
        // not sure how to start the counter to record until vExcursion is max due to lower high
            //then record max counter and max vExcursion
            
        nCounter;
            for (
        030; ++i) {
                if( 
        curHigh pastHighs)  {
                
        vExcursion curHigh vLongBot;
                
        nCounter 1;
                }
                else if( 
        curHigh pastHighs
                
        ){
                    
        vExcursion prevHigh vLongBot;
                    
                }
            
            }
         } 
        //  Possible missing } for NEWBAR state

        return new Array( vExcursionvLongBotvShortTopnCountervStart);

        }


        var 
        xHighest null;
        var 
        xLowest null;

        function 
        calcPeak(nDay) {// not sure how to use this from APE.efs
            
        if(xHighest==nullxHighest highest(nDayhigh());
            if(
        xLowest==nullxLowest lowest(nDaylow());
            var 
        nO open(-nDay+1);
            if (
        nO == null) return null;
            
            var 
        nH Math.abs((xHighest.getValue(0) - nO) / nO)*100;
            var 
        nL Math.abs((xLowest.getValue(0)  - nO) / nO)*100;
            
        debugClear() ;

        debugPrintln("nDay  "+nDay );//
        debugPrintln("nL  "+nL );//
        debugPrintln("nH  "+nH );//
        debugPrintln("nO  "+nO );//
        debugPrintln("xLowest.getValue(0)  "+xLowest.getValue(0) );//
        debugPrintln("xHighest.getValue(0)  "+xHighest.getValue(0) );//

            
        return Math.max(nHnL);
        }
            }
            
        // rnd function - round to iDecimals places
        function rnd(valueiDecimals ) {  

        value =  value Math.pow(10iDecimals);

            return 
        Math.round(valueiDecimals) / Math.pow(10iDecimals);

        What I would suggest is using a CONTROL VARIABLE to determine WHEN/IF the instance of your BREACH has ocured.

        I use variables that rotate from 1, 0 to -1 and simply test for these values as needed

        PHP Code:

        var BreachDirectionControl 0;  //  no breach direction

        function main() {

         if( 
        getBarState() == BARSTATE_NEWBAR){
           if (
        high() < high(-1)) { 
             
        //  End Excursion - this checks the two previously formed bars for a lower high formation.
             
        Total_Excursion curHigh vLongBot;
             
        BreachDirectionControl 0;  //  reset the control variable.
           
        }

         }
        //  End if new bar


         
        if (getCurrentBarIndex() == 0) {  Only run on current RT bar.
            if ( (
        open(0) <= vLongBot) && (high(0) >= vLongBot)  && (BreachDirectionControl  <= 0) )     {
           
        //  Long Breach Instance
           
        BreachDirectionControl 1;  //  Currently a long breach
           
        curHigh Math.max(curHigh,high());    //  find highest high since breach
            
        }

        //  Now we need to continue to test for the new higher high until we reset the Breach Instance
            
        if (BreachDirectionControl 0) {
             
        curHigh Math.max(curHigh,high());    //  find highest high since 
            
        }
         
          }  
        //  End if get CurrentBarIndex() == 0

        // End main() 
        Now, I could alter this code to run all of this analysis on historical bars if you like?? It's all a matter of setting up your processes in a logical order WHEN you want them to operate - kinda like a staged event.

        If no even now, then check for new events
        If current event > 0 (bullish breach), then check for highest high
        If NEW BAR and current event > 0, then check for END of price run and reset control variables

        Does this help?
        Brad Matheny
        eSignal Solution Provider since 2000

        Comment


        • #5
          hmmm.. tried to fix a bug in my last post, but the EDIT function is not working for me?? Maybe someone does not want to allow me to edit my posts?? Oh well....

          In my last post where the new LONG BREACH instance starts, the line that reads....

          curHigh = Math.max(curHigh,high()); // find highest high since breach

          Should read....

          curHigh = high(); // record the current high of the breach

          Hope this helps.
          Brad Matheny
          eSignal Solution Provider since 2000

          Comment


          • #6
            Hi Brad,

            It used to be that you could go back and edit posts for an indefinite period. I remember awhile back that some posts were deleted that obliterated multiple threads (likely corrupted the databases).

            The indefinite window for editing has been changed. I assume this is what impacted your attempt to edit your post. The moderator for the associated forum has to be contacted to perform the edit after a certain time period (perhaps 15 minutes or less).


            Originally posted by Doji3333
            hmmm.. tried to fix a bug in my last post, but the EDIT function is not working for me?? Maybe someone does not want to allow me to edit my posts?? Oh well....

            Last edited by Guest; 02-24-2008, 05:25 PM.

            Comment


            • #7
              Thanks Steve. It worked a few days ago cause I often make minor errors when posting. Oh well, it will fix itself.

              Weeee, programming..
              Brad Matheny
              eSignal Solution Provider since 2000

              Comment


              • #8
                Kirk,

                Not sure if this is applicable as I read you post quickly but in case you are unaware there are back tester reports under the Graphs options:

                "Maximum Adverse Excursion", and "Maximum Favorable Excursion" which you may find helpful. From what I've read they are designed to be used to determine optimum stop and profit targets.

                Some other fields that I find helpful in the Trade Analysis tab of the back tester are the Average Run Up and Average drawdown.

                What I've never been able to figure out however is in a system that is "always in" for example if the Average Run up is 1.00 and the average drawdown is .50 if I set my stops and profit targets to 1.00 and .50 the systems always makes significantly less money. Perhaps the distribution is skewed not sure.

                Glen
                Last edited by demarcog; 02-25-2008, 10:25 AM.
                Glen Demarco
                [email protected]

                Comment


                • #9
                  Originally posted by Doji3333
                  What I would suggest is using a CONTROL VARIABLE to determine WHEN/IF the instance of your BREACH has ocured.

                  I use variables that rotate from 1, 0 to -1 and simply test for these values as needed

                  PHP Code:

                  var BreachDirectionControl 0;  //  no breach direction

                  function main() {

                   if( 
                  getBarState() == BARSTATE_NEWBAR){
                     if (
                  high() < high(-1)) { 
                       
                  //  End Excursion - this checks the two previously formed bars for a lower high formation.
                       
                  Total_Excursion curHigh vLongBot;
                       
                  BreachDirectionControl 0;  //  reset the control variable.
                     
                  }

                   }
                  //  End if new bar


                   
                  if (getCurrentBarIndex() == 0) {  Only run on current RT bar.
                      if ( (
                  open(0) <= vLongBot) && (high(0) >= vLongBot)  && (BreachDirectionControl  <= 0) )     {
                     
                  //  Long Breach Instance
                     
                  BreachDirectionControl 1;  //  Currently a long breach
                     
                  curHigh Math.max(curHigh,high());    //  find highest high since breach
                      
                  }

                  //  Now we need to continue to test for the new higher high until we reset the Breach Instance
                      
                  if (BreachDirectionControl 0) {
                       
                  curHigh Math.max(curHigh,high());    //  find highest high since 
                      
                  }
                   
                    }  
                  //  End if get CurrentBarIndex() == 0

                  // End main() 
                  Now, I could alter this code to run all of this analysis on historical bars if you like?? It's all a matter of setting up your processes in a logical order WHEN you want them to operate - kinda like a staged event.

                  If no even now, then check for new events
                  If current event > 0 (bullish breach), then check for highest high
                  If NEW BAR and current event > 0, then check for END of price run and reset control variables

                  Does this help?
                  Brad,
                  This definitely helps, it makes sense about the BreachDirectionControl (my vFlag needed a better definition).
                  I would ask if you're willing to help on the analysis of historical bars as you mentioned. I'm no statistics pro but am trying to determine the following:
                  1. what is the best time to trade this market (for best favorable excursion).
                  2. what is the average favorable excursion (for determining targets)
                  3. what is the bar count prior to backing off (for determining a time stop)
                  4. what is the prev high to current low (on longs) for how tight a stop could be.
                  I appreciate your help and insight.
                  Sincerely,
                  Kirk

                  Comment


                  • #10
                    Originally posted by demarcog
                    Kirk,

                    Not sure if this is applicable as I read you post quickly but in case you are unaware there are back tester reports under the Graphs options:

                    "Maximum Adverse Excursion", and "Maximum Favorable Excursion" which you may find helpful. From what I've read they are designed to be used to determine optimum stop and profit targets.

                    Some other fields that I find helpful in the Trade Analysis tab of the back tester are the Average Run Up and Average drawdown.

                    What I've never been able to figure out however is in a system that is "always in" for example if the Average Run up is 1.00 and the average drawdown is .50 if I set my stops and profit targets to 1.00 and .50 the systems always makes significantly less money. Perhaps the distribution is skewed not sure.

                    Glen
                    Glen,

                    Thanks for that reply, I've never studied how those fields in the Trade Analysis could be used this way. I'll check it out.

                    Kirk

                    Comment

                    Working...
                    X