Announcement

Collapse
No announcement yet.

Slow as a snail

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

  • Slow as a snail

    Hi!

    I am using this study which places buttons on my charts that allow me to change the variable "Shares". When clicked, the speed with which the variable is changed seems adequate. However, the second part of the script, which toggles the buttons so that it is easy to tell at a glance which one is active, is very sluggish. I am hoping that someone might have some coding recommendations that would make this script more efficient.

    Thank you, Charley


    PHP Code:
    var fpArray = new Array();

    function 
    preMain() {
        
    setPriceStudy(true);
        
    setStudyTitle("Shares Buttons");
        
    setShowCursorLabel(false);
        
    setShowTitleParameters(false);
        
        var 
    x=0;
        
        
    fpArray[x] = new FunctionParameter("sFont"FunctionParameter.STRING);
        
    with(fpArray[x++]){
            
    setName("Font");
            
    setDefault("Arial Black");
        }
        
    fpArray[x] = new FunctionParameter("nFontSize"FunctionParameter.NUMBER);
        
    with(fpArray[x++]){
            
    setName("Font Size")
            
    setLowerLimit(6);        
            
    setDefault(9);
        }
        
    fpArray[x] = new FunctionParameter("cBGColor"FunctionParameter.COLOR);
        
    with(fpArray[x++]){
            
    setName("BG Color");
            
    setDefault(Color.RGB(22,65,110));
        }    
        
    fpArray[x] = new FunctionParameter("cFontColor"FunctionParameter.COLOR);
        
    with(fpArray[x++]){    
            
    setName("Font Color");
            
    setDefault(Color.RGB(150,150,150));
        }
    }
    function 
    main(sFontnFontSizecBGColorcFontColor) {
        
    Find_SharesButtons(sFontnFontSizecBGColorcFontColor);
        return;
    }
    var 
    Shares 5;
    var 
    nID getCurrentBarCount();

    function 
    Shares5(nButtonPressed) {
        if (
    Shares != 1) {
            
    Shares 5; } }
    function 
    Shares10(nButtonPressed) {
        if (
    Shares != 2) {
            
    Shares 10; } }
    function 
    Shares25(nButtonPressed) {
        if (
    Shares != 3) {
            
    Shares 25; } }
    function 
    Shares50(nButtonPressed) {
        if (
    Shares != 4) {
            
    Shares 50; } }
    function 
    Shares100(nButtonPressed) {
        if (
    Shares != 5) {
            
    Shares 100; } }
    function 
    Shares200(nButtonPressed) {
        if (
    Shares != 6) {
            
    Shares 200; } }
    function 
    Shares300(nButtonPressed) {
        if (
    Shares != 7) {
            
    Shares 300; } }
    function 
    Shares500(nButtonPressed) {
        if (
    Shares != 8) {
            
    Shares 500; } }
    function 
    Shares1000(nButtonPressed) {
        if (
    Shares != 9) {
            
    Shares 1000; } }    

    function 
    Find_SharesButtons(sFontnFontSizecBGColorcFontColor) {
        
            
    SharesButton1(); 
            
    SharesButton2(); 
            
    SharesButton3();
            
    SharesButton4();
            
    SharesButton5();
            
    SharesButton6();
            
    SharesButton7();
            
    SharesButton8();
            
    SharesButton9();
     
        if (
    Shares == 5) {
            
            
    drawTextPixel(1202"5" "@URL=EFS:Shares5"cFontColorcBGColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTERsFontnFontSize"SB1 + nID"4817);
            
    drawTextPixel(1762"10" "@URL=EFS:Shares10"cFontColorcBGColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTERsFontnFontSize"SB2 + nID"4817);
            
    drawTextPixel(2322"25" "@URL=EFS:Shares25"cFontColorcBGColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTERsFontnFontSize"SB3 + nID"4817);
            
    drawTextPixel(2882"50" "@URL=EFS:Shares50"cFontColorcBGColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTERsFontnFontSize"SB4 + nID"4817);
            
    drawTextPixel(3442"100" "@URL=EFS:Shares100"cFontColorcBGColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTERsFontnFontSize"SB5 + nID"4817);
            
    drawTextPixel(4002"200" "@URL=EFS:Shares200"cFontColorcBGColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTERsFontnFontSize"SB6 + nID"4817);
            
    drawTextPixel(4562"300" "@URL=EFS:Shares300"cFontColorcBGColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTERsFontnFontSize"SB7 + nID"4817);
            
    drawTextPixel(5122"500" "@URL=EFS:Shares500"cFontColorcBGColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTERsFontnFontSize"SB8 + nID"4817);
            
    drawTextPixel(5682"1000" "@URL=EFS:Shares1000"cFontColorcBGColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTERsFontnFontSize"SB9 + nID"4817);
    }
    function 
    SharesButton1() {
        if (
    Shares == 5) {
            
    drawTextPixel(1202"5"cBGColorcFontColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTER Text.ONTOPsFont12"Active"4824);
       }    
    }        
    function 
    SharesButton2() {
        if (
    Shares == 10) {
            
    drawTextPixel(1762"10"cBGColorcFontColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTER Text.ONTOPsFont12"Active"4824);
        }    
    }
    function 
    SharesButton3() {
        if (
    Shares == 25) {
            
    drawTextPixel(2322"25"cBGColorcFontColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTER Text.ONTOPsFont12"Active"4824);
        }
    }
    function 
    SharesButton4() {
        if (
    Shares == 50) {
            
    drawTextPixel(2882"50"cBGColorcFontColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTER Text.ONTOPsFont12"Active"4824);
        }
    }
    function 
    SharesButton5() {
        if (
    Shares == 100) {
            
    drawTextPixel(3442"100"cBGColorcFontColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTER Text.ONTOPsFont12"Active"4824);
        }
    }
    function 
    SharesButton6() {
        if (
    Shares == 200) {
            
    drawTextPixel(4002"200"cBGColorcFontColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTER Text.ONTOPsFont12"Active"4824);
        }
    }
    function 
    SharesButton7() {
        if (
    Shares == 300) {
            
    drawTextPixel(4562"300"cBGColorcFontColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTER Text.ONTOPsFont12"Active"4824);
        }
    }
    function 
    SharesButton8() {
        if (
    Shares == 500) {
            
    drawTextPixel(5122"500"cBGColorcFontColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTER Text.ONTOPsFont12"Active"4824);
        }
    }
    function 
    SharesButton9() {
        if (
    Shares == 1000) {
            
    drawTextPixel(5682"1000"cBGColorcFontColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTER Text.ONTOPsFont12"Active"4824);
        }
    }
    }
    function 
    getButtonPressed(nButtonPressed) {
        
        if (
    nButtonPressed == BUTTON_LEFT) {
            return(
    1);
        }
        else {
            return(
    0);

        }
        return;


  • #2
    Hi,

    However, the second part of the script, which toggles the buttons so that it is easy to tell at a glance which one is active, is very sluggish.
    Here is an explanation of why it happens (as I discovered through debug testing).

    PHP Code:
    function getButtonPressed(nButtonPressed) { 
         
        if (
    nButtonPressed == BUTTON_LEFT) { 
            return(
    1); 
        } 
        else { 
            return(
    0); 

        } 
        return; 
    }  
    function 
    Shares5(nButtonPressed) { 
        if (
    Shares != 1) { 
            
    Shares 5; } } 
    function 
    Shares10(nButtonPressed) { 
        if (
    Shares != 2) { 
            
    Shares 10; } } 
    function 
    Shares25(nButtonPressed) { 
        if (
    Shares != 3) { 
            
    Shares 25; } } 
    function 
    Shares50(nButtonPressed) { 
        if (
    Shares != 4) { 
            
    Shares 50; } } 
    function 
    Shares100(nButtonPressed) { 
        if (
    Shares != 5) { 
            
    Shares 100; } } 
    function 
    Shares200(nButtonPressed) { 
        if (
    Shares != 6) { 
            
    Shares 200; } } 
    function 
    Shares300(nButtonPressed) { 
        if (
    Shares != 7) { 
            
    Shares 300; } } 
    function 
    Shares500(nButtonPressed) { 
        if (
    Shares != 8) { 
            
    Shares 500; } } 
    function 
    Shares1000(nButtonPressed) { 
        if (
    Shares != 9) { 
            
    Shares 1000; } } 
    "function getButtonPressed(nButtonPressed) { " works immediately when the button is clicked. So the variable "Shares" is modified right away.
    But all commands within main have to wait for a new tick to iterate main. In other words:

    PHP Code:
    function main(sFontnFontSizecBGColorcFontColor) { 
        
    Find_SharesButtons(sFontnFontSizecBGColorcFontColor); 
        return; 

    Because main() only iterates when a new tick comes in, the functions that draw the new button will only execute on new ticks.

    On fast moving markets/times the new button will draw fast, on sluggish slow markets, where a tick may not come in for several seconds, drawing the new button will also be sluggish and slow.

    To make the buttons change when they are clicked try the following (I left the debugPrintln() for you to verify my conclusions, by: when [("line 43: "+Shares)] prints vs when the "Shares" in the other debugPrintln() print).
    BUT test it during slow markets like after hours.

    PHP Code:
    //http://forum.esignal.com/showthread.php?38835-Slow-as-a-snail
    var fpArray = new Array(); 
    var 
    nButtonPressed        false;
    var 
    Shares 5
    var 
    nID getCurrentBarCount(); 

    function 
    preMain() { 
        
    setPriceStudy(true); 
        
    setStudyTitle("Shares Buttons"); 
        
    setShowCursorLabel(false); 
        
    setShowTitleParameters(false); 
         
        var 
    x=0
         
        
    fpArray[x] = new FunctionParameter("sFont"FunctionParameter.STRING); 
        
    with(fpArray[x++]){ 
            
    setName("Font"); 
            
    setDefault("Arial Black"); 
        } 
        
    fpArray[x] = new FunctionParameter("nFontSize"FunctionParameter.NUMBER); 
        
    with(fpArray[x++]){ 
            
    setName("Font Size"
            
    setLowerLimit(6);         
            
    setDefault(9); 
        } 
        
    fpArray[x] = new FunctionParameter("cBGColor"FunctionParameter.COLOR); 
        
    with(fpArray[x++]){ 
            
    setName("BG Color"); 
            
    setDefault(Color.RGB(22,65,110)); 
        }     
        
    fpArray[x] = new FunctionParameter("cFontColor"FunctionParameter.COLOR); 
        
    with(fpArray[x++]){     
            
    setName("Font Color"); 
            
    setDefault(Color.RGB(150,150,150)); 
        } 
    }
    var 
    s_Font,n_FontSize,c_BGColor,c_FontColor;
    var 
    bInit false
    function 
    main(sFontnFontSizecBGColorcFontColor) { 
        if(
    isLastBarOnChart()) debugPrintln("line 43: "+Shares); 
        if(!
    bInit){
            
    s_Font=sFont;
            
    n_FontSize=nFontSize;
            
    c_BGColor=cBGColor;
            
    c_FontColor=cFontColor;
            
    bInt=true;
        }
        
    Find_SharesButtons(sFontnFontSizecBGColorcFontColor); 

        return; 

     
    function 
    getButtonPressed(nButtonPressed) { 
         
        if (
    nButtonPressed == BUTTON_LEFT) { 
            return(
    1); 
        } 
        else { 
            return(
    0); 

        } 
        return; 
    }  
    function 
    Shares5(nButtonPressed) { 
        if (
    Shares != 1) { 
            
    Shares 5;debugPrintln(Shares); 
            
    drawTextPixel(1202"5"c_BGColorc_FontColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTER Text.ONTOPs_Font12"Active"4824); 
        } 

    function 
    Shares10(nButtonPressed) { 
        if (
    Shares != 2) { 
            
    Shares 10debugPrintln(Shares); 
            
    drawTextPixel(1762"10"c_BGColorc_FontColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTER Text.ONTOPs_Font12"Active"4824); 
        } 

    function 
    Shares25(nButtonPressed) { 
        if (
    Shares != 3) { 
            
    Shares 25debugPrintln(Shares); 
            
    drawTextPixel(2322"25"c_BGColorc_FontColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTER Text.ONTOPs_Font12"Active"4824); 
        } 

    function 
    Shares50(nButtonPressed) { 
        if (
    Shares != 4) { 
            
    Shares 50debugPrintln(Shares); 
            
    drawTextPixel(2882"50"c_BGColorc_FontColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTER Text.ONTOPs_Font12"Active"4824); 
        } 

    function 
    Shares100(nButtonPressed) { 
        if (
    Shares != 5) { 
            
    Shares 100debugPrintln(Shares); 
            
    drawTextPixel(3442"100"c_BGColorc_FontColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTER Text.ONTOPs_Font12"Active"4824); 
        } 

    function 
    Shares200(nButtonPressed) { 
        if (
    Shares != 6) { 
            
    Shares 200debugPrintln(Shares); 
            
    drawTextPixel(4002"200"c_BGColorc_FontColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTER Text.ONTOPs_Font12"Active"4824); 
        } 

    function 
    Shares300(nButtonPressed) { 
        if (
    Shares != 7) { 
            
    Shares 300debugPrintln(Shares); 
            
    drawTextPixel(4562"300"c_BGColorc_FontColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTER Text.ONTOPs_Font12"Active"4824); 
        } 

    function 
    Shares500(nButtonPressed) { 
        if (
    Shares != 8) { 
            
    Shares 500debugPrintln(Shares); 
            
    drawTextPixel(5122"500"c_BGColorc_FontColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTER Text.ONTOPs_Font12"Active"4824); 
        } 

    function 
    Shares1000(nButtonPressed) { 
        if (
    Shares != 9) { 
            
    Shares 1000debugPrintln(Shares); 
            
    drawTextPixel(5682"1000"c_BGColorc_FontColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTER Text.ONTOPs_Font12"Active"4824); 
        } 
    }     
    function 
    Find_SharesButtons(sFontnFontSizecBGColorcFontColor) { 
        if (
    Shares == 5) { 
             
            
    drawTextPixel(1202"5" "@URL=EFS:Shares5"cFontColorcBGColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTERsFontnFontSize"SB1 + nID"4817); 
            
    drawTextPixel(1762"10" "@URL=EFS:Shares10"cFontColorcBGColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTERsFontnFontSize"SB2 + nID"4817); 
            
    drawTextPixel(2322"25" "@URL=EFS:Shares25"cFontColorcBGColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTERsFontnFontSize"SB3 + nID"4817); 
            
    drawTextPixel(2882"50" "@URL=EFS:Shares50"cFontColorcBGColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTERsFontnFontSize"SB4 + nID"4817); 
            
    drawTextPixel(3442"100" "@URL=EFS:Shares100"cFontColorcBGColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTERsFontnFontSize"SB5 + nID"4817); 
            
    drawTextPixel(4002"200" "@URL=EFS:Shares200"cFontColorcBGColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTERsFontnFontSize"SB6 + nID"4817); 
            
    drawTextPixel(4562"300" "@URL=EFS:Shares300"cFontColorcBGColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTERsFontnFontSize"SB7 + nID"4817); 
            
    drawTextPixel(5122"500" "@URL=EFS:Shares500"cFontColorcBGColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTERsFontnFontSize"SB8 + nID"4817); 
            
    drawTextPixel(5682"1000" "@URL=EFS:Shares1000"cFontColorcBGColorText.RELATIVETOLEFT Text.RELATIVETOBOTTOM Text.CENTERsFontnFontSize"SB9 + nID"4817); 
        }



    Wayne
    Last edited by waynecd; 08-08-2013, 09:43 PM.

    Comment


    • #3
      Thanks for your help Wayne. This works much better.

      Comment


      • #4
        Yw.

        Wayne

        Comment

        Working...
        X