Fix issue with closing hedge positions in MGSHMomentum

This commit is contained in:
2025-08-04 11:53:47 -04:00
parent 4591b7203f
commit 21ca55f41a
2 changed files with 15 additions and 7 deletions

View File

@@ -160,7 +160,14 @@ namespace MarketData.Generator.MGSHMomentum
MGSHPositions activePositions = ActivePositions.GetPositions();
MGSHPosition position=activePositions.Where(x => x.Symbol.Equals(symbol) && x.PurchaseDate.Equals(purchaseDate)).FirstOrDefault();
if(null==position) // if it is not in the active positions then the position is already closed and we are modifying either the sell date or the sell price
bool isHedgePosition=false;
if(null==position) // It could be an open hedge position that we are closing
{
MGSHPositions hedgePositions = HedgePositions;
position = hedgePositions.Where(x => x.Symbol.Equals(symbol) && x.PurchaseDate.Equals(purchaseDate)).FirstOrDefault();
isHedgePosition=true;
}
if(null==position) // if it is not in the active positions or hedge positions then the position is already closed and we are modifying either the sell date or the sell price
{
position=AllPositions.Where(x => x.Symbol.Equals(symbol)&&x.PurchaseDate.Equals(purchaseDate)).FirstOrDefault();
if(null==position)
@@ -179,7 +186,8 @@ namespace MarketData.Generator.MGSHMomentum
position.CurrentPrice=price;
position.Comment="Manual close.";
CashBalance+=position.MarketValue;
ActivePositions.Remove(position);
if(!isHedgePosition)ActivePositions.Remove(position);
else HedgePositions.Remove(position);
AllPositions.Add(position);
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Position for symbol '{0}' purchased on {1} is now closed.",symbol,purchaseDate.ToShortDateString()));
SaveSession();