Announcement

Collapse
No announcement yet.

Price Distance from MA

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

  • Price Distance from MA

    Hi.
    I'd like to know if someone could program a script that shows the current distance of the price to a certain MA, either shown as an absolute value/number or as an oscillator / histogram.

    Thanks

    Gerd

  • #2
    Gerd
    The attached efs will plot the difference between the Close and the MA as an oscillator.
    All parameters for the MA can be adjusted through Edit Studies
    Alex

    Attached Files

    Comment


    • #3
      Alex can we...

      Hi Alex can you tell me how to add an alert to this efs so that if at the bar close the line has crossed above zero I get a bullish alert just once and when after the bar close the line has crossed down below zero i get bearish alert!

      Comment


      • #4
        Rupe
        The easiest way to do that is by creating two new variables ie vDiff1 which will be the value of the indicator at the prior bar and vDiff2 which is the value of two bars ago which you will then use for the conditional statement. To do that add the following two lines just below var vDiff = close()...etc

        PHP Code:
        var vDiff1 close(-1)-vMA.getValue(MAStudy.MA,-1);
        var 
        vDiff2 close(-2)-vMA.getValue(MAStudy.MA,-2); 
        At this point you can set the conditions when a new bar is formed (which is the same as saying at the close of the prior bar). Insert the following lines after those in which you have declared the variables

        PHP Code:
        if(getBarState()==BARSTATE_NEWBAR){
                if(
        vDiff2 && vDiff1 0)
                    
        Alert.playSound("ding.wav")
                
        //add here a second conditional statement for crossing down
                    //add here the alert
            

        These additions will trigger a single alert every time the indicator crosses the 0 line
        Alex

        Comment


        • #5
          made the changes and...

          hi alex,

          i made the changes but i'm getting bullish AND bearish alert at every new bar ... can u look at this please...
          Attached Files
          Last edited by rupe; 05-06-2005, 09:02 AM.

          Comment


          • #6
            Rupe
            In my example I was executing only one command based on the conditional statement so I did not enclose it with {} brackets as that is not necessary. However if you are executing multiple commands you need to enclose them in {} brackets as in the following example
            if(x>y){
            do this
            do that
            }
            Add the appropriate brackets and it will work correctly.
            Alex

            Comment

            Working...
            X