// Path: src/main/java/net/tradelib/functors/Atr.java
// public class Atr {
//    public enum MaType { SMA, EMA }
//    private IMovingAverage ima;
//    private int n;
//    private double dn;
//    private double close; // Previous close
//    int count;
//    
//    public Atr(int n) {
//       this.count = 0;
//       this.ima = new Ema(n, true);
//       this.n = n;
//       this.dn = n;
//    }
//    
//    public Atr(int n, boolean wilder) {
//       this.count = 0;
//       this.ima = new Ema(n, wilder);
//       this.n = n;
//       this.dn = n;
//    }
//    
//    public Atr(int n, MaType mt) {
//       this.count = 0;
//       if(mt == MaType.SMA) {
//          this.ima = new Sma(n);
//       } else {
//          this.ima = new Ema(n, true);
//       }
//       this.n = n;
//       this.dn = n;
//    }
//    
//    public int getLength() { return n; }
//    public int getCount() { return count; }
//    
//    public double add(double high, double low, double close) {
//       
//       assert high >= low && high >= close && low <= close;
//       
//       if(count > 0) {
//          double trueHigh = Math.max(high, this.close); 
//          double trueLow = Math.min(low, this.close);
//          ima.add(trueHigh - trueLow);
//          this.close = close;
//       } else {
//          // No previous close - use high/low
//          // ema_.add(high.subtract(low));
//          this.close = close;
//       }
//       
//       ++count;
//       
//       return last();
//    }
//    
//    public double last() { return ima.last(); }
// }
// 
// Path: src/main/java/net/tradelib/functors/Atr.java
// public enum MaType { SMA, EMA }


import static org.junit.Assert.*;
import net.tradelib.functors.Atr;
import net.tradelib.functors.Atr.MaType;
import org.junit.Test;
// Copyright 2015 by Ivan Popivanov
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package net.tradelib.functors;



public class AtrTest {

   @Test
   public void test() {
      String [] close = {"124.41","125.34","125.68","125.92","127.32","129.32","131.65","131.39","129.65","131.07",
                         "131.82","131.82","132.82","134.91","133.19","134.96","133.4","132.92","134.77","134.36"};
      String [] high = {"124.59","125.66","126.21","126.17","127.66","129.63","132.49","131.97","130.37","131.11",
                        "132.31","132.61","134.12","135.03","134.68","135.11","135.59","134.18","135.12","135.19"};
      String [] low = {"124.02","124.81","125.53","125.65","126.15","127.96","130.19","130.17","129.42","129.04",
                       "130.92","131.14","132.7","132.68","133.03","134.07","132.96","132.28","134.15","134.05"};
      String [] expected = {null,null,null,null,null,null,null,null,null,null,"1.709","1.6851","1.74659","1.806931",
                            "1.814238","1.824814","1.905333","1.904799","1.934319","1.854888"};

      Atr atr = new Atr(10);
      for(int ii = 0; ii < close.length; ++ii) {
         atr.add(Double.parseDouble(high[ii]), Double.parseDouble(low[ii]), Double.parseDouble(close[ii]));
         double val = atr.last();
         if(expected[ii] == null) {
            assertTrue(Double.isNaN(val));
         } else {
            assertEquals(val, Double.parseDouble(expected[ii]), 1e-6);
         }
      }
      
      String [] expectedRegular = {null,null,null,null,null,null,null,null,null,null,
            "1.709","1.665545","1.780901","1.884373","1.883578","1.8902","2.024709","2.002035","2.038029","1.874751"};

// Path: src/main/java/net/tradelib/functors/Atr.java
// public class Atr {
//    public enum MaType { SMA, EMA }
//    private IMovingAverage ima;
//    private int n;
//    private double dn;
//    private double close; // Previous close
//    int count;
//    
//    public Atr(int n) {
//       this.count = 0;
//       this.ima = new Ema(n, true);
//       this.n = n;
//       this.dn = n;
//    }
//    
//    public Atr(int n, boolean wilder) {
//       this.count = 0;
//       this.ima = new Ema(n, wilder);
//       this.n = n;
//       this.dn = n;
//    }
//    
//    public Atr(int n, MaType mt) {
//       this.count = 0;
//       if(mt == MaType.SMA) {
//          this.ima = new Sma(n);
//       } else {
//          this.ima = new Ema(n, true);
//       }
//       this.n = n;
//       this.dn = n;
//    }
//    
//    public int getLength() { return n; }
//    public int getCount() { return count; }
//    
//    public double add(double high, double low, double close) {
//       
//       assert high >= low && high >= close && low <= close;
//       
//       if(count > 0) {
//          double trueHigh = Math.max(high, this.close); 
//          double trueLow = Math.min(low, this.close);
//          ima.add(trueHigh - trueLow);
//          this.close = close;
//       } else {
//          // No previous close - use high/low
//          // ema_.add(high.subtract(low));
//          this.close = close;
//       }
//       
//       ++count;
//       
//       return last();
//    }
//    
//    public double last() { return ima.last(); }
// }
// 
// Path: src/main/java/net/tradelib/functors/Atr.java
// public enum MaType { SMA, EMA }


// Path: src/test/java/net/tradelib/functors/AtrTest.java
import static org.junit.Assert.*;
import net.tradelib.functors.Atr;
import net.tradelib.functors.Atr.MaType;
import org.junit.Test;

// Copyright 2015 by Ivan Popivanov
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package net.tradelib.functors;



public class AtrTest {

   @Test
   public void test() {
      String [] close = {"124.41","125.34","125.68","125.92","127.32","129.32","131.65","131.39","129.65","131.07",
                         "131.82","131.82","132.82","134.91","133.19","134.96","133.4","132.92","134.77","134.36"};
      String [] high = {"124.59","125.66","126.21","126.17","127.66","129.63","132.49","131.97","130.37","131.11",
                        "132.31","132.61","134.12","135.03","134.68","135.11","135.59","134.18","135.12","135.19"};
      String [] low = {"124.02","124.81","125.53","125.65","126.15","127.96","130.19","130.17","129.42","129.04",
                       "130.92","131.14","132.7","132.68","133.03","134.07","132.96","132.28","134.15","134.05"};
      String [] expected = {null,null,null,null,null,null,null,null,null,null,"1.709","1.6851","1.74659","1.806931",
                            "1.814238","1.824814","1.905333","1.904799","1.934319","1.854888"};

      Atr atr = new Atr(10);
      for(int ii = 0; ii < close.length; ++ii) {
         atr.add(Double.parseDouble(high[ii]), Double.parseDouble(low[ii]), Double.parseDouble(close[ii]));
         double val = atr.last();
         if(expected[ii] == null) {
            assertTrue(Double.isNaN(val));
         } else {
            assertEquals(val, Double.parseDouble(expected[ii]), 1e-6);
         }
      }
      
      String [] expectedRegular = {null,null,null,null,null,null,null,null,null,null,
            "1.709","1.665545","1.780901","1.884373","1.883578","1.8902","2.024709","2.002035","2.038029","1.874751"};

      atr = new Atr(10, false);