Files
2024-02-22 14:52:53 -05:00

143 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MarketData.Utils;
using System.Collections.ObjectModel;
using MarketData.Generator.GainLoss;
using MarketData.DataAccess;
namespace MarketData.MarketDataModel.GainLoss
{
// *********************************************************************************************************************************************************************
// ************************************************************************ G A I N L O S S **************************************************************************
// *********************************************************************************************************************************************************************
// This gain loss provides a picture of the Active Gain/Loss. The gain loss on open positions
public class GainLossItem : IComparable
{
private DateTime date;
private double gainLoss;
private double gainLossPercent;
private double exposure;
private double dividends;
private bool valueIsPercent;
public GainLossItem()
{
}
public GainLossItem(DateTime date, double gainLoss,double exposure,bool valueIsPercent)
{
this.date = date;
this.gainLoss = gainLoss;
this.exposure = exposure;
this.valueIsPercent = valueIsPercent;
}
public GainLossItem(DateTime date, double gainLoss,double gainLossPercent,double exposure,bool valueIsPercent)
{
this.date = date;
this.gainLoss = gainLoss;
this.gainLossPercent=gainLossPercent;
this.exposure = exposure;
this.valueIsPercent = valueIsPercent;
}
public GainLossItem(DateTime date, double gainLoss,double gainLossPercent,double exposure,double dividends,bool valueIsPercent)
{
this.date = date;
this.gainLoss = gainLoss;
this.gainLossPercent=gainLossPercent;
this.exposure = exposure;
this.dividends=dividends;
this.valueIsPercent = valueIsPercent;
}
public DateTime Date
{
get { return date; }
}
public double Exposure
{
get
{
return exposure;
}
}
public double GainLoss
{
get { return gainLoss; }
}
public double Dividends
{
get{return dividends;}
}
public double GainLossPercent
{
get { return gainLossPercent; }
}
public bool ValueIsPercent
{
get
{
return valueIsPercent;
}
set
{
valueIsPercent = value;
}
}
public String FormattedGainLoss
{
get
{
if (valueIsPercent) return Utility.FormatNumber(gainLoss);
return Utility.FormatCurrency(gainLoss);
}
}
public int CompareTo(Object obj)
{
if (!obj.GetType().IsInstanceOfType(this)) throw new Exception("Expected GainLoss");
GainLossItem that = (GainLossItem)obj;
return date.CompareTo(that.Date);
}
}
public class GainLossCollection : List<GainLossItem>
{
public GainLossCollection(ICollection<GainLossItem> gainLoss)
: base(gainLoss)
{
}
public DMAValues DMAValues
{
get
{
DMAValues dmaValues = new DMAValues();
foreach (GainLossItem gainLoss in this)
{
dmaValues.Add(new DMAValue(gainLoss.Date,gainLoss.GainLoss));
}
return dmaValues;
}
}
public void WriteToDisk(String strPathFileName)
{
FileStream outStream=new FileStream(strPathFileName,FileMode.Create);
StreamWriter streamWriter=new StreamWriter(outStream);
streamWriter.WriteLine("Date,GainLoss,GainLossPercent,Exposure,Dividends,ValueIsPercent");
foreach(GainLossItem item in this)
{
StringBuilder sb=new StringBuilder();
sb.Append(item.Date.ToShortDateString()).Append(",");
sb.Append(item.GainLoss).Append(",");
sb.Append(item.GainLossPercent).Append(",");
sb.Append(item.Exposure).Append(",");
sb.Append(item.Dividends).Append(",");
sb.Append(item.ValueIsPercent);
streamWriter.WriteLine(sb.ToString());
}
streamWriter.Flush();
streamWriter.Close();
streamWriter.Dispose();
outStream.Close();
outStream.Dispose();
}
}
}