simmarket

package
v0.0.0-...-ce97658 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2024 License: Apache-2.0 Imports: 3 Imported by: 1

README

Simmarket: Simple market simulation

NOTE: This is heavily influenced by the excellent implementation https://github.com/zond/gomarket

Documentation

Overview

Package simmarket implements a simple market simulation. See: https://download.tuxfamily.org/moneta/documents/LARC-2010-03.pdf Heavily based on: https://github.com/zond/gomarket

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Carrier

type Carrier interface {
	Buy(*Order, *Order, float64)
	Deliver(*Order, *Order, float64)
}

Carrier is the interface for a carrier of resources. NOTE: A carrier is responsible for fulfilling orders. I don't know why this is a separate interface from the trader.

type Market

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

Market represents a single market where resources are traded.

func NewMarket

func NewMarket() *Market

NewMarket returns a new Market struct.

func (*Market) Add

func (m *Market) Add(t Trader)

Add adds the given trader to the market.

func (*Market) Del

func (m *Market) Del(t Trader)

Del removes the given trader from the market.

func (*Market) Price

func (m *Market) Price(r Resource) (price float64, ok bool)

Price returns the price for a given resource if any is known.

func (*Market) Trade

func (m *Market) Trade()

Trade runs the trading simulation for one step.

func (*Market) Value

func (m *Market) Value(resources Resources) float64

Value returns the total value of the given resources.

type Order

type Order struct {
	Carrier  Carrier  // Creator of the offer
	Resource Resource // Resource to trade
	Units    float64  // Number of units to trade
	Price    float64  // Proposed price
}

Order represents a request to trade an amount of a resource for a given price.

func NewOrder

func NewOrder(carrier Carrier, resource Resource, units, price float64) *Order

func (*Order) String

func (o *Order) String() string

String returns a string representation of the order.

type Orders

type Orders []*Order

Orders is a sortable slice of orders.

func (Orders) Len

func (o Orders) Len() int

func (Orders) Less

func (o Orders) Less(i, j int) bool

func (Orders) Swap

func (o Orders) Swap(i, j int)

type Resource

type Resource any

Resource represents a single kind of resource.

type Resources

type Resources map[Resource]float64

Resources is a map of resource type to amount or price.

func (Resources) Clone

func (r Resources) Clone() Resources

Clone makes a copy of the resource set.

func (Resources) Eq

func (r Resources) Eq(o Resources) bool

Eq returns true if the two sets are equal.

func (Resources) MergeIn

func (r Resources) MergeIn(o Resources)

MergeIn merges the given resouce set with the current one.

type StandardTrader

type StandardTrader struct {
	Carrier
	// contains filtered or unexported fields
}

StandardTrader implements the Trader interface.

func NewStandardTrader

func NewStandardTrader(carrier Carrier) *StandardTrader

NewStandardTrader returns a new StandardTrader using a given carrier.

func (*StandardTrader) Ask

func (a *StandardTrader) Ask(units float64, resource Resource, price float64)

Ask creates a new ask for a number of units of a given resource for the given price.

func (*StandardTrader) Asks

func (a *StandardTrader) Asks() []*Order

Asks returns all outstanding asks.

func (*StandardTrader) Bid

func (a *StandardTrader) Bid(units float64, resource Resource, price float64)

func (*StandardTrader) Bids

func (a *StandardTrader) Bids() []*Order

Bids returns all outstanding bids.

type Sums

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

Sums is a helper struct to collect all asks and bids by resources.

type Trader

type Trader interface {
	Asks() []*Order
	Bids() []*Order
}

Trader is the interface for an individual merchant / trader.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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