Skip to content

Instantly share code, notes, and snippets.

@arturaz
Created April 22, 2019 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arturaz/750707d55444a7eb39ec147c50da9bf9 to your computer and use it in GitHub Desktop.
Save arturaz/750707d55444a7eb39ec147c50da9bf9 to your computer and use it in GitHub Desktop.
Little scala script for calculating the dividends and withholdings for them for InteractiveBrokers platform
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Path, Paths}
case class Identifier(ticker: String, isin: String)
val DescriptionRe = """^(.+?)\s*\((.+?)\).*$""".r
def read(path: Path): Map[Identifier, Vector[BigDecimal]] = {
val lines = Files.readAllLines(path, StandardCharsets.UTF_8)
var map = Map.empty[Identifier, Vector[BigDecimal]].withDefaultValue(Vector.empty)
lines.forEach { line =>
val Vector(date, description, amountStr, _*) = line.split("\t").toVector
val DescriptionRe(ticker, isin) = description
val id = Identifier(ticker, isin)
val amount = BigDecimal(amountStr)
println(s"Read $id -> $amount")
map = map.updated(id, map(id) :+ amount)
}
map
}
val dividends = read(Paths.get("e:\\2016-us-div.txt"))
val withholdings = read(Paths.get("e:\\2016-us-withhold.txt"))
(withholdings -- dividends.keys).foreach { what =>
println(s"WTF withholding: $what")
}
dividends.toVector.sortBy(_._1.ticker).foreach { dividend =>
val withheld = withholdings.getOrElse(dividend._1, Vector.empty).sum
println(s"${dividend._1.ticker}\t${dividend._1.isin}\t${dividend._2.sum}\t$withheld")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment