private void UnpledgeStock(OCCStockToUnpledge stu, string source, bool sendToG1, bool sendToDTC) { List <StartingPositionView> OCCStartingPositions = new List <StartingPositionView>(); StartingPositionViewDA SPVDA = new StartingPositionViewDA(); SPVDA.Load(OCCStartingPositions, "OCC", stu.RealTimePosition.ParticipantNum, stu.RealTimePosition.cusip); //If we don't have a position in it, we won't do anything. Try to return a message saying so if (OCCStartingPositions.Count > 0) { Pledge SlateUnpledgeItem = PopulateSlatePledgeObject(stu.RealTimePosition, stu.SharesToUnpledge, 0, "U", sendToG1, sendToDTC); PDA.Insert(SlateUnpledgeItem); int ReturnedG1ID = CreateTheReturnForTheUnpledge(OCCStartingPositions, source, SlateUnpledgeItem.PledgeId, stu.SharesToUnpledge, stu.RealTimePosition.ParticipantNum); int ReturnedDTCID = UnpledgeStockFromOCC(SlateUnpledgeItem.PledgeId, stu.RealTimePosition.cusip, stu.SharesToUnpledge, stu.RealTimePosition.ParticipantNum); SlateUnpledgeItem.G1Id = ReturnedG1ID; SlateUnpledgeItem.DtcId = ReturnedDTCID; PDA.Update(SlateUnpledgeItem); } }
private List <OCCStockPledge> CalculateWhatToPledge(List <OCCStockToUnpledge> stocksToUnpledge, int req) { /* 1. Get starting position to know which of our inventory are borrows so we don't pledge those * 2. Remove stocks that are on the unpledgeable list * 3. Calculate the amt we need to pledge (The required amt needed in OCC - (Amt we have pledged already - Amt we are going to unpledge) * 4. Mult amt we need to pledge by a buffer * 5. Now choose stocks we can pledge */ List <StartingPositionView> StartingPositions = new List <StartingPositionView>(); StartingPositionViewDA SPVDA = new StartingPositionViewDA(); SPVDA.Load(StartingPositions); List <OCCStockPledge> pledgeableStocks = new List <OCCStockPledge>(); //1. var BorrowCusips = StartingPositions.Where(x => x.BorrowLoanIndicator.ToLower() == "b").Select(y => y.Cusip).Distinct().ToList(); foreach (spAlanGetRealTimePositions_Pledger_Result pos in RealTimePos) { if (!BorrowCusips.Any(x => x.ToLower() == pos.cusip.ToLower()) && pos.Price != null && pos.Price > 10) { if (pos.TodaysNet >= 0 && pos.unpledgedquantity > 0) { pledgeableStocks.Add(new OCCStockPledge(pos)); } else if (pos.TodaysNet < 0 && pos.unpledgedquantity > Math.Abs(pos.TodaysNet.Value)) { pledgeableStocks.Add(new OCCStockPledge(pos)); } } } //2. UnPledgeableDA upda = new UnPledgeableDA(); List <UnPledgeableView_AlanPledger> UnPledgeable = new List <UnPledgeableView_AlanPledger>(); upda.Load(UnPledgeable); pledgeableStocks.RemoveAll(x => UnPledgeable.Exists(y => y.Cusip == x.RealTimePosition.cusip)); pledgeableStocks = pledgeableStocks.OrderByDescending(x => x.PledgeableShares * x.RealTimePosition.Price).ToList(); //3. //Amt we need to pledge = Requirement - (Find sum of what we have pledged - What we are going to unpledge) decimal AmtWeHavePledged = RealTimePos.Where(x => x.Price != null).Sum(x => x.pledgedquantity * x.Price).Value *(decimal)StockBalanceFactor; decimal AmtWeAreGoingToUnpledge = stocksToUnpledge.Where(p => p.RealTimePosition.Price != null).Sum(y => y.SharesToUnpledge * y.RealTimePosition.Price).Value; decimal WhatWeNeedToPledge = req - (AmtWeHavePledged - AmtWeAreGoingToUnpledge); //4. decimal BufferAmount = WhatWeNeedToPledge * Buffer; if (BufferAmount < 0) { BufferAmount = 0; } Dispatcher.BeginInvoke(new Action(() => { labelAmtPledging.Content = WhatWeNeedToPledge.ToString("c0"); labelBufferAmtPledging.Content = BufferAmount.ToString("c0"); })); WhatWeNeedToPledge += BufferAmount; if (WhatWeNeedToPledge < 0) { WhatWeNeedToPledge = 0; } //5. decimal current = 0; List <OCCStockPledge> StocksToPledge = new List <OCCStockPledge>(); foreach (var p in pledgeableStocks) { decimal mktval = p.PledgeableShares * p.RealTimePosition.Price.Value; decimal valueNeeded = 0; if ((current + mktval) > WhatWeNeedToPledge) { valueNeeded = WhatWeNeedToPledge - current; p.QuantityToPledge = Convert.ToInt32(Math.Round(((valueNeeded / p.RealTimePosition.Price.Value) + 1), 0)); StocksToPledge.Add(p); break; } else { current += mktval; p.QuantityToPledge = p.PledgeableShares; StocksToPledge.Add(p); } } return(StocksToPledge); }