Announcement

Collapse
No announcement yet.

Need help adding an alarm into my indicator

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

  • Need help adding an alarm into my indicator

    I was wondering if someone could help me with my indicator. I wrote an indicator that tells me when there are spikes of volume and it works fine but when I tried to add in an alert, the code seems to not work. Code is below. Thank you in advance!



    /************************************************** *******
    By the Choltron, the Doughboy, the man-monster-machine
    Holla back!
    ************************************************** ********/

    var vLastAlert = -1;

    function preMain() {

    setPriceStudy(true);
    setStudyTitle("Spikey");
    setCursorLabelName("Spikey", 0);
    setDefaultBarFgColor(Color.blue, 0);
    setPlotType(PLOTTYPE_LINE,0);
    setDefaultBarThickness(2,0);
    }



    function main() {

    if ((volume() > volume(-1)) && (2 * (sma(13, volume())) <= volume()))
    onAction1()

    else if ((volume() > volume(-1)) && (3 * (sma(13, volume())) <= volume()))
    onAction2();

    return null;
    }

    function postMain() {
    }


    function onAction1() {
    if (vLastAlert != 1) Alert.addToList(getSymbol(), "Pay attention!", Color.RGB(0,0,0), Color.blue);
    if (vLastAlert != 1) Alert.playSound("C:\Program Files\eSignal\Sounds\Ding.wav");
    if (vLastAlert != 1) setBarBgColor(Color.blue);

    vLastAlert = 1;
    }

    function onAction2() {
    if (vLastAlert != 2) Alert.addToList(getSymbol(), "Mommy!", Color.RGB(0,0,0), Color.red);
    if (vLastAlert != 2) Alert.playSound("C:\Program Files\eSignal\Sounds\Ding.wav");
    if (vLastAlert != 2) setBarBgColor(Color.red);

    vLastAlert = 2;

    }
    Attached Files

  • #2
    I believe alerts are only generated when getCurrentBarIndex() returns 0. In other words, only when the alert occurs on the last (most recent) bar of the chart. Otherwise, you'd be generating alerts on old bars.

    For example, the following works fine if added to main():
    PHP Code:
    if (getCurrentBarIndex() == 0) {
        
    Alert.addToList(getSymbol(), "Pay attention!"Color.RGB(0,0,0), Color.blue);
        
    Alert.playSound("C:\\Program Files\\eSignal\\Sounds\\Ding.wav");

    Last edited by shortandlong; 12-18-2009, 02:57 PM.

    Comment


    • #3
      thanks for the help shortandlong! I'll try it out on Monday and see how it works out

      Comment


      • #4
        Re: Need help adding an alarm into my indicator

        Choleon
        The reason the formula is not working with alerts is because of a number of logic errors
        When you have the conditions set as if...else if... the formula engine only evaluates the subsequent condition if the current one is false. If the current one is true then it stops evaluating any subsequent conditions.
        So you either need to remove the else from the second condition otherwise it will never be evaluated because if the volume is greater than 3 times the average of the volume it is by definition also already greater than 2 times the moving average of the volume
        PHP Code:
        if ((volume() > volume(-1)) && (* (sma(13volume())) <= volume())) 
                
        onAction1();
            
        if ((
        volume() > volume(-1)) && (* (sma(13volume())) <= volume())) 
                
        onAction2(); 
        or if you want to maintain the if...else if... logic then you need to write the conditions in the following order [ie reversed respect to yours]
        PHP Code:
        if ((volume() > volume(-1)) && (* (sma(13volume())) <= volume())) 
                
        onAction2();

        else if ((
        volume() > volume(-1)) && (* (sma(13volume())) <= volume())) 
                
        onAction1(); 
        The next logic error is in the use of the vLastAlert flag that is used to trigger the alerts once only ie
        PHP Code:
        function onAction1() {
                if (
        vLastAlert != 1Alert.addToList(getSymbol(), "Pay attention!"Color.RGB(0,0,0), Color.blue);
                if (
        vLastAlert != 1Alert.playSound("C:\\Program Files\\eSignal\\Sounds\\Ding.wav");
                if (
        vLastAlert != 1setBarBgColor(Color.blue);

                
        vLastAlert 1;
            }

        function 
        onAction2() {
                if (
        vLastAlert != 2Alert.addToList(getSymbol(), "Mommy!"Color.RGB(0,0,0), Color.red);
                if (
        vLastAlert != 2Alert.playSound("C:\\Program Files\\eSignal\\Sounds\\Ding.wav");
                if (
        vLastAlert != 2setBarBgColor(Color.red);

                
        vLastAlert 2;

            } 
        What will happen here is that while the formula is loading in the chart and runs through historical bars the first time that your first condition is true it will trigger the alerts [which will not be executed because the formula is still running on historical bars] and it will set the vLastAlert flag to 1. Since you never reset the flag to any other value no subsequent alerts will ever be triggered becauser the condition
        if (vLastAlert != 1)
        never evaluates to true.
        So you need to reset the vLastAlert flag at every bar to a value other than 1 or 2. To do this you need to add the following condition to your main function after the initial ones
        PHP Code:
        if(getBarState()==BARSTATE_NEWBAR)
                
        onAction3(); 
        and then add an onAction3() function as follows
        PHP Code:
        function onAction3(){
            
        vLastAlert = -1;

        This means that on every first tick of each new bar [ie BARSTATE_NEWBAR] you are executing the onAction3() function which in turn resets the value of the vLastAlert flag to -1. This way the next time one of your conditions is true the alerts will be executed because they are neither equal to 1 or 2
        That said once you have done this a new problem will arise ie that the alerts will be triggered continuously and not just once if both conditions evaluate to true.
        This is because the onAction1() function sets the vLastAlert flag to 1 which means that the condition
        if (vLastAlert != 2)
        in the onAction2() function will then be true thereby triggering the second alert. That function also sets the vLastAlert flag to 2 which means that now the condition
        if (vLastAlert != 1)
        in onAction1() will be again true on the following iteration of the efs thereby triggering the first alert again and so on and so forth in a continuous loop
        To correct this you need to set the conditions in the onAction1() function in a slightly different way ie
        PHP Code:
         function onAction1() {
                if(
        vLastAlert !=&& vLastAlert != 2Alert.addToList(getSymbol(), "Pay attention!"Color.RGB(0,0,0), Color.blue);
                if(
        vLastAlert !=&& vLastAlert != 2Alert.playSound("C:\\Program Files\\eSignal\\Sounds\\Ding.wav");
                if(
        vLastAlert !=&& vLastAlert != 2setBarBgColor(Color.blue);

                
        vLastAlert 1;
            } 
        This way when onAction2() sets the vLastAlert flag to 2 the condition in the onAction1() will no longer evaluate to true and the alerts will not be executed over and over again on that same bar.
        This leaves one more logic problem that will occur when the fiormula is triggering alerts on real time data. Because the setBarBgColor() function is executed only if the conditions
        if(vLastAlert !=1 && vLastAlert != 2)
        or
        if (vLastAlert != 2)
        are true the background will be painted only on that tick after which it disappears.
        To correct this you need to remove the condition to paint the background eg
        PHP Code:
         function onAction1() {
                if(
        vLastAlert !=&& vLastAlert != 2Alert.addToList(getSymbol(), "Pay attention!"Color.RGB(0,0,0), Color.blue);
                if(
        vLastAlert !=&& vLastAlert != 2Alert.playSound("C:\\Program Files\\eSignal\\Sounds\\Ding.wav");
                
        setBarBgColor(Color.blue);

                
        vLastAlert 1;
            } 
        As an aside a better way to write this would be as follows
        PHP Code:
        function onAction1() {
            
        setBarBgColor(Color.blue);
            if(
        vLastAlert !=&& vLastAlert != 2) {
                
        Alert.addToList(getSymbol(), "Pay attention!"Color.RGB(0,0,0), Color.blue);
                
        Alert.playSound("C:\\Program Files\\eSignal\\Sounds\\Ding.wav");
                
        vLastAlert 1;
            }

        Apply the same suggestions to the onAction2() function and the formula should work as expected ie it will color the background of the bar and sound the alerts once only per bar (note that I tested it using Tick Replay and it works fine once you make all these corrections)
        Alex



        Originally posted by Choleon
        I was wondering if someone could help me with my indicator. I wrote an indicator that tells me when there are spikes of volume and it works fine but when I tried to add in an alert, the code seems to not work. Code is below. Thank you in advance!



        /************************************************** *******
        By the Choltron, the Doughboy, the man-monster-machine
        Holla back!
        ************************************************** ********/

        var vLastAlert = -1;

        function preMain() {

        setPriceStudy(true);
        setStudyTitle("Spikey");
        setCursorLabelName("Spikey", 0);
        setDefaultBarFgColor(Color.blue, 0);
        setPlotType(PLOTTYPE_LINE,0);
        setDefaultBarThickness(2,0);
        }



        function main() {

        if ((volume() > volume(-1)) && (2 * (sma(13, volume())) <= volume()))
        onAction1()

        else if ((volume() > volume(-1)) && (3 * (sma(13, volume())) <= volume()))
        onAction2();

        return null;
        }

        function postMain() {
        }


        function onAction1() {
        if (vLastAlert != 1) Alert.addToList(getSymbol(), "Pay attention!", Color.RGB(0,0,0), Color.blue);
        if (vLastAlert != 1) Alert.playSound("C:\Program Files\eSignal\Sounds\Ding.wav");
        if (vLastAlert != 1) setBarBgColor(Color.blue);

        vLastAlert = 1;
        }

        function onAction2() {
        if (vLastAlert != 2) Alert.addToList(getSymbol(), "Mommy!", Color.RGB(0,0,0), Color.red);
        if (vLastAlert != 2) Alert.playSound("C:\Program Files\eSignal\Sounds\Ding.wav");
        if (vLastAlert != 2) setBarBgColor(Color.red);

        vLastAlert = 2;

        }

        Comment


        • #5
          I was going to comment on the vLastAlert flag, but Alex beat me to it...

          Personally, I find it almost impossible to debug EFS code without using debugPrintln() in key places in the code to print out what's going on. You can do things like:

          debugPrintln ("inside onAction1(), vLastAlert = " + vLastAlert);

          Also, I think the default directory for playSound is already C:\Program Files\eSignal\Sounds\ so all you need is:

          Alert.playSound ("Ding.wav");

          ...although what you have works fine.

          AlertOncePerBar.efs at Click Here is simple code the does an Alert everytime there's a new bar. Might be worth playing with.

          Comment


          • #6
            Thanks a ton Alexis and shortandlong! I edited the code and looks like its working so thanks again for all the work!

            Comment

            Working...
            X