Skip to content

Instantly share code, notes, and snippets.

@pheymann
Last active April 16, 2021 17:30
Show Gist options
  • Save pheymann/ccb80e82ad2b633f6a5d8f207648579b to your computer and use it in GitHub Desktop.
Save pheymann/ccb80e82ad2b633f6a5d8f207648579b to your computer and use it in GitHub Desktop.
// based on https://github.com/pheymann/investment-library
// to execute follow: https://github.com/pheymann/investment-library#how-to-install-and-use-it-with-a-docker-based-setup
/* Result: 50,000 EUR capital and 40 years runtime (ignoring dividends for now)
* - lump sum investment at the beginning leads to: 274383.125EUR
* - continues investment over 40 years leads to: 130788.584EUR
* - spread investment over the first 5 years leads to: 250692.769EUR
*
* Assumption: flat 25% capital gains tax.
*/
import $ivy.`com.github.pheymann::investment-library:0.0.0`
import investment._, units._, distributions._
// this stock's value growth 5% annually
val valueGrowth: ReturnDistribution[Year] = year => {
Percentage.fromInt(5)
}
// this stock pays 1% dividends annually
val dividends: ReturnDistribution[Year] = year => {
Percentage.fromInt(1)
}
// lumpsum investment of 50,000EUR
{
val orderFlow: OrderDistribution[Year] = (year, balance) => {
// sell all after 40 years
if (year == Year(40)) {
balance.addOrder(Order.Liquidation)
}
// lump sum at the beginning
else if (year == Year(0)) {
balance.addOrder(Order.Purchase(Capital(50000)))
}
else {
balance
}
}
// invest for 10 years with an initial stock share price of 1.
val result = stocks.invest(
investmentLifetime = Years.forYears(40),
initialSharePrice = Capital(1),
orderDist = orderFlow,
valueGrowthReturnDist = valueGrowth,
dividendDist = dividends,
taxation = Taxation.fifoBasedTaxation(capitalGainsTax = Percentage.fromInt(25))
)
println("##### Lumpsum investment of 50,000EUR")
println(prettyPrint(result))
}
// continues investment of 50,000EUR
{
val orderFlow: OrderDistribution[Year] = (year, balance) => {
// sell all after 40 years
if (year == Year(40)) {
balance.addOrder(Order.Liquidation)
}
else {
balance.addOrder(Order.Purchase(Capital.fromDouble(50000/40)))
}
}
// invest for 10 years with an initial stock share price of 1.
val result = stocks.invest(
investmentLifetime = Years.forYears(40),
initialSharePrice = Capital(1),
orderDist = orderFlow,
valueGrowthReturnDist = valueGrowth,
dividendDist = dividends,
taxation = Taxation.fifoBasedTaxation(capitalGainsTax = Percentage.fromInt(25))
)
println("\n\n##### Cont inues investment of 50,000EUR")
println(prettyPrint(result))
}
// Spread investment of 50,000EUR
{
val orderFlow: OrderDistribution[Year] = (year, balance) => {
// sell all after 40 years
if (year == Year(40)) {
balance.addOrder(Order.Liquidation)
}
else if (year >= Year(0) && year <= Year(4)) {
balance.addOrder(Order.Purchase(Capital.fromDouble(50000/5)))
}
else {
balance
}
}
// invest for 40 years with an initial stock share price of 1.
val result = stocks.invest(
investmentLifetime = Years.forYears(40),
initialSharePrice = Capital(1),
orderDist = orderFlow,
valueGrowthReturnDist = valueGrowth,
dividendDist = dividends,
taxation = Taxation.fifoBasedTaxation(capitalGainsTax = Percentage.fromInt(25))
)
println("\n\n##### Spread investment of 50,000EUR")
println(prettyPrint(result))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment