### Calculate Portfolio Performance with Modified Dietz Source: https://github.com/openalloc/swiftmodifieddietz/blob/main/README.md Example demonstrating how to calculate portfolio performance using the Modified Dietz method. It initializes the period, market value changes, and cash flows to compute the net performance. ```swift typealias MD = ModifiedDietz let df = ISO8601DateFormatter() let beg = df.date(from: "2020-06-01T12:00:00Z")! let mid = df.date(from: "2020-06-16T00:00:00Z")! let end = df.date(from: "2020-06-30T12:00:00Z")! let period = DateInterval(start: beg, end: end) let mv = MD.MarketValueDelta(105, 100) let cf: MD.CashflowMap = [mid: -10.0] let md = MD(period, mv, cf)! print("\(md.performance * 100)%") => 5.0% ``` -------------------------------- ### Define MarketValueDelta Struct Source: https://github.com/openalloc/swiftmodifieddietz/blob/main/README.md Defines the MarketValueDelta struct which holds the starting and ending market values for a performance calculation period. Ensure the end value can be less than the start value. ```swift public struct MarketValueDelta { public let start, end: T public init(start: T, end: T) { self.start = start self.end = end } } ``` -------------------------------- ### ModifiedDietz Initialization Source: https://github.com/openalloc/swiftmodifieddietz/blob/main/README.md Two initializers are provided for the ModifiedDietz type. The first is a convenient initializer with explicit parameters, and the second uses a MarketValueDelta struct and CashflowMap typealias. Both initializers can fail and return nil if invalid parameters are provided, such as a period with zero duration. ```APIDOC ## Initialization Two initializers are provided, one more explicit than the other, but functionally equivalent: - `init?(period: DateInterval, startValue: T, endValue: T, cashflowMap: [Date: T], epsilon: T)` - Conveniently initialize a ModifiedDietz with explicit parameters. - `init?(DateInterval, ModifiedDietz.MarketValueDelta, ModifiedDietz.CashflowMap, epsilon: T)` - Initialize a ModifiedDietz with the specified parameters. Initialization will fail and return `nil` if provided nonsense parameters, such as a period with zero duration. ``` -------------------------------- ### ModifiedDietz Types Source: https://github.com/openalloc/swiftmodifieddietz/blob/main/README.md The library defines two key types: `MarketValueDelta` for tracking portfolio market values and `CashflowMap` for recording cash flow transactions. These types are generic and can be used with any `BinaryFloatingPoint` data type. ```APIDOC ## Types The `MarketValueDelta` and `CashFlowMap` types are declared within `ModifiedDietz`, where `T` is your `BinaryFloatingPoint` data type: `MarketValueDelta` specifies the beginning and ending market value for the period. Note that the `end` value can be less than the `start` value. ```swift public struct MarketValueDelta { public let start, end: T public init(start: T, end: T) { self.start = start self.end = end } } ``` `CashFlowMap` specifies the inflow (positive) or outflow (negative) of cash on particular dates. (Dates outside of period are ignored.) ```swift typealias CashflowMap = [Date: T] ``` It's often convenient to declare your own derivative type: ```swift typealias MD = ModifiedDietz ``` ``` -------------------------------- ### ModifiedDietz Properties Source: https://github.com/openalloc/swiftmodifieddietz/blob/main/README.md The ModifiedDietz struct exposes several computed properties that provide insights into the portfolio's performance and capital over the specified period. These properties are lazy and calculated only when first accessed. ```APIDOC ## Instance Properties and Methods Computed properties are lazy, meaning that they are only calculated when first needed. - `var adjustedNetCashflow: T` - Adjusted Net Cash Flow is the sum of each flow `Fi` multiplied by its weight `Wi`. Also known as total time-weighted cash flows (ttwcf) - `var adjustedPeriod: DateInterval` - The net period excludes both (1) the time until the user funds, and (2) after the user defunds. - `var averageCapital: T` - Average capital over the period. - `var gainOrLoss: T` - Total gain (or loss) over period, independent of cash flow. - `var netCashflowMap: ModifiedDietz.CashflowMap` - Valid map of cash flows for period. Includes non-zero cashflows that are within `period.start < $0 <= period.end` - `var netCashflowTotal: T` - Net external inflow (F) for the period. Also known as total net cash flows (tncf) Contributions to a portfolio are treated as positive flows while withdrawals are negative flows. - `var orderedCashflowDates: [Date]` - Ordered list of valid cash flow dates. - `var performance: T` - The calculated rate of return (R). Note: can return `NaN/Inf` if the sum of the starting market value and adjusted net cash flow is `0`. ``` -------------------------------- ### Define CashflowMap Type Alias Source: https://github.com/openalloc/swiftmodifieddietz/blob/main/README.md Defines the CashflowMap type alias as a dictionary mapping Dates to a generic BinaryFloatingPoint type T. Dates outside the specified period are ignored. ```swift typealias CashflowMap = [Date: T] ``` -------------------------------- ### Declare Custom ModifiedDietz Type Alias Source: https://github.com/openalloc/swiftmodifieddietz/blob/main/README.md Conveniently declare a custom type alias for ModifiedDietz with a specific floating-point type, such as Float. ```swift typealias MD = ModifiedDietz ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.