using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using MarketData.Utils;

namespace MarketData.MarketDataModel
{
  public class MACDFastSlowSignalsComparerDesc : IComparer<MACDFastSlowSignal>
  {
    public int Compare(MACDFastSlowSignal macd1, MACDFastSlowSignal macd2)
    {
      if (macd1.Date < macd2.Date) return -1;
      else if (macd1.Date > macd2.Date) return 1;
      return 0;
    }
  }
  public class MACDFastSlowSignalsComparerAsc : IComparer<MACDFastSlowSignal>
  {
    public int Compare(MACDFastSlowSignal macd1, MACDFastSlowSignal macd2)
    {
      if (macd1.Date > macd2.Date) return -1;
      else if (macd1.Date < macd2.Date) return 1;
      return 0;
    }
  }
  public class MACDFastSlowSignals : List<MACDFastSlowSignal>
  {
    private String symbol;
    private DateTime fromDate;
    private DateTime thruDate;
    private DateTime date;
    public MACDFastSlowSignals()
    {
    }
    public MACDFastSlowSignals(ICollection<MACDFastSlowSignal> collection)
    {
      IEnumerator<MACDFastSlowSignal> enumerator=collection.GetEnumerator();
      while (enumerator.MoveNext()) Add(enumerator.Current);
    }
    public Signals FastCondensedSignals{get;set;}
    public Signals SlowCondensedSignals{get;set;}
    public double MinFastMACD
    {
      get{return (from MACDFastSlowSignal signal in this select signal.MACDFast).Min()-.25;}
    }
    public double MinSlowMACD
    {
      get{return (from MACDFastSlowSignal signal in this select signal.MACDSlow).Min()-.25;}
    }
    public String Symbol
    {
      get { return symbol; }
      set { symbol = value; }
    }
    public DateTime Date
    {
      get { return date; }
      set { date = value; }
    }
    public DateTime FromDate
    {
      get { return fromDate; }
      set { fromDate = value; }
    }
    public DateTime ThruDate
    {
      get { return thruDate; }
      set { thruDate = value; }
    }
  }
  public class MACDFastSlowSignal
  {
    private DateTime date;
    private double signalFast;   
    private double macdFast;
    private double signalSlow;
    private double macdSlow;

    public MACDFastSlowSignal()
    {
    }
    public DateTime Date
    {
      get { return date; }
      set { date = value; }
    }
    public double SignalFast
    {
      get { return signalFast; }
      set { signalFast = value; }
    }
    public double MACDFast
    {
      get { return macdFast; }
      set { macdFast = value; }
    }
    public double SignalSlow
    {
      get { return signalSlow; }
      set { signalSlow = value; }
    }
    public double MACDSlow
    {
      get { return macdSlow; }
      set { macdSlow = value; }
    }
  }
  public class MACDSignals : List<MACDSignal>
  {
    public MACDSignals()
    {
    }
    public float[] GetMACD(int startIndex, int count)
    {
      if (startIndex + count > Count) return null;
      float[] macdArray = new float[count];
      for (int index = startIndex, arrayIndex = 0; index < startIndex + count; index++, arrayIndex++)
      {
        macdArray[arrayIndex] = (float)this[index].MACD;
      }
      return macdArray;
    }
  }
  public class MACDSignal
  {
    private String symbol;
    private DateTime date;
    private double fast;      // fast  12
    private double slow;      // slow  26
    private double signal;   // signal 9
    private double macd;
    private double histogram;

    public MACDSignal()
    {
    }
    public String Symbol
    {
      get {return symbol; }
      set { symbol = value; }
    }
    public DateTime Date
    {
      get { return date; }
      set { date = value; }
    }
    public double Fast
    {
      get { return fast; }
      set { fast = value; }
    }
    public double Slow
    {
      get { return slow; }
      set { slow = value; }
    }
    public double Signal
    {
      get { return signal; }
      set { signal = value; }
    }
    public double MACD
    {
      get { return macd; }
      set { macd = value; }
    }
    public double Histogram
    {
      get { return histogram; }
      set { histogram = value; }
    }
    public static String Header
    {
      get
      {
        return "Symbol,Date,Fast,Slow,Signal,MACD,Histogram";
      }
    }
    public override String ToString()
    {
      StringBuilder sb=new StringBuilder();
      sb.Append(symbol).Append(",");
      sb.Append(Utility.DateTimeToStringMMSDDSYYYY(Date)).Append(",");
      sb.Append(String.Format("{0:0.00000}",Fast)).Append(",");
      sb.Append(String.Format("{0:0.00000}",Slow)).Append(",");
      sb.Append(String.Format("{0:0.00000}",Signal)).Append(",");
      sb.Append(String.Format("{0:0.00000}",MACD)).Append(",");
      sb.Append(String.Format("{0:0.00000}",Histogram));
      return sb.ToString();
    }
  }
}
