Beispiel #1
0
        /// <summary>
        /// Calculates the price of one currency in another base currency.
        /// </summary>
        /// <param name="baseCurrency">The base currency for the requested price.</param>
        /// <param name="CurrencyId">The identifier of the currency.</param>
        /// <returns>The price of the currency in the base currency</returns>
        public static decimal Currency(DataModel.CurrencyRow baseCurrency, DataModel.CurrencyRow currencyRow)
        {
            // Look up the price in the security's native currency.
            DataModel.PriceRow securityPriceRow =
                DataModel.Price.FindBySecurityIdCurrencyId(currencyRow.CurrencyId, baseCurrency.CurrencyId);
            if (securityPriceRow == null)
            {
                return(0.0M);
            }

            // Use the User Preferences to determine which price (Last, Closing) we should use.
            decimal securityPrice = 0.0M;

            if (Preferences.Pricing == Pricing.Last)
            {
                securityPrice = securityPriceRow.LastPrice;
            }
            if (Preferences.Pricing == Pricing.Close)
            {
                securityPrice = securityPriceRow.ClosePrice;
            }

            // The price, in the requested currency, is the native price multiplied by the cross currency price.
            return(securityPrice);
        }
Beispiel #2
0
        /// <summary>
        /// Calculates the market market value of the account and, optinally, all the sub-account.
        /// </summary>
        /// <param name="baseCurrency">The identifier of the base currency.</param>
        /// <param name="accountRow">The identifier of the account.</param>
        /// <returns>The market value of the account and, optinally, all the sub account in the base currency.</returns>
        public static decimal Calculate(DataModel.CurrencyRow baseCurrency, DataModel.AccountRow accountRow,
                                        MarketValueFlags marketValueFlags)
        {
            // This is the accumulator for market value.
            decimal marketValue = 0.0M;

            // Add up all the taxLot.
            if ((marketValueFlags & MarketValueFlags.IncludeTaxLot) != 0)
            {
                foreach (DataModel.TaxLotRow taxLotRow in accountRow.GetTaxLotRows())
                {
                    marketValue += Calculate(baseCurrency, taxLotRow);
                }
            }

            // Add up all the proposedOrder.
            if ((marketValueFlags & MarketValueFlags.IncludeProposedOrder) != 0)
            {
                foreach (DataModel.ProposedOrderRow proposedOrderRow in accountRow.GetProposedOrderRows())
                {
                    marketValue += Calculate(baseCurrency, proposedOrderRow);
                }
            }

            // Add up all the order.
            if ((marketValueFlags & MarketValueFlags.IncludeOrder) != 0)
            {
                foreach (DataModel.OrderRow SetPrice in accountRow.GetOrderRows())
                {
                    marketValue += Calculate(baseCurrency, SetPrice);
                }
            }

            // Add up all the allocation.
            if ((marketValueFlags & MarketValueFlags.IncludeAllocation) != 0)
            {
                foreach (DataModel.AllocationRow allocationRow in accountRow.GetAllocationRows())
                {
                    marketValue += Calculate(baseCurrency, allocationRow);
                }
            }

            // Add in the market value of the sub-account.
            if ((marketValueFlags & MarketValueFlags.IncludeChildAccounts) != 0)
            {
                foreach (DataModel.ObjectTreeRow objectTreeRow in
                         accountRow.ObjectRow.GetObjectTreeRowsByFKObjectObjectTreeParentId())
                {
                    foreach (DataModel.AccountRow childAccount in
                             objectTreeRow.ObjectRowByFKObjectObjectTreeChildId.GetAccountRows())
                    {
                        marketValue += Calculate(baseCurrency, childAccount, marketValueFlags);
                    }
                }
            }

            // This is the market value of the given account in the base currency.
            return(marketValue);
        }
Beispiel #3
0
 /// <summary>
 /// Calculates the market value of the given Order 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 Order in the base currency.</returns>
 private static decimal Calculate(DataModel.CurrencyRow baseCurrency, DataModel.OrderRow SetPrice)
 {
     return(SetPrice.Quantity *
            SetPrice.SecurityRowByFKSecurityOrderSecurityId.QuantityFactor *
            Price.Security(baseCurrency, SetPrice.SecurityRowByFKSecurityOrderSecurityId) *
            SetPrice.SecurityRowByFKSecurityOrderSecurityId.PriceFactor *
            SetPrice.TransactionTypeRow.QuantitySign);
 }
Beispiel #4
0
 /// <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);
 }
