Files
marketdata/MarketDataLib/MarketDataModel/PortfolioTrade.cs
2024-02-22 14:52:53 -05:00

122 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarketData.Utils;
using MarketData.DataAccess;
namespace MarketData.MarketDataModel
{
public class LotAggregator
{
private LotAggregator()
{
}
// combine multipe trades that share a common date, side, and price
// This is used specifically for the trade indicators in the Bollinger bands.
public static PortfolioTrades CombineLots(List<PortfolioTrade> portfolioTrades)
{
Dictionary<String,PortfolioTrade> lots=new Dictionary<String,PortfolioTrade>();
if (null == portfolioTrades||0==portfolioTrades.Count) return null;
for(int index=0;index<portfolioTrades.Count;index++)
{
PortfolioTrade portfolioTrade=portfolioTrades[index];
String key="";
if(portfolioTrade.BuySell.Equals("B"))key=portfolioTrade.BuySell+portfolioTrade.TradeDate.Date.ToShortDateString()+"-"+Utility.FormatNumber(portfolioTrade.Price,3);
else key=portfolioTrade.BuySell+portfolioTrade.TradeDate.Date.ToShortDateString()+"-"+Utility.FormatNumber(portfolioTrade.SellPrice,3);
if(!lots.ContainsKey(key)){lots.Add(key,portfolioTrade);}
else
{
PortfolioTrade tradeLot=lots[key];
tradeLot.Shares+=portfolioTrade.Shares;
}
}
PortfolioTrades combinedLots=new PortfolioTrades(lots.Values.OrderBy(x=>x.TradeDate).ToList());
return combinedLots;
}
}
// *********************************************************************************************************************************************************
public class PortfolioTrade
{
private int tradeId=-1;
private String symbol;
private DateTime tradeDate;
private double shares;
private double price;
private double commission;
private String buySell;
private String account;
private String status;
private double sellPrice=double.NaN;
private DateTime sellDate=Utility.Epoch;
public int TradeId
{
get {return tradeId ;}
set { tradeId = value; ;}
}
public String Symbol
{
get { return symbol; }
set { symbol = value; }
}
public DateTime TradeDate
{
get { return tradeDate; }
set { tradeDate = value; }
}
public double Shares
{
get { return shares; }
set { shares = value; }
}
public double Exposure()
{
return Price*Shares;
}
public String BuySell
{
get { return buySell; }
set { buySell = value; }
}
public String Status
{
get { return status; }
set { status = value; }
}
public bool IsOpen
{
get { return status.ToUpper().Equals("OPEN"); }
}
public bool IsClosed
{
get { return !IsOpen; }
}
public String Account
{
get { return account; }
set{account=value;}
}
public DateTime SellDate
{
get { return sellDate; }
set { sellDate = value; }
}
public double SellPrice
{
get { return sellPrice;}
set { sellPrice = value;}
}
public double Price
{
get { return price; }
set { price = value; }
}
public double Commission
{
get { return commission; }
set { commission = value; }
}
}
}