Coder Social home page Coder Social logo

taxlots's Introduction

Tax Lot Processor

Information

An important part of a brokerage product is keeping track of tax lots. A tax lot is created when a purchase is made. When a sale is made, the tax lots deducted by the sale are determined by a chosen algorithm. This processor parses a transaction log and outputs the remaining tax lots based on the chosen algorithm (fifo or hifo).

Requirements

  • A modern version of Golang must be installed

Installation

git clone [email protected]:yojoots/taxlots.git
cd taxlots
go install

This will create a taxlots executable binary in your $GOBIN directory (by default $GOPATH/bin or $HOME/go/bin) which can be executed on the command-line with the taxlots command.

Alternatively, you can use go build (instead of go install) to have a taxlots binary generated in your current working directory, and either call it with ./taxlots or move it to a folder that is included in your $PATH environment variable to remove the need for the relative path prefix of ./

Implementation details

  • The script takes one argument and reads a transaction log from stdin in the format of date,buy/sell,price,quantity separated by line breaks
  • Transactions are expected to be provided in chronological order
  • The argument passed into the script determines the tax lot selection algorithm
    • fifo - the first lots bought are the first lots sold
    • hifo - the first lots sold are the lots with the highest price
  • Lots are tracked internally by an incrementing integer id starting at 1
    • Buys on the same date are aggregated into a single lot, the price is the weighted average price, the id remains the same
  • After the transaction log is processed, the remaining lots (in the format of id,date,price,quantity) are printed to stdout
    • price shown with two decimal places
    • quantity shown with eight decimal places
  • If an error is encountered, a descriptive error message is printed to stdout and the script exits with a non-zero exit code
  • Automated tests are included in main_test.go

Testing

Unit tests can be run with go test (or go test -v if you want verbose output)

Example Usage

$ echo -e '2021-01-01,buy,10000.00,1.00000000\n2021-02-01,sell,20000.00,0.50000000' | taxlots fifo
1,2021-01-01,10000.00,0.50000000

$ echo -e '2021-01-01,buy,10000.00,1.00000000\n2021-01-02,buy,20000.00,1.00000000\n2021-02-01,sell,20000.00,1.50000000' | taxlots fifo
2,2021-01-02,20000.00,0.50000000

$ echo -e '2021-01-01,buy,10000.00,1.00000000\n2021-01-02,buy,20000.00,1.00000000\n2021-02-01,sell,20000.00,1.50000000' | taxlots hifo
1,2021-01-01,10000.00,0.50000000

taxlots's People

Contributors

yojoots avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.