GithubHelp home page GithubHelp logo

jbertovic / costbasis Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 32 KB

Costbasis library helps calculate realized gains/losses and track remaining unrealized inventory. I use it with my transactions in crypto, tokens, equity and equity options.

License: MIT License

Rust 100.00%
cryptocurrency cryptocoins accounting gains-calculator profit-loss capital-gains

costbasis's Introduction

I created this library to help track costbasis of my trading of equity, options and crypto. I use this to help create a profit loss report. I will continue developing this further to allow me to calculate capital gains for taxes similar to what gainskeeper does.

CostBasis library maintains unrealized transactions in a holding. As inventory change transactions are added, the inventory is updated or removed into a realized gain.

  • Holding - maintains the current inventory and determines change impacts in inventory/unrealized and realized gains/loss
  • URealized - is a transaction record not realized (open position)
  • Realized - is a combination of a transaction open and matching close, captures gain/loss

Transaction - is a record of inventory change. Can be replaced by a user defined struct that implements Inventory and VolumeSplit trait

Assumes FIFO for maintaining holding inventory. Future plans to add Lot, Avg Weight, and LIFO.

Look at examples cryptogains and cryptodetails. This uses a csv of my Crypto transactions which is culmination of wallet, coinbase, and coinbase pro transactions. I took csv reports from each of them and data wrangled it down to transaction inventory changes - removed some transactions so its not as large for an example. All transaction fees in ETH on the ethereum network are indicated as REMOVE in the data. Internal Sends and Receives are not included as they aren't taxable events or inventory changes.

Also, you can look at some of the tests under ./tests/costbasis.rs

A quick example:

use costbasis::holding::Holding;
use costbasis::inventory::InventoryType;
use costbasis::realized::Realized;
use costbasis::transaction::Transaction;
use costbasis::unrealized::URealized;

let transactions = [
    Transaction::from("2020-01-01,long,200.0,25.0"),
    Transaction::from("2020-02-01,short,100.0,35.0"),
];

let mut holding = Holding::new(&transactions[0]);
let gains_realized = holding.add_transaction(&transactions[1]);

// remaining inventory left in holding
let results_urealized = vec![URealized::from("2020-01-01,100.0,-2500.0")];

// in the form of close date, close quantity, proceeds, open date, open quantity, cost, profit
let results_realized = [Realized::from(
    "2020-02-01,-100.0,3500.0,2020-01-01,100.0,-2500.0,1000.0",
)];

assert_eq!(gains_realized, results_realized);
assert_eq!(holding.inventory(), results_urealized);

FUTURE:

  • need to add better documentation
  • add Avg Cost method - would work with mutual funds
  • add LIFO method - alternative to the current FIFO implementation
  • Lots method - not sure if this one is worth implementing
  • Realized better access to members
  • Realized identification of long-term vs short-term gains
  • Realized a way to add adjustments like wash sales and dealing with basis transfer from options relative to the underlying
  • Error checking and implementing some form of error framework

QUESTIONS:

  • Do proceeds always need to be on the left. Thinking when short an option or stock and then buy back. What does the realized gain look like?

costbasis's People

Contributors

jbertovic avatar

Stargazers

 avatar  avatar  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.