diff --git a/Extensions/PortfolioTradesExtensions.cs b/Extensions/PortfolioTradesExtensions.cs new file mode 100644 index 0000000..cd9e7c7 --- /dev/null +++ b/Extensions/PortfolioTradesExtensions.cs @@ -0,0 +1,77 @@ +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 item in saveParameters where item.Key.Equals("PortfolioTradesCount") select item).FirstOrDefault().Value); + for(int index=0;index 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()); + } + } +} diff --git a/Extensions/StopLimitExtensions.cs b/Extensions/StopLimitExtensions.cs new file mode 100644 index 0000000..0ebfc80 --- /dev/null +++ b/Extensions/StopLimitExtensions.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MarketData.MarketDataModel; +using System.Runtime.CompilerServices; +using TradeBlotter.ViewModels; +using MarketData.Utils; + +namespace TradeBlotter.Extensions +{ + public static class StopLimitsExtensions + { + public static StopLimits FromSaveParams(SaveParameters saveParameters) + { + StopLimits stopLimits=new StopLimits(); + int stopHistoryCount=int.Parse((from KeyValuePair item in saveParameters where item.Key.Equals("StopHistoryCount") select item).FirstOrDefault().Value); + for(int index=0;index item in saveParameters where item.Key.Equals(strItemKey) select item).FirstOrDefault().Value; + NVPCollection nvpCollection=new NVPCollection(strStopHistoryItem); + StopLimit stopLimit=MarketData.MarketDataModel.StopLimit.FromNVPCollection(nvpCollection); + stopLimits.Add(stopLimit); + } + stopLimits=new StopLimits(stopLimits.OrderBy(x => x.EffectiveDate).ToList()); + return stopLimits; + } + + public static SaveParameters FromStopLimits(this StopLimits stopLimits) + { + StringBuilder sb = new StringBuilder(); + if(null==stopLimits || 0==stopLimits.Count)return new SaveParameters(); + NVPCollections nvpCollections = stopLimits.ToNVPCollections(); + sb.Append("StopHistoryCount").Append(",").Append(String.Format("{0}", nvpCollections.Count)).Append(","); + for (int index = 0; index < nvpCollections.Count; index++) + { + sb.Append(String.Format("StopHistory_{0}", index)).Append(",").Append(nvpCollections[index].ToString()); + if (index < nvpCollections.Count - 1) sb.Append(","); + } + return SaveParameters.Parse(sb.ToString()); + } + } +} + + +