Announcement

Collapse
No announcement yet.

Adding Alerts on DiNaploi Stochastic

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

  • Adding Alerts on DiNaploi Stochastic

    I am trying to add an alert to the built-in DiNapoli Stochastic so when the fast Stochastic reaches the 20 or 80 level my computer will beep.

    I have tried to program it myself but it does not seem to work. Can someone please help me and let me know what I am doing wrong.

    PHP Code:
    addBand(80PS_SOLID1Color.grey"Upper");
    addBand(20PS_SOLID1Color.grey"Lower");

    function 
    preMain()
    {
        
    setStudyTitle("Stochastic Alert");
        
    setCursorLabelName("percentK"0);
        
    setCursorLabelName("percentD"1);
        
    setDefaultBarFgColor(Color.blue0);
        
    setDefaultBarFgColor(Color.red1);

    }

    var 
    MAVt null;
    var 
    MAVt1 null;
    var 
    MAVt_1 0;
    var 
    MAVt1_1 0;

    function 
    main(nLengthnSmoothingnSmoothings) {
        if(
    nLength == null)
            
    nLength 8;
        if(
    nSmoothing == null)
            
    nSmoothing 3;
        if(
    nSmoothings == null)
            
    nSmoothings 3;

        var 
    percentK;
        var 
    ll 0hh 0;
        var 
    sum 0;
        var 
    vHigh getValue("High"0, -nLength);
        var 
    vLow getValue("Low"0, -nLength);
        var 
    temp 0;
        
        if(
    vHigh == null || vLow == null)
            return;


        for(
    0nLengthj++) {
            if(
    == 0) {
                
    ll vLow[j];    
                
    hh vHigh[j];
            } else {
                
    hh Math.max(hhvHigh[j]);
                
    ll Math.min(llvLow[j]);
            }
        }
        
    percentK = ((close() - ll) / (hh ll)) * 100;
        if (
    isNaN(percentK) == falseMAVt MAVt_1 + (percentK MAVt_1) / nSmoothing;
        if (
    getBarState() == BARSTATE_NEWBARMAVt_1 MAVt;
        if (
    isNaN(percentK) == falseMAVt1 MAVt1_1 + (MAVt MAVt1_1) / nSmoothings;
        if (
    getBarState() == BARSTATE_NEWBARMAVt1_1 MAVt1;
        
        return new Array(
    MAVt,MAVt1);
        
        
    /*********************************************
        ALERTS
        **********************************************/
        
        
    if(getBarState()==BARSTATE_NEWBAR){
            if(
    MAVt.getValue(MAVt,-2) < Upper && MAVt.getValue(MAVt,-1)>Upper){
                
    Alert.playSound("ding.wav");
                     
            if(
    MAVt.getValue(MAVt,-2) > Lower && MAVt.getValue(MAVt,-1)<Lower){
                
    Alert.playSound("ding.wav");

            }
        }}
        



  • #2
    steven723
    There are several errors in the code you posted.
    - The conditions and statements used to trigger the alerts should be placed before the return statement [ie return new Array(...)] and not after it.
    - In the conditions you reference the variables Upper and Lower but these variables have not been declared and have no values assigned to them.
    - In the conditions some closing brackets are placed in the wrong locations
    - The variables MAVt.getValue(MAVt,-2) and MAVt.getValue(MAVt,-1) are invalid because MAVt is not a series but a single value
    There are several ways you can retrieve prior values of a custom variable that is not a series and some methods are briefly explained in this post.
    In the case of the efs you posted the easiest way is to transfer those values at every BARSTATE_NEWBAR in the same way that is already being done for MAVt_1 and MAVt1_1.
    Here is what you need to do to resolve these issues.
    First thing create a new global variable (ie outside of main() or preMain()) calling it MAVt_2 and set it initially to a value of 0
    PHP Code:
    ...
    var 
    MAVt_2 0
     
    function main(nLength, ...){ 
    Then replace the following section of code
    PHP Code:
    if (isNaN(percentK) == falseMAVt MAVt_1 + (percentK MAVt_1) / nSmoothing;
    if (
    getBarState() == BARSTATE_NEWBARMAVt_1 MAVt;
    if (
    isNaN(percentK) == falseMAVt1 MAVt1_1 + (MAVt MAVt1_1) / nSmoothings;
    if (
    getBarState() == BARSTATE_NEWBARMAVt1_1 MAVt1
    with the following. In making the required changes I have also simplified the code as there is no need to check for BARSTATE_NEWBAR individually for MAVt and MAVt1
    PHP Code:
    if (getBarState() == BARSTATE_NEWBAR){
            
    MAVt_2 MAVt_1;
            
    MAVt_1 MAVt;
            
    MAVt1_1 MAVt1;
        }
        if (
    isNaN(percentK) == false){
            
    MAVt MAVt_1 + (percentK MAVt_1) / nSmoothing;
            
    MAVt1 MAVt1_1 + (MAVt MAVt1_1) / nSmoothings;
        } 
    At this point right before the return statement you can add your conditions to trigger the alerts. These would now have to be written as follows
    PHP Code:
    if(getBarState()==BARSTATE_NEWBAR){
            if(
    MAVt_2 80 && MAVt_1>80){
                
    Alert.playSound("ding.wav");
            }
            if(
    MAVt_2 20 && MAVt_1<20){
                
    Alert.playSound("ding.wav");
            }
        } 
    Replace each instance of 80 and 20 in the conditions with a variable if you want to do that. Rememeber however that you will need to declare that variable and assign some value to it.
    Once you implement these changes the script will trigger the alerts when the conditions evaluate to true
    Alex

    Comment


    • #3
      Thanks alot for your help and your explination of how to fix the problem.

      Steven

      Comment


      • #4
        Steven
        You are most welcome
        Alex

        Comment


        • #5
          Hi Steven,

          Any chance you could post the updated script so that it can be downloaded. Thank!

          Comment


          • #6
            Here you go. I don't know how to attach a file so I will just copy and paste it here:

            /************************************************** *******
            Alexis C. Montenegro © July 2003
            Use and/or modify this code freely. If you redistribute it
            please include this and/or any other comment blocks and a
            description of any changes you make.
            ************************************************** ********/

            addBand(80, PS_SOLID, 1, Color.grey, "Upper");
            addBand(20, PS_SOLID, 1, Color.grey, "Lower");

            function preMain()
            {
            setStudyTitle("Stochastic Alert");
            setCursorLabelName("percentK", 0);
            setCursorLabelName("percentD", 1);
            setDefaultBarFgColor(Color.blue, 0);
            setDefaultBarFgColor(Color.red, 1);

            }

            var MAVt = null;
            var MAVt1 = null;
            var MAVt_1 = 0;
            var MAVt1_1 = 0;
            var MAVt_2 = 0

            function main(nLength, nSmoothing, nSmoothings) {
            if(nLength == null)
            nLength = 8;
            if(nSmoothing == null)
            nSmoothing = 3;
            if(nSmoothings == null)
            nSmoothings = 3;

            var percentK;
            var ll = 0, hh = 0;
            var sum = 0;
            var vHigh = getValue("High", 0, -nLength);
            var vLow = getValue("Low", 0, -nLength);
            var temp = 0;

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


            for(j = 0; j < nLength; j++) {
            if(j == 0) {
            ll = vLow[j];
            hh = vHigh[j];
            } else {
            hh = Math.max(hh, vHigh[j]);
            ll = Math.min(ll, vLow[j]);
            }
            }
            percentK = ((close() - ll) / (hh - ll)) * 100;
            if (getBarState() == BARSTATE_NEWBAR){
            MAVt_2 = MAVt_1;
            MAVt_1 = MAVt;
            MAVt1_1 = MAVt1;
            }

            if (isNaN(percentK) == false){
            MAVt = MAVt_1 + (percentK - MAVt_1) / nSmoothing;
            MAVt1 = MAVt1_1 + (MAVt - MAVt1_1) / nSmoothings;
            }

            /*********************************************
            ALERTS
            **********************************************/

            if(getBarState()==BARSTATE_NEWBAR){
            if(MAVt_2 < 80 && MAVt_1 > 80){
            Alert.playSound("ding.wav");
            Alert.addToList(getSymbol(), "Stoch OverBot", Color.black, Color.red);
            }

            if(MAVt_2 > 20 && MAVt_1 < 20){
            Alert.playSound("ding.wav");
            Alert.addToList(getSymbol(), "Stoch OverSold", Color.black, Color.green);
            }
            }


            return new Array(MAVt,MAVt1);

            }

            Comment


            • #7
              Thank you so much!

              Comment

              Working...
              X