fpd

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2014 License: MIT Imports: 5 Imported by: 3

README

*fpd.Decimal Build Status

Package implements fixed-point decimal

Usage

package main

import "github.com/oguzbilgic/fpd"

func main() {
	// Buy price of the security: $136.02
	buyPrice := fpd.New(13602000, -5)

	// Sell price of the security: $137.699
	sellPrice := fpd.New(13769900, -5)

	// Volume traded: 0.01
	volume := fpd.New(1000000, -8)

	// Trade fee percentage: 0.6%
	feePer := fpd.New(6, -3)

	buyCost := buyPrice.Mul(volume)
	buyFee := buyPrice.Mul(volume).Mul(feePer)
	sellRevenue := sellPrice.Mul(volume)
	sellFee := sellPrice.Mul(volume).Mul(feePer)

	// Initall account balance: $2.00000
	balance := fpd.New(200000, -5)

	balance = balance.Sub(buyCost)
	balance = balance.Sub(buyFee)
	balance = balance.Add(sellRevenue)
	balance = balance.Sub(sellFee)

	// Final balance
	fmt.Println(balance)
	// Did this trade turn into profit? :)
}

Documentation

http://godoc.org/github.com/oguzbilgic/fpd

License

The MIT License (MIT)

Documentation

Overview

Package implements a fixed-point decimal

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decimal

type Decimal struct {
	// contains filtered or unexported fields
}

Decimal represents a fixed-point decimal.

func New

func New(value int64, scale int) *Decimal

New returns a new fixed-point decimal

func NewFromFloat added in v1.0.2

func NewFromFloat(value float64, scale int) *Decimal

func NewFromString

func NewFromString(value string, scale int) (*Decimal, error)

NewFromString returns a new fixed-point decimal based on the given string

func (*Decimal) Abs added in v1.0.1

func (d *Decimal) Abs() *Decimal

func (*Decimal) Add

func (d *Decimal) Add(d2 *Decimal) *Decimal

Add adds d to d2 and return d3

func (*Decimal) Cmp

func (d *Decimal) Cmp(d2 *Decimal) int

Cmp compares x and y and returns -1, 0 or 1

Example

-1 if x < y 0 if x == y +1 if x > y

func (*Decimal) Div added in v1.0.0

func (d *Decimal) Div(d2 *Decimal) *Decimal

Mul divides d by d2 and returns d3

func (*Decimal) FormattedString

func (d *Decimal) FormattedString() string

String returns the string representatino of the decimal with the fixed point

Example:

d := New(-12345, -3)
println(d.String())

Output:

-12.345

func (*Decimal) Mul

func (d *Decimal) Mul(d2 *Decimal) *Decimal

Mul multiplies d with d2 and returns d3

func (*Decimal) Scale

func (d *Decimal) Scale() int

func (*Decimal) String

func (d *Decimal) String() string

String returns the string representatino of the decimal

Example:

d := New(-12345, -3)
println(d.String())

Output:

-12345

func (*Decimal) StringScaled

func (d *Decimal) StringScaled(scale int) string

StringScaled first scales the decimal then calls .String() on it.

func (*Decimal) Sub

func (d *Decimal) Sub(d2 *Decimal) *Decimal

Sub subtracts d2 from d and returns d3

type MovingAverage added in v1.1.0

type MovingAverage struct {
	// contains filtered or unexported fields
}

func NewMovingAverage added in v1.1.0

func NewMovingAverage(capacity int, scale int) *MovingAverage

func (*MovingAverage) Append added in v1.1.0

func (ma *MovingAverage) Append(sample *Decimal)

func (*MovingAverage) Calculate added in v1.1.0

func (ma *MovingAverage) Calculate() *Decimal

func (*MovingAverage) Capacity added in v1.1.0

func (ma *MovingAverage) Capacity() int

func (*MovingAverage) Size added in v1.1.0

func (ma *MovingAverage) Size() int

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL