using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using MarketData.MarketDataModel;
using MarketData.Utils;
using MarketData.DataAccess;
using MarketData.Numerical;

namespace MarketData.Generator.ModelGenerators
{
  public class TrueRangeGenerator
  {
    private TrueRangeGenerator()
    {
    }
    // Prices : Where most recent price is at lowest index
    public static TrueRangeResult GenerateAverageTrueRange(String symbol, DateTime startDate, int atrDays = 14)
    {
      Prices prices = PricingDA.GetPrices(symbol, startDate, atrDays + 1);
      if (null == prices || prices.Count < 2 || 0 == prices[0].Close) return null;
      double[] values = new double[prices.Count - 1];
      for (int index = 0; index < prices.Count - 1; index++)
      {
        Price price = prices[index];
        Price prevPrice = prices[index + 1];
        double levelA = price.High - price.Low;
        double levelB = Math.Abs(price.High - prevPrice.Close);
        double levelC = Math.Abs(price.Low - prevPrice.Close);
        values[index] = Math.Max(levelA, levelB);
        values[index] = Math.Max(values[index], levelC);
      }
      double averageTrueRange = Numerics.Mean(ref values);
      return new TrueRangeResult(averageTrueRange, (averageTrueRange / prices[0].Close) * 2.00);
    }
  }
  public class TrueRangeResult
  {
    public TrueRangeResult()
    {
    }
    public TrueRangeResult(double averageTrueRange,double shortTermVolatility)
    {
      AverageTrueRange=averageTrueRange;
      ShortTermVolatility=shortTermVolatility;
    }
    public double AverageTrueRange{get;set;}
    public double ShortTermVolatility{get;set;}
  }
}