Beispiel #5
0
 /// <summary>
 /// Calculates the market value of the given Proposed Order 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 Proposed Order in the base currency.</returns>
 private static decimal Calculate(DataModel.CurrencyRow baseCurrency,
                                  DataModel.ProposedOrderRow proposedOrderRow)
 {
     return(proposedOrderRow.Quantity *
            proposedOrderRow.SecurityRowByFKSecurityProposedOrderSecurityId.QuantityFactor *
            Price.Security(baseCurrency, proposedOrderRow.SecurityRowByFKSecurityProposedOrderSecurityId) *
            proposedOrderRow.SecurityRowByFKSecurityProposedOrderSecurityId.PriceFactor *
            proposedOrderRow.TransactionTypeRow.QuantitySign);
 }
Beispiel #6
0
        /// <summary>
        /// Calculates the price of a security.
        /// </summary>
        /// <param name="baseCurrency">The demonimation of the price.</param>
        /// <param name="securityRow">The security to be priced.</param>
        /// <returns>The price of the security in the requested denomination.</returns>
        public static decimal Security(DataModel.CurrencyRow baseCurrency, DataModel.SecurityRow securityRow)
        {
            // Calculate the price of a Currency.
            foreach (DataModel.CurrencyRow currencyRow in securityRow.GetCurrencyRows())
            {
                return(Price.Currency(baseCurrency, currencyRow));
            }

            // Calculate the price of a Debt.
            foreach (DataModel.DebtRow debtRow in securityRow.GetDebtRowsByFKSecurityDebtDebtId())
            {
                return(Price.Debt(baseCurrency, debtRow));
            }

            // Calculate the price of an Equity.
            foreach (DataModel.EquityRow equityRow in securityRow.GetEquityRowsByFKSecurityEquityEquityId())
            {
                return(Price.Equity(baseCurrency, equityRow));
            }

            // If the security isn't one of the major securityType, then it can't be priced.
            return(0.0M);
        }
Beispiel #7
0
        /// <summary>
        /// Calculates the market value of a sector in an account.
        /// </summary>
        /// <param name="baseCurrency">The base currency for the market value calculation.</param>
        /// <param name="accountRow">The identifier of the account.</param>
        /// <param name="SecurityId">The identifier of the security.</param>
        /// <param name="marketValueFlags">These flags direct what elements are included and whether to include
        /// children.</param>
        /// <returns>The market value of the position.</returns>
        public static decimal Calculate(DataModel.CurrencyRow baseCurrency, DataModel.AccountRow accountRow,
                                        DataModel.SectorRow sectorRow, MarketValueFlags marketValueFlags)
        {
            // This is the accumulator for market value.
            decimal marketValue = 0.0M;

            // Aggregate the market value of sub-sector.
            foreach (DataModel.ObjectTreeRow objectTreeRow in
                     sectorRow.ObjectRow.GetObjectTreeRowsByFKObjectObjectTreeParentId())
            {
                foreach (DataModel.SectorRow childSector in
                         objectTreeRow.ObjectRowByFKObjectObjectTreeChildId.GetSectorRows())
                {
                    marketValue += Calculate(baseCurrency, accountRow, childSector, marketValueFlags);
                }
            }

            // Aggregate the market value of all security belonging to this sector.
            foreach (DataModel.ObjectTreeRow objectTreeRow in
                     sectorRow.ObjectRow.GetObjectTreeRowsByFKObjectObjectTreeParentId())
            {
                foreach (DataModel.SecurityRow childSecurity in
                         objectTreeRow.ObjectRowByFKObjectObjectTreeChildId.GetSecurityRows())
                {
                    // Aggregate the Long Position in this security in this account.
                    marketValue += Calculate(baseCurrency, accountRow, childSecurity, PositionType.Long,
                                             marketValueFlags);

                    // Aggregate the Short Position in this security in this account.
                    marketValue += Calculate(baseCurrency, accountRow, childSecurity, PositionType.Short,
                                             marketValueFlags);
                }
            }

            // This is the market value of the given account/sector combination.
            return(marketValue);
        }
