Announcement

Collapse
No announcement yet.

Please help with ATR

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

  • Please help with ATR

    Hi,

    I'm new to writing EFS code, and I want to plot two lines on an intraday chart.

    The first line should be the weekly opening price - (Weekly ATR(100) * 0.65).

    The second line should be opening price + (Weekly ATR(100) * 0.65).

    Here's my code that doesn't work and I'm not sure what to do next:

    Code:
    function preMain() {
    	setPriceStudy(true);
    	setStudyTitle("Weekly 65 Percent ATR");
    	setCursorLabelName("WATR");
    
    	setDefaultBarStyle(PS_DASH);
    	setDefaultBarFgColor(Color.red);
    	setDefaultBarThickness(2);
        setPlotType(PLOTTYPE_FLATLINES);
    }       
    var bInit = false;
    var xStudy = null;
    var nShortATR = null
    
    function main() {
    	if(isMonthly() || isWeekly() || isDaily())
        return;
        
        if(bInit == false){
            xStudy = atr(100, inv("W")) * 0.65;
            xOpen  = open(inv("W"));
            nShortATR = xOpen - xStudy;
    
            bInit = true;
        }
        
        return getSeries(nShortATR);
    }
    Please help. Oh, BTW I'm only expecting the above code to draw one line, but I'd like to do two once I've worked out how to draw one.

    Thanks,
    Mark.

  • #2
    Hi Mark,
    I should do this way.
    Bye.
    Massimo


    function preMain() {
    setPriceStudy(true);
    setStudyTitle("Weekly 65 Percent ATR");
    setCursorLabelName("WATR_Long",0);
    setDefaultBarStyle(PS_DASH,0);
    setDefaultBarFgColor(Color.green,0);
    setDefaultBarThickness(2,0);
    setPlotType(PLOTTYPE_FLATLINES,0);
    setCursorLabelName("WATR_Short",1);
    setDefaultBarStyle(PS_DASH,1);
    setDefaultBarFgColor(Color.red,1);
    setDefaultBarThickness(2,1);
    setPlotType(PLOTTYPE_FLATLINES,1);
    }
    var bInit = false;
    var xStudy = null;
    var vStudy = null;
    var xOpen = null;
    var vOpen = null;
    var nShortATR = null;
    var nLongATR = null;

    function main() {
    if (isMonthly() || isWeekly() || isDaily()) return;

    if (bInit == false){
    xStudy = atr(100, inv("W"));
    xOpen = open(inv("W"));
    bInit = true;
    }

    vStudy = xStudy.getValue(0) * 0.65;
    vOpen = xOpen.getValue(0);

    if (vStudy == null || vOpen == null) return;

    nShortATR = vOpen - vStudy;
    nLongATR = vOpen + vStudy;
    return new Array (nLongATR,nShortATR);
    }

    Comment


    • #3
      That's great - works well. Thanks for your help.
      Last edited by mallison; 05-06-2010, 12:16 AM.

      Comment


      • #4
        I want to adapt this study a little so that it draws two lines. The first one will be the high of the week so far minus the weekly ATR(100). The second line will be the low of the week so far + the weekly ATR(100). I don't want to take the values from the weekly chart because then I get horizontal lines for each week. What I want is for the lines to develop bar by bar on a 240 min chart.

        I've attempted this with the code below but it gives me horizontal lines, not a developing line, which is what I want. Does that make sense? Is there a way of getting the high and the low of the week so far?

        Code:
        function preMain() {
        	setPriceStudy(true);
        	setStudyTitle("Weekly 65 Percent ATR");
        	setCursorLabelName("WATR_Long",0);
        	setDefaultBarStyle(PS_DASH,0);
        	setDefaultBarFgColor(Color.darkgreen,0);
        	setDefaultBarThickness(2,0);
        	setPlotType(PLOTTYPE_FLATLINES,0);
        	setCursorLabelName("WATR_Short",1);
        	setDefaultBarStyle(PS_DASH,1);
        	setDefaultBarFgColor(Color.maroon,1);
        	setDefaultBarThickness(2,1);
        	setPlotType(PLOTTYPE_FLATLINES,1);
        } 
        
        var bInit = false;
        var xStudy = null;
        var vStudy = null;
        var xLow = null;
        var xHigh = null;
        var vHigh = null;
        var vLow = null;
        var nShortATR = null;
        var nLongATR = null;
        
        function main() {
        	if (isMonthly() || isWeekly() || isDaily()) return;
        
        	if (bInit == false){
        	xStudy = atr(100, inv("W"));
        	xHigh = high(inv("W"));
        	xLow = low(inv("W"));
        	bInit = true;
        }
        
        vStudy = xStudy.getValue(0) * 0.65;
        vHigh = xHigh.getValue(0);
        vLow = xLow.getValue(0);
        
        if (vStudy == null || vHigh == null || vLow == null) return;
        
        nShortATR = vHigh - vStudy;
        nLongATR = vLow + vStudy;
        return new Array (nLongATR,nShortATR);
        }

        Comment


        • #5
          Hi Mallison,
          if you use xHigh = high(inv("W")); and vHigh = xHigh.getValue(-x);
          you will get the high of week -x and it will be the same value all the days of the week till the next monday when it will change.
          That's why your efs gives you horizontal lines from monday till friday and next monday changes.
          You say you use 240 min chart and that you want the lines to develop bar by bar.
          I don't understand what kind of lines do you want ?
          You mean the highest or lowest of past five days +/- the weekly ATR(100)? Or what else ?
          Massimo

          Comment


          • #6
            Originally posted by maxmax68
            Hi Mallison,
            if you use xHigh = high(inv("W")); and vHigh = xHigh.getValue(-x);
            you will get the high of week -x and it will be the same value all the days of the week till the next monday when it will change.
            That's why your efs gives you horizontal lines from monday till friday and next monday changes.
            You say you use 240 min chart and that you want the lines to develop bar by bar.
            I don't understand what kind of lines do you want ?
            You mean the highest or lowest of past five days +/- the weekly ATR(100)? Or what else ?
            Massimo
            Thanks for the reply. What I want is for each bar on a 240 min chart, the current high for the current week. So if I go back in history and look at a bar on Tuesday that draws from 0800-1200 for example, I want the high of the week until Tuesday 1200 - not the high of the entire week. How can I retrieve that value?

            Comment


            • #7
              Try this:

              function preMain() {
              setPriceStudy(true);
              setStudyTitle("Weekly 65 Percent ATR");
              setCursorLabelName("WATR_Long",0);
              setDefaultBarStyle(PS_DASH,0);
              setDefaultBarFgColor(Color.green,0);
              setDefaultBarThickness(2,0);
              //setPlotType(PLOTTYPE_FLATLINES,0);
              setCursorLabelName("WATR_Short",1);
              setDefaultBarStyle(PS_DASH,1);
              setDefaultBarFgColor(Color.red,1);
              setDefaultBarThickness(2,1);
              //setPlotType(PLOTTYPE_FLATLINES,1);

              }
              debugClear;
              var bInit = false;
              var xStudy = null;
              var vStudy = null;
              var xLow = null;
              var xHigh = null;
              var vHigh = null;
              var vLow = null;
              var vHigh_1 = null;
              var vLow_1 = null;
              var nShortATR = null;
              var nLongATR = null;
              var counter = 0;

              function main() {
              if (isMonthly() || isWeekly() || isDaily()) return;

              if (bInit == false){
              xStudy = atr(100, inv("W"));
              xHigh = high(inv("W"));
              xLow = low(inv("W"));
              vHigh_1 = xHigh.getValue(0);
              vLow_1 = xLow.getValue(0);
              bInit = true;
              }
              if (getCurrentBarIndex() < 0) {
              if (xHigh.getValue(0) != vHigh_1 || xLow.getValue(0) != vLow_1) { // week has changed
              counter = 0; // reset counter
              }
              ++counter; // increase counter
              vStudy = xStudy.getValue(0) * 0.65;
              vHigh = hhv(counter, high());
              vLow = llv(counter, low());
              vHigh_1 = xHigh.getValue(0);
              vLow_1 = xLow.getValue(0);
              } else {
              vStudy = xStudy.getValue(0) * 0.65;
              vHigh = xHigh.getValue(0);
              vLow = xLow.getValue(0);
              }

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

              nShortATR = vHigh ; // or nShortATR = vHigh - vStudy;
              nLongATR = vLow ; // or nLongATR = vLow + vStudy;
              return new Array (nLongATR,nShortATR);
              }

              Comment

              Working...
              X