Add
This commit is contained in:
77
Extensions/PortfolioTradesExtensions.cs
Normal file
77
Extensions/PortfolioTradesExtensions.cs
Normal file
@@ -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<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());
|
||||
}
|
||||
}
|
||||
}
|
||||
48
Extensions/StopLimitExtensions.cs
Normal file
48
Extensions/StopLimitExtensions.cs
Normal file
@@ -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<String,String> item in saveParameters where item.Key.Equals("StopHistoryCount") select item).FirstOrDefault().Value);
|
||||
for(int index=0;index<stopHistoryCount;index++)
|
||||
{
|
||||
String strItemKey=String.Format("StopHistory_{0}",index);
|
||||
String strStopHistoryItem=(from KeyValuePair<String,String> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user