{"id":15038465,"url":"https://github.com/openalloc/swiftmodifieddietz","last_synced_at":"2026-03-14T20:59:17.179Z","repository":{"id":63919501,"uuid":"429636577","full_name":"openalloc/SwiftModifiedDietz","owner":"openalloc","description":"A tool for calculating portfolio performance using the Modified Dietz method","archived":false,"fork":false,"pushed_at":"2023-05-01T23:59:18.000Z","size":25,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-03-13T13:14:09.559Z","etag":null,"topics":["dietz","finance","modified-dietz","net-worth","portfolio-management","rate-of-return","swift-generics","swift-lang","swift-language","swift-library"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/openalloc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-11-19T01:49:03.000Z","updated_at":"2023-06-16T09:11:17.000Z","dependencies_parsed_at":"2024-10-12T14:42:02.918Z","dependency_job_id":"f19538ae-ff62-4a3f-8676-181f775878fc","html_url":"https://github.com/openalloc/SwiftModifiedDietz","commit_stats":{"total_commits":8,"total_committers":1,"mean_commits":8.0,"dds":0.0,"last_synced_commit":"26c654e2527c8ff2c0163a5ce013850e5144c692"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openalloc%2FSwiftModifiedDietz","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openalloc%2FSwiftModifiedDietz/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openalloc%2FSwiftModifiedDietz/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openalloc%2FSwiftModifiedDietz/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/openalloc","download_url":"https://codeload.github.com/openalloc/SwiftModifiedDietz/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243410453,"owners_count":20286400,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dietz","finance","modified-dietz","net-worth","portfolio-management","rate-of-return","swift-generics","swift-lang","swift-language","swift-library"],"created_at":"2024-09-24T20:38:36.336Z","updated_at":"2025-12-26T21:22:53.531Z","avatar_url":"https://github.com/openalloc.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SwiftModifiedDietz\n\nA tool for calculating portfolio performance using the Modified Dietz method.\n\nAvailable as an open source Swift library to be incorporated in other apps.\n\n_SwiftModifiedDietz_ is part of the [OpenAlloc](https://github.com/openalloc) family of open source Swift software tools.\n\n## ModifiedDietz\n\nFor details on the method, consult the [Modified Dietz method](https://en.wikipedia.org/wiki/Modified_Dietz_method) page on Wikipedia.\n\nAn example where the market value of a portfolio starts the month at `$105` and drops to `$100` by the end. Midway `$10` is withdrawn. The net performance is `+5.0%`.\n\n```swift\ntypealias MD = ModifiedDietz\u003cDouble\u003e\nlet df = ISO8601DateFormatter()\nlet beg = df.date(from: \"2020-06-01T12:00:00Z\")!\nlet mid = df.date(from: \"2020-06-16T00:00:00Z\")!\nlet end = df.date(from: \"2020-06-30T12:00:00Z\")!\n\nlet period = DateInterval(start: beg, end: end)\nlet mv = MD.MarketValueDelta(105, 100)\nlet cf: MD.CashflowMap = [mid: -10.0]\nlet md = MD(period, mv, cf)!\n\nprint(\"\\(md.performance * 100)%\")\n\n=\u003e 5.0%\n```\n\nNote that `performance` can return NaN if the sum of the starting market value and adjusted net cash flow is 0. Such a value is detectable with the `.isNaN` property on the return value.\n\n## Types\n\nThe `MarketValueDelta` and `CashFlowMap` types are declared within `ModifiedDietz`, where `T` is your `BinaryFloatingPoint` data type:\n\n`MarketValueDelta` specifies the beginning and ending market value for the period. Note that the `end` value can be less than the `start` value.\n\n```swift\npublic struct MarketValueDelta {\n    public let start, end: T\n    public init(start: T, end: T) {\n        self.start = start\n        self.end = end\n    }\n}   \n```\n\n`CashFlowMap` specifies the inflow (positive) or outflow (negative) of cash on particular dates. (Dates outside of period are ignored.)\n\n```swift\ntypealias CashflowMap = [Date: T]\n```\n\nIt's often convenient to declare your own derivative type:\n\n```swift\ntypealias MD = ModifiedDietz\u003cFloat\u003e\n```\n\n## Initialization\n\nTwo initializers are provided, one more explicit than the other, but functionally equivalent:\n\n- `init?(period: DateInterval, startValue: T, endValue: T, cashflowMap: [Date: T], epsilon: T)` - Conveniently initialize a ModifiedDietz with explicit parameters.\n\n- `init?(DateInterval, ModifiedDietz\u003cT\u003e.MarketValueDelta, ModifiedDietz\u003cT\u003e.CashflowMap, epsilon: T)` - Initialize a ModifiedDietz with the specified parameters.\n\nInitialization will fail and return `nil` if provided nonsense parameters, such as a period with zero duration.\n\nThe initialization values are also available as properties:\n\n- `let period: DateInterval` - The period for which performance will be calculated. NOTE: `start \u003c x \u003c= end`; exclusive of start; inclusive of end.\n\n- `let marketValue: ModifiedDietz\u003cT\u003e.MarketValueDelta` - The beginning and ending market value.\n\n- `let rawCashflowMap: ModifiedDietz\u003cT\u003e.CashflowMap` - Optional map of cash flows for dates within the period\n\n- `let epsilon: T` - Optional precision for comparing values that are very close to one another.\n\n## Instance Properties and Methods\n\nComputed properties are lazy, meaning that they are only calculated when first needed.\n\n- `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)\n\n- `var adjustedPeriod: DateInterval` - The net period excludes both (1) the time until the user funds, and (2) after the user defunds.\n\n- `var averageCapital: T` - Average capital over the period.\n\n- `var gainOrLoss: T` - Total gain (or loss) over period, independent of cash flow.\n\n- `var netCashflowMap: ModifiedDietz\u003cT\u003e.CashflowMap` - Valid map of cash flows for period. Includes non-zero cashflows that are within `period.start \u003c $0 \u003c= period.end`\n\n- `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.\n\n- `var orderedCashflowDates: [Date]` - Ordered list of valid cash flow dates.\n\n- `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`.\n\n## See Also\n\nThis library is a member of the _OpenAlloc Project_.\n\n* [_OpenAlloc_](https://openalloc.github.io) - product website for all the _OpenAlloc_ apps and libraries\n* [_OpenAlloc Project_](https://github.com/openalloc) - Github site for the development project, including full source code\n\n## License\n\nCopyright 2021, 2022 OpenAlloc LLC\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n\n[http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)\n\nUnless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n\n## Contributing\n\nContributions are welcome. You are encouraged to submit pull requests to fix bugs, improve documentation, or offer new features. \n\nThe pull request need not be a production-ready feature or fix. It can be a draft of proposed changes, or simply a test to show that expected behavior is buggy. Discussion on the pull request can proceed from there.\n\nContributions should ultimately have adequate test coverage. See tests for current entities to see what coverage is expected.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenalloc%2Fswiftmodifieddietz","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopenalloc%2Fswiftmodifieddietz","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenalloc%2Fswiftmodifieddietz/lists"}