GithubHelp home page GithubHelp logo

rebalancer's Introduction

rebalancer

Build Status GoDoc Go Report Card Codecov

Rebalancer provides tools to help you with rebalancing your assets.

Examples

Rebalancing

Let's assume the current price of 1 BTC is $5000 and the current price of 1 ETH is $200.

First set the global pricelist to reflect these prices:

err := SetPricelist(Pricelist{
	"ETH": decimal.NewFromFloat(200),
	"BTC": decimal.NewFromFloat(5000),
})

if err != nil {
	log.Fatalf("unexpected error whilst setting pricelist: %v", err)
}

If your assets are 20 ETH and 0.5 BTC, you can model your account like this:

account, err := NewAccount(Portfolio{
	"ETH": decimal.NewFromFloat(20),
	"BTC": decimal.NewFromFloat(0.5),
})

if err != nil {
	log.Fatalf("unexpected error whilst creating account: %v", err)
}

The current value of all your assets is:

0.5 x 5000 + 20 x 200 = 6500

The current percentage of each asset is:

ETH = 20 * 200 / 6500 = 0.615384...
BTC = 0.5 * 5000 / 6500 = 0.384615...

If you wanted to change this to a 50/50 split, we need to model a target index:

targetIndex := Index{
	"ETH": decimal.NewFromFloat(0.5),
	"BTC": decimal.NewFromFloat(0.5),
})

You can then pass targetIndex to your account.Rebalance() and you'll receive
the trades necessary to rebalance your portfolio as a map[Asset]Trade.

requiredTrades, err := account.Rebalance(targetIndex)

if err != nil {
	log.Fatalf("unexpected error whilst rebalancing account: %v", err)
}

for asset, trade := range requiredTrades {
	fmt.Printf("%s %s %s\n", trade.Action, trade.Amount, asset)
}

// Unordered output:
// sell 3.75 ETH
// buy 0.15 BTC

Rebalancing into new assets

You can also rebalance your current portfolio into other new assets, as long as these new assets are included in the global pricelist:

err := SetPricelist(Pricelist{
	"ETH":  decimal.NewFromFloat(200),
	"BTC":  decimal.NewFromFloat(2000),
	"IOTA": decimal.NewFromFloat(0.3),
	"BAT":  decimal.NewFromFloat(0.12),
	"XLM":  decimal.NewFromFloat(0.2),
})

if err != nil {
	log.Fatalf("unexpected error whilst setting pricelist: %v", err)
}

account, err := NewAccount(Portfolio{
	"ETH": decimal.NewFromFloat(42),
})

if err != nil {
	log.Fatalf("unexpected error whilst creating account: %v", err)
}

targetIndex := Index{
	"ETH":  decimal.NewFromFloat(0.2),
	"BTC":  decimal.NewFromFloat(0.2),
	"IOTA": decimal.NewFromFloat(0.2),
	"BAT":  decimal.NewFromFloat(0.2),
	"XLM":  decimal.NewFromFloat(0.2),
}

requiredTrades, err := account.Rebalance(targetIndex)

if err != nil {
	log.Fatalf("unexpected error whilst rebalancing account: %v", err)
}

for asset, trade := range requiredTrades {
	fmt.Printf("%s %s %s\n", trade.Action, trade.Amount, asset)
}

// Unordered output:
// sell 33.6 ETH
// buy 0.84 BTC
// buy 5600 IOTA
// buy 14000 BAT
// buy 8400 XLM

rebalancer's People

Contributors

pdbrito avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

rebalancer's Issues

Balance doesn't work when index assets are missing from holdings

Doing so causes a division by zero error.

Furthermore it's impossible to calculate the amount of an asset to purchase as prices are currently passed through as part of holdings.

Need to rethink how we pass in price information to enable these sorts of calculations

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.