Beispiel #8
0
        /// <summary>
        /// Calculates the price of a debt issue.
        /// </summary>
        /// <param name="baseCurrency">The base currency for the requested price.</param>
        /// <param name="debtId">The identifier of a debt.</param>
        /// <returns>The price of the debt in the base currency</returns>
        public static decimal Debt(DataModel.CurrencyRow baseCurrency, DataModel.DebtRow debtRow)
        {
            // Look up the price in the security's native currency.
            DataModel.PriceRow securityPriceRow = DataModel.Price.FindBySecurityIdCurrencyId(
                debtRow.DebtId, debtRow.SettlementId);
            if (securityPriceRow == null)
            {
                return(0.0M);
            }

            // Look up the cross currency value.  This is needed to return the value in the requested base currency.
            DataModel.PriceRow crossPriceRow = DataModel.Price.FindBySecurityIdCurrencyId(
                debtRow.SettlementId, baseCurrency.CurrencyId);
            if (crossPriceRow == null)
            {
                return(0.0M);
            }

            // Use the User Preferences to determine which price (Last, Closing) we should use.
            decimal securityPrice = 0.0M;
            decimal crossPrice    = 0.0M;

            if (Preferences.Pricing == Pricing.Last)
            {
                securityPrice = securityPriceRow.LastPrice;
                crossPrice    = crossPriceRow.LastPrice;
            }
            if (Preferences.Pricing == Pricing.Close)
            {
                securityPrice = securityPriceRow.ClosePrice;
                crossPrice    = crossPriceRow.ClosePrice;
            }

            // The price, in the requested currency, is the native price multiplied by the cross currency price and any factors.
            return(securityPrice * crossPrice);
        }
Beispiel #9
0
        /// <summary>
        /// Calculates the market value of a position.
        /// </summary>
        /// <param name="baseCurrency">The base currency for the market value calculation.</param>
        /// <param name="accountRow">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="marketValueFlags">These flags direct what elements are included and whether to include
        /// children.</param>
        /// <returns></returns>
        public static decimal Calculate(DataModel.CurrencyRow baseCurrency, DataModel.AccountRow accountRow,
                                        DataModel.SecurityRow securityRow, int PositionTypeCode, MarketValueFlags marketValueFlags)
        {
            // This is the accumulator for market value.
            decimal marketValue = 0.0M;

            // Aggregate Tax Lots
            if ((marketValueFlags & MarketValueFlags.IncludeTaxLot) != 0)
            {
                foreach (DataModel.TaxLotRow taxLotRow in accountRow.GetTaxLotRows())
                {
                    if (taxLotRow.SecurityId == securityRow.SecurityId &&
                        taxLotRow.PositionTypeCode == PositionTypeCode)
                    {
                        marketValue += Calculate(baseCurrency, taxLotRow);
                    }
                }
            }

            // Aggregate Proposed Order
            if ((marketValueFlags & MarketValueFlags.IncludeProposedOrder) != 0)
            {
                foreach (DataModel.ProposedOrderRow proposedOrderRow in accountRow.GetProposedOrderRows())
                {
                    if (proposedOrderRow.SecurityId == securityRow.SecurityId &&
                        proposedOrderRow.PositionTypeCode == PositionTypeCode)
                    {
                        marketValue += Calculate(baseCurrency, proposedOrderRow);
                    }
                }
            }

            // Aggregate Order
            if ((marketValueFlags & MarketValueFlags.IncludeOrder) != 0)
            {
                foreach (DataModel.OrderRow SetPrice in accountRow.GetOrderRows())
                {
                    if (SetPrice.SecurityId == securityRow.SecurityId &&
                        SetPrice.PositionTypeCode == PositionTypeCode)
                    {
                        marketValue += Calculate(baseCurrency, SetPrice);
                    }
                }
            }

            // Aggregate Allocation
            if ((marketValueFlags & MarketValueFlags.IncludeAllocation) != 0)
            {
                foreach (DataModel.AllocationRow allocationRow in accountRow.GetAllocationRows())
                {
                    if (allocationRow.SecurityId == securityRow.SecurityId &&
                        allocationRow.PositionTypeCode == PositionTypeCode)
                    {
                        marketValue += Calculate(baseCurrency, allocationRow);
                    }
                }
            }

            // If sub-account are to be included, recurse into the account structures.
            if ((marketValueFlags & MarketValueFlags.IncludeChildAccounts) != 0)
            {
                foreach (DataModel.ObjectTreeRow objectTreeRow in
                         accountRow.ObjectRow.GetObjectTreeRowsByFKObjectObjectTreeParentId())
                {
                    foreach (DataModel.AccountRow childAccount in
                             objectTreeRow.ObjectRowByFKObjectObjectTreeChildId.GetAccountRows())
                    {
                        marketValue += Calculate(baseCurrency, childAccount, securityRow,
                                                 PositionTypeCode, marketValueFlags);
                    }
                }
            }

            // This is the market value of the given account/security/position combination.
            return(marketValue);
        }
Beispiel #10
0
 /// <summary>
 /// Calculates the market value of the given Tax Lot 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 Tax Lot in the base currency.</returns>
 private static decimal Calculate(DataModel.CurrencyRow baseCurrency, DataModel.TaxLotRow taxLotRow)
 {
     return(taxLotRow.Quantity * taxLotRow.SecurityRow.QuantityFactor *
            Price.Security(baseCurrency, taxLotRow.SecurityRow) * taxLotRow.SecurityRow.PriceFactor);
 }