78 lines
3.0 KiB
C#
78 lines
3.0 KiB
C#
using MarketData.Generator.Interface;
|
|
using MarketData.MarketDataModel;
|
|
using MarketData.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using TradeBlotter.Interface;
|
|
using TradeBlotter.ViewModels;
|
|
|
|
namespace TradeBlotter.Extensions
|
|
{
|
|
public static class PortfolioTradesExtensions
|
|
{
|
|
|
|
public static PortfolioTrades FromSaveParams(SaveParameters saveParameters)
|
|
{
|
|
PortfolioTrades portfolioTrades=new PortfolioTrades();
|
|
int stopHistoryCount=int.Parse((from KeyValuePair<String,String> item in saveParameters where item.Key.Equals("PortfolioTradesCount") select item).FirstOrDefault().Value);
|
|
for(int index=0;index<stopHistoryCount;index++)
|
|
{
|
|
String strItemKey=String.Format("PortfolioTrade_{0}",index);
|
|
String strPortfolioTradeItem=(from KeyValuePair<String,String> item in saveParameters where item.Key.Equals(strItemKey) select item).FirstOrDefault().Value;
|
|
NVPCollection nvpCollection=new NVPCollection(strPortfolioTradeItem);
|
|
PortfolioTrade portfolioTrade=PortfolioTrade.FromNVPCollection(nvpCollection);
|
|
portfolioTrades.Add(portfolioTrade);
|
|
}
|
|
portfolioTrades=new PortfolioTrades(portfolioTrades.OrderBy(x => x.TradeDate).ToList());
|
|
return portfolioTrades;
|
|
}
|
|
|
|
public static SaveParameters FromPosition(IPositionModel position)
|
|
{
|
|
PortfolioTrades portfolioTrades = new PortfolioTrades();
|
|
SaveParameters saveParameters = new SaveParameters();
|
|
|
|
if(null == position)
|
|
{
|
|
return saveParameters;
|
|
}
|
|
|
|
PortfolioTrade portfolioTrade = new PortfolioTrade()
|
|
{
|
|
Symbol = position.Symbol,
|
|
TradeDate = position.PurchaseDate,
|
|
Shares = position.Shares,
|
|
Price = position.PurchasePrice,
|
|
SellPrice = Utility.IsEpoch(position.SellDate)?double.NaN:position.CurrentPrice,
|
|
SellDate = position.SellDate ,
|
|
BuySell= Utility.IsEpoch(position.SellDate)?"B":"S",
|
|
Status= Utility.IsEpoch(position.SellDate)?"OPEN":"CLOSED",
|
|
};
|
|
portfolioTrades.Add(portfolioTrade);
|
|
return FromPortfolioTrades(portfolioTrades);
|
|
}
|
|
|
|
public static SaveParameters FromPortfolioTrades(PortfolioTrades portfolioTrades)
|
|
{
|
|
SaveParameters saveParameters = new SaveParameters();
|
|
|
|
if(null == portfolioTrades || 0==portfolioTrades.Count)
|
|
{
|
|
return saveParameters;
|
|
}
|
|
StringBuilder sb = new StringBuilder();
|
|
NVPCollections nvpCollections = portfolioTrades.ToNVPCollections();
|
|
sb.Append("PortfolioTradesCount").Append(",").Append(String.Format("{0}", nvpCollections.Count)).Append(",");
|
|
for (int index = 0; index < nvpCollections.Count; index++)
|
|
{
|
|
sb.Append(String.Format("PortfolioTrade_{0}", index)).Append(",").Append(nvpCollections[index].ToString());
|
|
if (index < nvpCollections.Count - 1) sb.Append(",");
|
|
}
|
|
return SaveParameters.Parse(sb.ToString());
|
|
}
|
|
}
|
|
}
|