Announcement

Collapse
No announcement yet.

CCI Alert on each Cross vs ComputerOnClose

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

  • CCI Alert on each Cross vs ComputerOnClose

    Hi,

    I have a CCI Alert that beeps on each tick (which is driving me nuts) when the CCI is above 100 or below -100. I need help modifying the script so that the Alert beeps on each cross of the -+100. I would like the Alerts to beep on each cross not at the close of the bar as would be the case if I used "SetComputerOnClose".

    Thank you for your help.


    PHP Code:
    var vLastAlert = -1;

    function 
    preMain() {
        
    setPriceStudy(true);
        
    setStudyTitle("CCI Alert");
    }

    var 
    bInit false;

    var 
    wCCI null;

    function 
    main() {
        
        if (
    bInit == false) {
            
    Symbol getSymbol();
            
    Interval1 getInterval();
            var 
    vSymbol1 Symbol+","+Interval1;
            
    wCCI cci(20,sym(vSymbol1));
            
    bInit true;
        }
        
        var 
    gCCI wCCI.getValue(0);
        
        if (
           
    gCCI == null
           
    ) return;
     
        if  (
            
    gCCI 100
            
    )
            
    onAction1();
            
        else if (
            
    gCCI < -100
            
    )
            
    onAction2();
           
        return 
    null;
    }
        function 
    postMain() {
        }
        function 
    onAction1() {
            
    Alert.playSound("beep.wav");
            
    vLastAlert 1;
        }

        function 
    onAction2() {
            
    Alert.playSound("beep.wav");
            
    vLastAlert 2;
        } 

  • #2
    Re: CCI Alert on each Cross vs ComputerOnClose

    MacDaddy
    You need to first define the conditions for the crosses of +/-100 whereas at this time you are only defining the conditions when the CCI is above 100 or below -100
    To do this create a new local variable called [for example] gCCI_1 and assign to that the value of the vCCI at the prior bar ie.
    PHP Code:
    var gCCI_1 wCCI.getValue(-1); 
    Then modify the conditions to look for the crosses ie.
    PHP Code:
    if  (
            
    gCCI 100 && gCCI_1 <100
            
    )
            
    onAction1();
        else if (
            
    gCCI < -100 && gCCI_1 >-100
            
    )
            
    onAction2(); 
    Once you have done this implement the logic shown in the example provided in this thread which will trigger an alert only once per bar
    Alex


    Originally posted by MacDaddy
    Hi,

    I have a CCI Alert that beeps on each tick (which is driving me nuts) when the CCI is above 100 or below -100. I need help modifying the script so that the Alert beeps on each cross of the -+100. I would like the Alerts to beep on each cross not at the close of the bar as would be the case if I used "SetComputerOnClose".

    Thank you for your help.


    PHP Code:
    var vLastAlert = -1;

    function 
    preMain() {
        
    setPriceStudy(true);
        
    setStudyTitle("CCI Alert");
    }

    var 
    bInit false;

    var 
    wCCI null;

    function 
    main() {
        
        if (
    bInit == false) {
            
    Symbol getSymbol();
            
    Interval1 getInterval();
            var 
    vSymbol1 Symbol+","+Interval1;
            
    wCCI cci(20,sym(vSymbol1));
            
    bInit true;
        }
        
        var 
    gCCI wCCI.getValue(0);
        
        if (
           
    gCCI == null
           
    ) return;
     
        if  (
            
    gCCI 100
            
    )
            
    onAction1();
            
        else if (
            
    gCCI < -100
            
    )
            
    onAction2();
           
        return 
    null;
    }
        function 
    postMain() {
        }
        function 
    onAction1() {
            
    Alert.playSound("beep.wav");
            
    vLastAlert 1;
        }

        function 
    onAction2() {
            
    Alert.playSound("beep.wav");
            
    vLastAlert 2;
        } 

    Comment


    • #3
      MacDaddy
      See also this thread for an example of how to trigger a single alert each time an event occurs within the same bar
      Alex

      Comment


      • #4
        Thanks Alexis!!

        I went for the once per bar option. Below is the code and it seems to be working. Is there any improvments you make on the code?

        Thanks Again,
        Mac

        PHP Code:
        var vFlag false;

        function 
        preMain() {
            
        setPriceStudy(true);
            
        setStudyTitle("CCI Alert Once");
        }

        var 
        bInit false;

        var 
        wCCI null;

        function 
        main() {
            
            if (
        bInit == false) {
                
        Symbol getSymbol();
                
        Interval1 getInterval();
                var 
        vSymbol1 Symbol+","+Interval1;
                
        wCCI cci(20,sym(vSymbol1));
                
        bInit true;
            }
            
            if (
        getBarState() == BARSTATE_NEWBAR)
                
        vFlag false;
            
            var 
        gCCI   wCCI.getValue(0);
            var 
        gCCI_1 wCCI.getValue(-1);
            
            if (
            
        gCCI == null || gCCI_1 == null
            
        ) return;
         
            if  (
            
        gCCI 100 && gCCI_1 100 && vFlag == false
            
        )
            {
            
        Alert.playSound("beep.wav");
            
        vFlag true;
            }
                
            else if (
            
        gCCI < -100 && gCCI_1 > -100 && vFlag == false
            
        )
            {
            
        Alert.playSound("beep.wav");
            
        vFlag true;
            }
           
            return;

        Comment


        • #5
          Mac
          You are most welcome.
          As far as I can see your code looks fine. The only change I would probably make is to group all the code that needs to be executed under the same condition eg
          PHP Code:
          if(vFlag == false){
              if (
          gCCI 100 && gCCI_1 100) {
                  
          Alert.playSound("beep.wav");
                  
          vFlag true;
              }
              else if (
          gCCI < -100 && gCCI_1 > -100) {
                  
          Alert.playSound("beep.wav");
                  
          vFlag true;
              }

          There is no intrinsic advantage doing it this way other than that it is slightly easier in my opinion to identify the blocks of code that are executed under the same condition.
          Alex


          Originally posted by MacDaddy
          Thanks Alexis!!

          I went for the once per bar option. Below is the code and it seems to be working. Is there any improvments you make on the code?

          Thanks Again,
          Mac

          PHP Code:
          var vFlag false;

          function 
          preMain() {
              
          setPriceStudy(true);
              
          setStudyTitle("CCI Alert Once");
          }

          var 
          bInit false;

          var 
          wCCI null;

          function 
          main() {
              
              if (
          bInit == false) {
                  
          Symbol getSymbol();
                  
          Interval1 getInterval();
                  var 
          vSymbol1 Symbol+","+Interval1;
                  
          wCCI cci(20,sym(vSymbol1));
                  
          bInit true;
              }
              
              if (
          getBarState() == BARSTATE_NEWBAR)
                  
          vFlag false;
              
              var 
          gCCI   wCCI.getValue(0);
              var 
          gCCI_1 wCCI.getValue(-1);
              
              if (
              
          gCCI == null || gCCI_1 == null
              
          ) return;
           
              if  (
              
          gCCI 100 && gCCI_1 100 && vFlag == false
              
          )
              {
              
          Alert.playSound("beep.wav");
              
          vFlag true;
              }
                  
              else if (
              
          gCCI < -100 && gCCI_1 > -100 && vFlag == false
              
          )
              {
              
          Alert.playSound("beep.wav");
              
          vFlag true;
              }
             
              return;

          Comment


          • #6
            I hope I am not out of line.
            Your code triggers the beep.wav every time it crosses the 100 or -100 line even when CCI is just straddling the 100/-100 value without having reached the opposite value.

            If you just want an alert once upon cross of the 100 and no other signal until it crosses the -100 then you could try the following code:

            var vLastAlert = -1;

            function preMain() {
            setPriceStudy(true);
            setStudyTitle("CCI Alert");
            }

            var bInit = false;

            var wCCI = null;

            function main() {

            if (bInit == false) {
            Symbol = getSymbol();
            Interval1 = getInterval();
            var vSymbol1 = Symbol+","+Interval1;
            wCCI = cci(20,sym(vSymbol1));
            addBand(100, PS_SOLID, 1, Color.black, "BHigh");
            addBand(-100, PS_SOLID, 1, Color.black, "BLow");
            bInit = true;
            }

            var gCCI = wCCI.getValue(0);
            var gCCI_1 = wCCI.getValue(-1);

            if (gCCI == null) return;

            if (gCCI > 100 && gCCI_1 <100 && vLastAlert != 1){
            Alert.playSound("swoosh.wav");
            vLastAlert = 1;
            } else if (gCCI < -100 && gCCI_1 >-100 && vLastAlert != 2){
            Alert.playSound("swoosh.wav");
            vLastAlert = 2;
            return null;
            }

            Comment


            • #7
              Thanks again Alexis for your input. I rearranged the code as you suggested for easier reading. I'm just learning how to write code so I want to do the best job possible. Thanks much!!

              Waynecd......Thanks for your input. I saved your coded for future reference because of it's unique feature of one alert to +100 and not again until it comes back from the -100.

              Best,
              Mac

              Comment


              • #8
                Mac
                You are most welcome.
                In the case of the code suggested by waynecd (which is the same logic used by the Formula Wizard when you set the commands to be executed in "...the following will happen only the first time") you can also just check for the CCI to be above 100 or below -100 without the need to specify the cross eg
                PHP Code:
                if (gCCI 100 && vLastAlert != 1){
                    
                Alert.playSound("swoosh.wav");
                    
                vLastAlert 1;
                } else if (
                gCCI < -100 && vLastAlert != 2){
                    
                Alert.playSound("swoosh.wav");
                    
                vLastAlert 2;

                This is because the condition will be true only the first time the event occurs when the value of the flag vLastAlert is not equal to the value set in that condition. Once the alert is triggered the value of the flag is set to the same as that of the condition and is changed only when the other condition evaluates to true
                Alex


                Originally posted by MacDaddy
                Thanks again Alexis for your input. I rearranged the code as you suggested for easier reading. I'm just learning how to write code so I want to do the best job possible. Thanks much!!

                Waynecd......Thanks for your input. I saved your coded for future reference because of it's unique feature of one alert to +100 and not again until it comes back from the -100.

                Best,
                Mac

                Comment

                Working...
                X