/// <summary> /// Calculates the market value of the given Allocation in the base currency. /// </summary> /// <param name="baseCurrency">The base currency for the market value calculation.</param> /// <param name="proposedOrderRow">The subject of the calculation.</param> /// <returns>The market value of the Allocation in the base currency.</returns> private static decimal Calculate(DataModel.CurrencyRow baseCurrency, DataModel.AllocationRow allocationRow) { return(allocationRow.Quantity * allocationRow.SecurityRowByFKSecurityAllocationSecurityId.QuantityFactor * Price.Security(baseCurrency, allocationRow.SecurityRowByFKSecurityAllocationSecurityId) * allocationRow.SecurityRowByFKSecurityAllocationSecurityId.PriceFactor * allocationRow.TransactionTypeRow.QuantitySign); }
/// <summary> /// Calculates the number of shares in a given position. /// </summary> /// <param name="baseCurrency">The base currency for the market value calculation.</param> /// <param name="AccountId">The identifier of the account.</param> /// <param name="SecurityId">The identifier of the security.</param> /// <param name="positionTypeCode">The PositionTypeCode (long or short).</param> /// <param name="quantityFlags">These flags direct what elements are included and whether to include /// children.</param> /// <returns></returns> public static decimal Calculate(DataModel.AccountRow accountRow, DataModel.SecurityRow securityRow, int positionTypeCode, MarketValueFlags quantityFlags) { // This is the accumulator for market value. decimal quantity = 0.0M; // This key is used to find records that match the given position. object[] key = new object[] { accountRow.AccountId, securityRow.SecurityId, positionTypeCode }; // Aggregate Tax Lots if ((quantityFlags & MarketValueFlags.IncludeTaxLot) != 0) { foreach (DataRowView dataRowView in DataModel.TaxLot.UKTaxLotAccountIdSecurityIdPositionTypeCode.FindRows(key)) { quantity += ((DataModel.TaxLotRow)dataRowView.Row).Quantity; } } // Aggregate Proposed Order if ((quantityFlags & MarketValueFlags.IncludeProposedOrder) != 0) { foreach (DataRowView dataRowView in DataModel.ProposedOrder.UKProposedOrderAccountIdSecurityIdPositionTypeCode.FindRows(key)) { DataModel.ProposedOrderRow proposedProposedOrderRow = (DataModel.ProposedOrderRow)dataRowView.Row; quantity += proposedProposedOrderRow.Quantity * proposedProposedOrderRow.TransactionTypeRow.QuantitySign; } } // Aggregate Order if ((quantityFlags & MarketValueFlags.IncludeOrder) != 0) { foreach (DataRowView dataRowView in DataModel.Order.UKOrderAccountIdSecurityIdPositionTypeCode.FindRows(key)) { DataModel.OrderRow orderRow = (DataModel.OrderRow)dataRowView.Row; quantity += orderRow.Quantity * orderRow.TransactionTypeRow.QuantitySign; } } // Aggregate Allocation if ((quantityFlags & MarketValueFlags.IncludeAllocation) != 0) { foreach (DataRowView dataRowView in DataModel.Allocation.UKAllocationAccountIdSecurityIdPositionTypeCode.FindRows(key)) { DataModel.AllocationRow allocationRow = (DataModel.AllocationRow)dataRowView.Row; quantity += allocationRow.Quantity * allocationRow.TransactionTypeRow.QuantitySign; } } // If sub-account are to be included, recurse into the account structures. if ((quantityFlags & MarketValueFlags.IncludeChildAccounts) != 0) { foreach (DataModel.ObjectTreeRow objectTreeRow in accountRow.ObjectRow.GetObjectTreeRowsByFKObjectObjectTreeParentId()) { foreach (DataModel.AccountRow childAccount in objectTreeRow.ObjectRowByFKObjectObjectTreeChildId.GetAccountRows()) { quantity += Calculate(childAccount, securityRow, positionTypeCode, quantityFlags); } } } // This is the market value of the given account/security/position combination. return(quantity); }