Announcement

Collapse
No announcement yet.

Stop and Reverse ATR Trailing Stop

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

  • Stop and Reverse ATR Trailing Stop

    I've a stop and reverse study and would like to write it into efs.

    It's similar to Parabolic SAR stop and reverse in nature but ulitizing a simple ATR trailing stop; 2.5 multiples of 5 ATR below the high, then when tripped by price it reverses and becomes the short stop above the lows.

    Can anyone help? Point me in the right direction?

    Rgds, James

    PHP Code:
    function init()
    {
      
    setTitle("Trailing Stop 1");

      
    setSeriesColour(0Colour.Yellow);
      
    setSeriesLineStyle(0Pen.Solid);
      
    setRange(Range.Parent);
    }

    function 
    getGraph(sharedata)
    {
      var 
    trail = new Array();
      var 
    at 5//ATR periods
      
    var mult 2.5//ATR multiplier (smaller number = tighter stop)
      
    var a_ma = new MA(atMA.Simple);
      var 
    tr = new Array();
      var 
    tra = new Array();
      var 
    upperlowerhilo;
      var 
    test = new Array();

      
    trail[0] = data[0].close;
      for (var 
    i=1i<data.lengthi++)
      {
        
    hi Math.max(data[i].high,data[i-1].close);
        
    lo Math.min(data[i].low,data[i-1].close);
        
    tr[i] = hi lo;
        
    tra[i] = a_ma.getNext(tr[i]);
        
    upper data[i].low mult*tra[i];
        
    lower data[i].high mult*tra[i];
        
    test[i] = lower;
        if (
    data[i].high trail[i-1] && data[i-1].high trail[i-2]) trail[i] = lower;
        else if (
    trail[i-1] > data[i].low && trail[i-2] < data[i-1].lowtrail[i] = upper;
        else if (
    data[i].low trail[i-1]) trail[i] = Math.min(trail[i-1],upper);
        else if (
    data[i].high trail[i-1]) trail[i] = Math.max(trail[i-1],lower);
        else 
    trail[i] = trail[i-1];    
      }
      return 
    trail;//trail;


  • #2
    I've written the formula below. It's based off the code in my previous post. I've been studing the tutorials but (not surprizingly) there seem an error or two. I'm not sure if I can just call the "high" "low" or need to getValue first. Could anyone run an eye over it?? I'm trying to achieve a Stop and Reverse Trailing stop based off 2x5periodATR above/below the low/high.

    PHP Code:
    function preMain() {
        
    setPriceStudy(true);
        
    setStudyTitle("SAR");
        
    setCursorLabelName("VStop"0);
        
    setDefaultBarThickness(10);
        
    setDefaultBarFgColor(Color.yellow0);
        
        
    setShowTitleParameters(false);
        
        
    // Formula Parameters
        
    var fp1 = new FunctionParameter("nATRlen"FunctionParameter.NUMBER);
            
    fp1.setName("ATR Periods");
            
    fp1.setLowerLimit(1);
            
    fp1.setDefault(5);
    }

    var 
    bEdit true;
    var 
    vATR null;

    function 
    main(nATRlen) {
        if (
    bEdit == true) {
            
    vATR = new ATRStudy(nATRlen);
            
    bEdit false;
        }
        
        var 
    nState getBarState();
        if (
    nState == BARSTATE_NEWBAR) {
        }
        
        var 
    ATR vATR.getValue(ATRStudy.ATR);
        if (
    ATR == null) return;
        
        var 
    low low();
        var 
    high high();
        
        var 
    trail[0] = close[0];
        for (var 
    1lengthi++) {
        
    upper low 2*vATR[i];
        
    lower high 2*vATR[i];
        
    test[i] = lower;
        if (
    high[i] > trail[i-1] && high[i-1] < trail[i-2]) trail[i] = lower;
        else if (
    trail[i-1] > low[i] && trail[i-2] < low[i-1]) trail[i] = upper;
        else if (
    low[i] < trail[i-1]) trail[i] = Math.min(trail[i-1],upper);
        else if (
    high[i] > trail[i-1]) trail[i] = Math.max(trail[i-1],lower);
        else 
    trail[i] = trail[i-1];
    }
        
        return 
    trail;

    Comment

    Working...
    X