Announcement

Collapse
No announcement yet.

Suggestions for speeding this formula up

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

  • Suggestions for speeding this formula up

    This is the first formula I've done and I'm sure there's a lot of room for improvement. As it is now, it's unusably slow. I go through my watchlist pulling up the charts, and am waiting several seconds for each to load with this formula loaded. I'm hoping someone can offer some suggestions for speeding it up.

    thanks alot.


    PHP Code:
    bCurrentDay false;

    function 
    preMain() {

        
    setPriceStudy(true);
        
    setStudyTitle("rwe_traderxfib2");
        
    setCursorLabelName("yest"0);
        
    setCursorLabelName("today"1);
        
    setCursorLabelName("fib"2);
        
    setCursorLabelName("fib"3);
        
    setCursorLabelName("fib"4);
        
    setCursorLabelName("fib"5);
        
    setCursorLabelName("fib"6);
        
    setPlotType(PLOTTYPE_FLATLINES,0);
        
    setPlotType(PLOTTYPE_FLATLINES,1);
        
    setPlotType(PLOTTYPE_FLATLINES,2);
        
    setPlotType(PLOTTYPE_FLATLINES,3);
        
    setPlotType(PLOTTYPE_FLATLINES,4);
        
    setPlotType(PLOTTYPE_FLATLINES,5);
        
    setPlotType(PLOTTYPE_FLATLINES,6);
        
    setDefaultBarFgColor(Color.blue,0);
        
    setDefaultBarFgColor(Color.red,1);
        
    setDefaultBarFgColor(Color.black,2);
        
    setDefaultBarFgColor(Color.black,3);
        
    setDefaultBarFgColor(Color.black,4);
        
    setDefaultBarFgColor(Color.black,5);
        
    setDefaultBarFgColor(Color.black,6);
        
    }


    var 
    bInit false;
    var 
    xRange null;

    function 
    main(){

    if (
    bCurrentDay == false && getDay(0) != getDay(-1)){ 
        var 
    vTime getValue("time"); 
        var 
    vDate = new Date(); 
        var 
    sTime = (vTime.getMonth()+1+"/"+vTime.getDate()+"/"+vTime.getFullYear()); 
        var 
    sToday = (vDate.getMonth()+1+"/"+vDate.getDate()+"/"+vDate.getFullYear()); 
        if ( 
    sTime == sToday ) { 
            
    bCurrentDay true
        } 


    if(
    bInit==false){
        
    xRange efsInternal("calcOR","630","645","Show",inv(15));
        
    bInit=true;
     }

    orHigh getSeries(xRange0);
    orLow getSeries(xRange1);

    yestHefsExternal("/OHLC/getPrevOHLC.efs""High");
    yestLefsExternal("/OHLC/getPrevOHLC.efs""Low");
    yestCefsExternal("/OHLC/getPrevOHLC.efs""Close");
    todayOefsExternal("/OHLC/getTodayOHLC.efs""Open");

    gap = ((todayO yestC) / yestC) * 100;

    if(
    gap <= -1){ // gap down
        
    yestLine yestH;
        
    orLine orLow;
        
    hiloDiff yestLine orLine;
        
    fib1 hiloDiff*0.382;
        
    fib2 hiloDiff*0.5;
        
    fib3 hiloDiff*0.618;
        
    fib1Line orLine fib1;
        
    fib2Line orLine fib2;
        
    fib3Line orLine fib3;
        
    fib4Line orLine fib1;
        
    fib5Line yestLine fib1;
    }else if(
    gap >= 1){ // gap up
        
    yestLine yestL;
        
    orLine orHigh;
        
    hiloDiff orLine yestLine;
        
    fib1 hiloDiff*0.382;
        
    fib2 hiloDiff*0.5;
        
    fib3 hiloDiff*0.618;
        
    fib1Line yestLine fib1;
        
    fib2Line yestLine fib2;
        
    fib3Line yestLine fib3;
        
    fib4Line yestLine fib1;
        
    fib5Line orLine fib1;
    }else{
        return;
    }
       
    if (
    bCurrentDay==false){
        return;
    }

    return new Array(
    yestLineorLinefib1Linefib2Linefib3Linefib4Linefib5Line);

    }

    var 
    vFlag true;
    var 
    vFlag2 true
    var vHigh null;
    var 
    vLow null;
    var 
    vClose null;
    var 
    vDay1 null;
    var 
    vDay2 null;

    function 
    calcOR(start,end,display,source){

        if (
    getBarState() == BARSTATE_NEWBAR) {
            if (
    vDay1 == null) {
                
    vDay2 getDay(0);
            } else {
                
    vDay2 vDay1;
            }
            
    vDay1 getDay(0);
            if (
    vDay1 != vDay2) {
                
    vHigh null;
                
    vLow null;
                
    vFlag true;
                
    vFlag2 false;
            }
            var 
    vHour1 = (getHour()*100)+getMinute();
            if(
    vHour1 >= start){
                
    vFlag2=true;
            }
            var 
    vHour = (getHour()*100)+getMinute();
            if (
    vHour >= end) {
                
    vFlag false;
                
    vFlag2=false;
            }
        }

        if (
    vFlag == true&&vFlag2==true) {
            if (
    vHigh == null) {
                
    vHigh high(0);
            }
            if (
    vLow == null) {
                
    vLow low(0);
            }
            
    vHigh Math.max(high(0), vHigh);
            
    vLow Math.min(low(0), vLow);
        }
        
        if(
    display=="Hide"){
            if(
    vFlag==false&&vHigh!=null&&vLow!=null){
                var 
    vHigh2 vHigh;
                var 
    vLow2 vLow;
            }
        }else if(
    display=="Show"){
            var 
    vHigh2 vHigh;
            var 
    vLow2 vLow;
        }

        return new Array(
    vHigh2,vLow2);


  • #2
    I think this link will greatly help with coding( the IE/2 PHP): http://forum.esignalcentral.com/show...threadid=12924

    Haven't ran your code, but 2 areas in your code to look at:
    The efsExternal declarations are being called on every tick.
    There is no checking for null value of your returned values.

    I hope this is enough info to help you

    Comment


    • #3
      Thanks for the reply.

      I've been messing with the formula and have another question now. Why is bInit "false" 2 different times? I put a debugPrintln in that If statement and it's executing twice. I don't understand that. I thought the global variables were just set when the script was first loaded.

      Thanks for any help.

      Comment


      • #4
        I fought that battle too. I believe I solved it by doing testing for getBarState() in main() AND having the bulk of my code/subroutine calls INSIDE these test conditions.
        ex. your " if(gap <= -1){ ..} " do you really want this being computed on every incoming tick? [ it is now ]
        probably want newbar OR only once.

        3 ideas here: ( homework for you )


        go to the one at 11-01-2004 03:18 PM
        Very similar to your code, lots of other good stuff here too.

        in http://kb.esignalcentral.com search for
        AlertOncePerBar.efs (small code sample)
        Basic idea here is the test getBarState() in main()

        Checkout
        isLastBarOnChart() -- Returns true if the bar currently being processed is the last bar on the chart.

        P.S. keep growing, you are off to a great start

        Comment


        • #5
          Thanks for the help David. I'll go study those links.

          As for bInit executing twice, is that a bug in EFS? Or am I doing something else wrong. I see that used in other scripts, and it looked like it was for only running blocks of code once.

          Am I misunderstanding how the scripts are executed?

          Thanks again.

          Comment


          • #6
            Hello chud and David,

            A global bInit routine shouldn't be executing twice. As a test I used the following code and I'm only seeing one execution.

            PHP Code:
            var bInit false;
            var 
            nCount 0;

            function 
            main() {
                if (
            bInit == false) {
                    
            nCount++;
                    
            debugPrintln(nCount);
                    
            bInit true;
                }
                
                return;

            Please post any code examples you have that are executing a global bInit routine more than once, I'd like to investigate this further.
            Jason K.
            Project Manager
            eSignal - an Interactive Data company

            EFS KnowledgeBase
            JavaScript for EFS Video Series
            EFS Beginner Tutorial Series
            EFS Glossary
            Custom EFS Development Policy

            New User Orientation

            Comment


            • #7
              Jason,

              It looks like the efsInternal call within the if(bInit==false) block is causing the routine to execute twice. I took that statement out, and got the expected one execution. But with it in, I keep getting 2 executions. Try this code:

              PHP Code:

              var bInit false;
              var 
              xRange null;
              var 
              nCount 0;


              function 
              main(){

                  if(
              bInit==false){
                      
              nCount++;
                      
              debugPrintln(nCount);
                      
              xRange efsInternal("calcOR","630","645","Show",inv(15));
                      
              bInit=true;
                  }

              }

              var 
              vFlag true;
              var 
              vFlag2 true
              var vHigh null;
              var 
              vLow null;
              var 
              vClose null;
              var 
              vDay1 null;
              var 
              vDay2 null;

              function 
              calcOR(start,end,display,source){

                  if (
              getBarState() == BARSTATE_NEWBAR) {
                      if (
              vDay1 == null) {
                          
              vDay2 getDay(0);
                      } else {
                          
              vDay2 vDay1;
                      }
                      
              vDay1 getDay(0);
                      if (
              vDay1 != vDay2) {
                          
              vHigh null;
                          
              vLow null;
                          
              vFlag true;
                          
              vFlag2 false;
                      }
                      var 
              vHour1 = (getHour()*100)+getMinute();
                      if(
              vHour1 >= start){
                          
              vFlag2=true;
                      }
                      var 
              vHour = (getHour()*100)+getMinute();
                      if (
              vHour >= end) {
                          
              vFlag false;
                          
              vFlag2=false;
                      }
                  }

                  if (
              vFlag == true&&vFlag2==true) {
                      if (
              vHigh == null) {
                          
              vHigh high(0);
                      }
                      if (
              vLow == null) {
                          
              vLow low(0);
                      }
                      
              vHigh Math.max(high(0), vHigh);
                      
              vLow Math.min(low(0), vLow);
                  }
                  
                  if(
              display=="Hide"){
                      if(
              vFlag==false&&vHigh!=null&&vLow!=null){
                          var 
              vHigh2 vHigh;
                          var 
              vLow2 vLow;
                      }
                  }else if(
              display=="Show"){
                      var 
              vHigh2 vHigh;
                      var 
              vLow2 vLow;
                  }

                  return new Array(
              vHigh2,vLow2);

              Comment


              • #8
                Hello Chud,

                Thanks for posting your code. You're right, it is executing twice. However, it is not a bug. efsInternal() is forcing a reload of the formula. The values of a custom series have to be initialized for each bar on the chart in order for them to become available for the initialization of the formula as it processes the historical bars. The custom series values are not maintained at the formula level, which is the reason for the reload. This process occurs for each custom series created by efsInternal() as well. You can see this with the modification of your script below. If you enable the xRange2 series, you will see the bInit routine in main() executes a 3rd time. There's a debugPrintln() statement inside the clacOR function so you may want to limit the number of days of data in your chart before running this.

                PHP Code:
                var bInit false;
                var 
                xRange null;
                var 
                xRange2 null;
                var 
                nCount 0;


                function 
                main(){

                    if(
                bInit==false){
                        
                nCount++;
                        
                debugPrintln(nCount);
                        
                xRange efsInternal("calcOR","630","645","Show"inv(15));
                        
                //xRange2 = efsInternal("calcOR","630","645","Show", inv(30));
                        
                bInit=true;
                    }

                }

                var 
                vFlag true;
                var 
                vFlag2 true
                var vHigh null;
                var 
                vLow null;
                var 
                vClose null;
                var 
                vDay1 null;
                var 
                vDay2 null;

                function 
                calcOR(start,end,display,source){
                    if(
                bInit==false){
                        
                nCount++;
                        
                //debugPrintln(nCount);
                        
                bInit=true;
                    }
                    
                    var 
                nIndex getCurrentBarIndex();
                    if (
                nIndex != 0debugPrintln(nCount "  " nIndex);

                    if (
                getBarState() == BARSTATE_NEWBAR) {
                        if (
                vDay1 == null) {
                            
                vDay2 getDay(0);
                        } else {
                            
                vDay2 vDay1;
                        }
                        
                vDay1 getDay(0);
                        if (
                vDay1 != vDay2) {
                            
                vHigh null;
                            
                vLow null;
                            
                vFlag true;
                            
                vFlag2 false;
                        }
                        var 
                vHour1 = (getHour()*100)+getMinute();
                        if(
                vHour1 >= start){
                            
                vFlag2=true;
                        }
                        var 
                vHour = (getHour()*100)+getMinute();
                        if (
                vHour >= end) {
                            
                vFlag false;
                            
                vFlag2=false;
                        }
                    }

                    if (
                vFlag == true&&vFlag2==true) {
                        if (
                vHigh == null) {
                            
                vHigh high(0);
                        }
                        if (
                vLow == null) {
                            
                vLow low(0);
                        }
                        
                vHigh Math.max(high(0), vHigh);
                        
                vLow Math.min(low(0), vLow);
                    }
                    
                    if(
                display=="Hide"){
                        if(
                vFlag==false&&vHigh!=null&&vLow!=null){
                            var 
                vHigh2 vHigh;
                            var 
                vLow2 vLow;
                        }
                    }else if(
                display=="Show"){
                        var 
                vHigh2 vHigh;
                        var 
                vLow2 vLow;
                    }

                    return new Array(
                vHigh2,vLow2);

                Jason K.
                Project Manager
                eSignal - an Interactive Data company

                EFS KnowledgeBase
                JavaScript for EFS Video Series
                EFS Beginner Tutorial Series
                EFS Glossary
                Custom EFS Development Policy

                New User Orientation

                Comment

                Working...
                X