Announcement

Collapse
No announcement yet.

Optimizing study... Help!

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

  • Optimizing study... Help!

    Hi Alexis,

    Would you help me optimize an efs code which paints bars depending on Linear Regression Bands (High/Low) direction.
    The code works fine, but the application is very slow (even on Daily charts) and CPU utilization almost 100%...

    efslib:

    LSMA_High

    PHP Code:
    function preMain() 

    setPriceStudy(true); 
    setStudyTitle("LSMA_High"); 
    setCursorLabelName("LSMA_High"); 
    setDefaultBarThickness(2); 
    setDefaultBarFgColor(Color.cyan,3);


    var 
    vPrice null

    var 
    vInit false
    var 
    vHLC3 0;

    function 
    main(nLength,nOffset,nBar

    if (
    nLength == nullnLength 55
    if (
    nOffset == nullnOffset 0
    if (
    nBar == nullnBar 0;

    if (
    vInit == false) { 
    vPrice = new Array(nLength); 
    vInit true


    vClose close(nBar); 
    vHigh high(nBar); 
    vLow low(nBar); 

    if (
    vClose == null) return; 
    if (
    vHigh == null) return; 
    if (
    vLow == null) return; 

    if (
    getBarState() == BARSTATE_NEWBAR) { 
    vPrice.pop(); 
    vPrice.unshift(vHLC3); 

    vHLC3 vHigh
    vPrice[0] = vHLC3

    if (
    vPrice[nLength-1] == null) return; 

    var 
    Num1 0.0
    var 
    Num2 0.0
    var 
    SumBars nLength * (nLength 1) * 0.5
    var 
    SumSqrBars = (nLength 1) * nLength * (nLength 1) / 6
    var 
    SumY 0.0
    var 
    Sum1 0.0
    var 
    Sum2 0.0
    var 
    Slope 0.0
    var 
    Intercept 0.0
    var 
    0
    for (
    0nLengthi++) 

    if(
    getCurrentBarIndex() > -&& getCurrentBarIndex() != 0debugPrintln(vPrice[i]); 
    SumY += vPrice[i]; 
    Sum1 += vPrice[i]; 

    Sum2 SumBars SumY
    Num1 nLength Sum1 Sum2
    Num2 SumBars SumBars nLength SumSqrBars
    if (
    Num2 != 0Slope Num1 Num2
    Intercept = (SumY Slope SumBars) / nLength
    var 
    LinearRegValue Intercept Slope * (nLength nOffset); 
    return 
    LinearRegValue

    LSMA_Low

    PHP Code:
    function preMain() 

    setPriceStudy(true); 
    setStudyTitle("LSMA_Low"); 
    setCursorLabelName("LSMA_Low"); 
    setDefaultBarThickness(2); 
    setDefaultBarFgColor(Color.cyan,3);


    var 
    vPrice null

    var 
    vInit false
    var 
    vHLC3 0;

    function 
    main(nLength,nOffset,nBar

    if (
    nLength == nullnLength 55
    if (
    nOffset == nullnOffset 0
    if (
    nBar == nullnBar 0;

    if (
    vInit == false) { 
    vPrice = new Array(nLength); 
    vInit true


    vClose close(nBar); 
    vHigh high(nBar); 
    vLow low(nBar); 

    if (
    vClose == null) return; 
    if (
    vHigh == null) return; 
    if (
    vLow == null) return; 

    if (
    getBarState() == BARSTATE_NEWBAR) { 
    vPrice.pop(); 
    vPrice.unshift(vHLC3); 

    vHLC3 vLow
    vPrice[0] = vHLC3

    if (
    vPrice[nLength-1] == null) return; 

    var 
    Num1 0.0
    var 
    Num2 0.0
    var 
    SumBars nLength * (nLength 1) * 0.5
    var 
    SumSqrBars = (nLength 1) * nLength * (nLength 1) / 6
    var 
    SumY 0.0
    var 
    Sum1 0.0
    var 
    Sum2 0.0
    var 
    Slope 0.0
    var 
    Intercept 0.0
    var 
    0
    for (
    0nLengthi++) 

    if(
    getCurrentBarIndex() > -&& getCurrentBarIndex() != 0debugPrintln(vPrice[i]); 
    SumY += vPrice[i]; 
    Sum1 += vPrice[i]; 

    Sum2 SumBars SumY
    Num1 nLength Sum1 Sum2
    Num2 SumBars SumBars nLength SumSqrBars
    if (
    Num2 != 0Slope Num1 Num2
    Intercept = (SumY Slope SumBars) / nLength
    var 
    LinearRegValue Intercept Slope * (nLength nOffset); 
    return 
    LinearRegValue

    Study

    PHP Code:
    var fpArray = new Array();
    var 
    vLastAlert = -1;

    function 
    preMain() {
    setPriceStudy(true);
    setStudyTitle("LinReg Paint Bars"); 
    setCursorLabelName("LinReg Paint Bars");

    setShowCursorLabel(false);
    setShowTitleParameters(false);
    setColorPriceBars(true);

    var 
    x=0;
        
    fpArray[x] = new FunctionParameter("Length"FunctionParameter.NUMBER);
        
    with(fpArray[x++]){
            
    setLowerLimit(1);        
            
    setDefault(55);
        } 


    function 
    main(Length,inputLengthinputOffset) {

    setPriceBarColor(Color.RGB(255,170,0));

    if(
    inputLength == null)
    inputLength Length;

    if(
    inputOffset == null)
    inputOffset 0;

    var 
    vNewHvOldHvNewLvOldL;

    vNewH call("/downloads/LSMA_High.efs"inputLengthinputOffset0);
    vOldH call("/downloads/LSMA_High.efs"inputLengthinputOffset, -1);

    vNewL call("/downloads/LSMA_Low.efs"inputLengthinputOffset0);
    vOldL call("/downloads/LSMA_Low.efs"inputLengthinputOffset, -1);

    if(
    vNewH vOldH && vNewL vOldLonAction1();
    if(
    vNewH vOldH && vNewL vOldLonAction2();

    return 
    null;
    }
    function 
    onAction1() {
    setPriceBarColor(Color.lime);
    vLastAlert 1;
    }
    function 
    onAction2() {
    setPriceBarColor(Color.red);
    vLastAlert 2;

    Thanks in advance!

  • #2
    first thing to try...

    get rid of the debugPrintln() statements in your code. These functions are called 4 times for each new bar. Thus, your debugPrintln() functions are outputting 4 values for each bar. This will slow things down considerably.

    Simply comment out the debugPrintln() functions and you should see a huge speed increase.

    B
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #3
      Nooze
      If you are using eSignal 7.9 then a very easy solution is to write the efs using the LSMA function that is included in the amStudies Function Library. You will no longer need three efs(s) to accomplish the same task as you will be able to do it all in one simple and more efficient script. For more information on the amStudies Function Library see this post and the one that follows
      If you are not yet using version 7.9 then I would suggest upgrading
      Alex

      Comment


      • #4
        I re-wrote this code and it works amazing!

        Thanks a lot, guys!

        Comment


        • #5
          Hi Nooze

          I love using the LSMA and use it all the time. I don't know a thing about programing, but I was wondering if you would share your rewrite of the LSMA that you liked so much. I apreciate any help.
          Mark

          Comment


          • #6
            Hi guys!

            This is a new version of the code what I described below:



            PHP Code:
            var fpArray = new Array();
            var 
            vLastAlert = -1;

            function 
            preMain() {
            setPriceStudy(true);
            setStudyTitle("LinReg Paint Bars"); 
            setCursorLabelName("LinReg Paint Bars");

            setShowCursorLabel(false);
            setShowTitleParameters(false);
            setColorPriceBars(true);

            var 
            x=0;
                
            fpArray[x] = new FunctionParameter("Length"FunctionParameter.NUMBER);
                
            with(fpArray[x++]){
                    
            setLowerLimit(1);        
                    
            setDefault(55);
                } 


            var 
            myLib addLibrary("amStudies.efsLib");

            function 
            main(Length) {

            setPriceBarColor(Color.RGB(255,170,0));

            var 
            NewH myLib.amLSMA(Lengthhigh());
            var 
            OldH myLib.amLSMA(Lengthhigh(),-1);
            var 
            NewL myLib.amLSMA(Lengthlow());
            var 
            OldL myLib.amLSMA(Lengthlow(),-1);
             
            if(
            NewH OldH && NewL OldLonAction1();
            if(
            NewH OldH && NewL OldLonAction2();

            return 
            null;
            }
            function 
            onAction1() {
            setPriceBarColor(Color.lime);
            vLastAlert 1;
            }
            function 
            onAction2() {
            setPriceBarColor(Color.red);
            vLastAlert 2;

            Please advice me how I can improve it?

            Thanks in advance!

            Comment


            • #7
              Hi Nooze

              Thanks so much for posting the formula. I get an error message that says line 22 Formula file not found: amstudies.efsLib

              I have 7.9 version, does your formula work OK, maybe I am making a mistake or something.
              Any help would be great, thanks
              Mark

              Comment


              • #8
                Mark
                For that efs to work you need the amStudies.efsLib Library in the FunctionLibrary folder of eSignal.
                You can find all the information on the amStudies Library here
                Alex

                Comment


                • #9
                  As Always Alex, thank you very much for your constant generosity and help. I will figure it out.
                  thanks
                  Mark

                  Comment


                  • #10
                    Nooze
                    The enclosed revision of your study should be more efficient
                    Alex

                    PHP Code:
                    var fpArray = new Array();

                    function 
                    preMain() {
                        
                    setPriceStudy(true);
                        
                    setStudyTitle("LinReg Paint Bars"); 
                        
                    setCursorLabelName("LinReg Paint Bars");
                        
                    setShowCursorLabel(false);
                        
                    setShowTitleParameters(false);
                        
                    setColorPriceBars(true);
                        
                    setDefaultPriceBarColor(Color.RGB(255,170,0));

                        var 
                    x=0;
                        
                    fpArray[x] = new FunctionParameter("Length"FunctionParameter.NUMBER);
                        
                    with(fpArray[x++]){
                            
                    setLowerLimit(1);        
                            
                    setDefault(55);
                        } 


                    var 
                    myLib addLibrary("amStudies.efsLib");
                    var 
                    bInit false;
                    var 
                    lsmaH null;
                    var 
                    lsmaL null;

                    function 
                    main(Length) {

                        if(
                    bInit==false){
                            
                    lsmaH myLib.amLSMA(Lengthhigh());
                            
                    lsmaL myLib.amLSMA(Lengthlow());
                            
                    bInit true;
                        }
                     
                        if(
                    lsmaH.getValue(0) > lsmaH.getValue(-1) && lsmaL.getValue(0) > lsmaL.getValue(-1)) onAction1();
                        if(
                    lsmaH.getValue(0) < lsmaH.getValue(-1) && lsmaL.getValue(0) < lsmaL.getValue(-1)) onAction2();

                        return 
                    null;
                    }

                    function 
                    onAction1() {
                        
                    setPriceBarColor(Color.lime);
                    }
                    function 
                    onAction2() {
                        
                    setPriceBarColor(Color.red);

                    Comment


                    • #11
                      Hi Alex

                      i know you weren't replying to me, but the study works really good and it is very much appreciated.
                      Thanks for all your help always
                      Mark

                      Comment


                      • #12
                        Mark
                        You are most welcome
                        Alex

                        Comment


                        • #13
                          Hi Alex,

                          Your version of the code is much better!

                          Thanks a lot!

                          Comment


                          • #14
                            Nooze
                            My pleasure and thank you for the feedback
                            Alex

                            Comment

                            Working...
                            X