Announcement

Collapse
No announcement yet.

Adding a delay to an alert

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

  • Adding a delay to an alert

    With the script below, I hear an alert every time the alert condition changes. But in flat markets, I hear the up, down, up, down alerts sometimes four times in 5 seconds. Is there a way to add a delay (of say 5 seconds) so that a change in condition will not be checked for until the 5 seconds has elapsed?

    Thanks
    Shaeffer

    An afterthought .. I could try changing the first condition to say:
    if(nMACD>(nMACD1 +.05) ....., but then I might miss some alerts

    PHP Code:
        if (getValue("rawtime",0) != nLastRawTime) {
            
            if(
    nMACDnMACD1 && count != 1){ //First Time Rising 
                
    Alert.playSound("SOUND19.wav");
                
    Alert.addToList(getSymbol(),"MACDc-"+getInterval()+" Up",Color.green,Color.green);
                
    //nLastRawTime = getValue("rawtime",0);
                
    count=1;
                
            }else if(
    nMACD nMACD1 && count != 2){ // First Time Falling  
                
    Alert.playSound("SOUND20.wav");
                
    Alert.addToList(getSymbol(),"MACDc-"+getInterval()+" Dn",Color.red,Color.red);
                
    //nLastRawTime = getValue("rawtime",0);
                
    count 2
            
    }
        } 
    Last edited by shaeffer; 06-30-2009, 10:43 AM.

  • #2
    What I did was reset the alert until the next new bar.

    var alertReset = false; //above the main function

    var barCount = getCurrentBarCount();

    //when alert happens
    alertReset = true;
    alertBar = barCount;

    //then in the alert logic

    if (alertReset == false) {
    // do alert stuff
    alertReset = true;
    }

    // where it is in main but not in alert

    if ((alertReset = true) && (barCount > alertBar)) alertReset = false;

    Comment


    • #3
      Hi jgr.

      I am using this on 15 and 60 minute charts, which for me is too long to wait between alerts.

      Thanks for the suggestion though
      Regards
      Shaeffer

      Comment


      • #4
        Re: Adding a delay to an alert

        Hi Shaeffer,

        You can try this function I put together using your code snippet. Based on you indicating your alerts were working, this should work as well (assuming I got my logic straight). Place the function outside the main function as you would with any other supporting function along with its associated global variables. Call the function with your variables as shown.

        Hope this works and helps you out.

        PHP Code:
        //~ call this function from main with your variables like this...
        alertDelay(nMACDnMACD1); 

        //~ delay code snippet with your alert snippet incorporated
        //~ #################################
        var alertDelayTimecheckAlertFlagdelayCheckFlagfallAlertriseAlert//~ associated global variables
        function alertDelay(aMACDaMACD1) {
          var 
        rTimesecDelay;
          
        rTime rawtime(0);
          
        secDelay 5;
          if (
        delayCheckFlag) {
            if (
        rTime alertDelayTime) {
              
        checkAlertFlag true;
            } else {
              
        checkAlertFlag false;
            }
          } else {
            
        checkAlertFlag true;
          }
            
          if (
        checkAlertFlag) {
            
        alertDelayTime rTime 1000 secDelay;
            if ((!
        riseAlert) && (aMACD aMACD1)) { //First Time Rising 
              
        riseAlert true;
              
        fallAlert false;
              
        delayCheckFlag true;
            } else if ((!
        fallAlert) && (aMACD aMACD1)) { // First Time Falling  
              
        riseAlert false;
              
        fallAlert true;
              
        delayCheckFlag true;
            }
            if (
        riseAlert) {
              
        Alert.playSound("SOUND19.wav");
              
        Alert.addToList(getSymbol(), "MACDc-" getInterval() + " Up"Color.greenColor.green);
            } else if (
        fallAlert) {
              
        Alert.playSound("SOUND20.wav");
              
        Alert.addToList(getSymbol(), "MACDc-" getInterval() + " Dn"Color.redColor.red);
            }
          }
        }
        //~ ################################# 

        Originally posted by shaeffer
        With the script below, I hear an alert every time the alert condition changes. But in flat markets, I hear the up, down, up, down alerts sometimes four times in 5 seconds. Is there a way to add a delay (of say 5 seconds) so that a change in condition will not be checked for until the 5 seconds has elapsed?

        Thanks
        Shaeffer

        An afterthought .. I could try changing the first condition to say:
        if(nMACD>(nMACD1 +.05) ....., but then I might miss some alerts

        PHP Code:
            if (getValue("rawtime",0) != nLastRawTime) {
                
                if(
        nMACDnMACD1 && count != 1){ //First Time Rising 
                    
        Alert.playSound("SOUND19.wav");
                    
        Alert.addToList(getSymbol(),"MACDc-"+getInterval()+" Up",Color.green,Color.green);
                    
        //nLastRawTime = getValue("rawtime",0);
                    
        count=1;
                    
                }else if(
        nMACD nMACD1 && count != 2){ // First Time Falling  
                    
        Alert.playSound("SOUND20.wav");
                    
        Alert.addToList(getSymbol(),"MACDc-"+getInterval()+" Dn",Color.red,Color.red);
                    
        //nLastRawTime = getValue("rawtime",0);
                    
        count 2
                
        }
            } 

        Comment


        • #5
          Thanks Steve,

          Much appreciated. After getting bombarded some more by multiple alerts after my original posting, I decided to try adding the +/- .005 values to the conditions as shown below to require a slightly larger change before the alert sounds. So far that has greatly cut down on the multiple 'teetering on the edge' alerts.

          if(nMACD> (nMACD1+0.005) && count != 1){ //First Time Rising
          ....
          }else if(nMACD < (nMACD1-0.005) && count != 2){ // First Time Falling

          But if it that .005 doesn't doesn't work (too many alerts or missed alerts) for all stocks, I'll give your solution a whirl.

          Thanks again
          shaeffer

          Comment


          • #6
            C'est la vie

            Originally posted by shaeffer
            Thanks Steve,

            Much appreciated. After getting bombarded some more by multiple alerts after my original posting, I decided to try adding the +/- .005 values to the conditions as shown below to require a slightly larger change before the alert sounds. So far that has greatly cut down on the multiple 'teetering on the edge' alerts.

            if(nMACD> (nMACD1+0.005) && count != 1){ //First Time Rising
            ....
            }else if(nMACD < (nMACD1-0.005) && count != 2){ // First Time Falling

            But if it that .005 doesn't doesn't work (too many alerts or missed alerts) for all stocks, I'll give your solution a whirl.

            Thanks again
            shaeffer

            Comment

            Working...
            X