marketsim

package module
v0.0.0-...-903ccb3 Latest Latest
Warning

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

Go to latest
Published: May 12, 2016 License: MIT Imports: 8 Imported by: 0

README

Marketsim

Marketsim is a library to aid in simulation of a market. It allows you to create a market, add stocks to your market, submit limit orders to the market, and then keep track of the execution of trades that occur if they can be facilitated by the prices and quantities set in the limit orders. The bid/ask spread for the stock will be updated according to the outstanding orders on the order book.

(This is a work in progress and only supports limit orders at the moment)

Installation

The easiest way to install marketsim is to run go get github.com/dob/marketsim.

Usage

To use marketsim, you first have to instantiate a market and add at least one stock to it.

import ms "github.com/dob/marketsim"

var market *ms.Market = ms.NewMarket()

// Add a stock or two.
// Lets add a symbols called AMZN and TSLA starting with the default price
market.Stocks[ms.StockSymbol("AMZN")] = &ms.Stock{ms.StockSymbol("AMZN"), "Amazon", ms.StartingPrice}
market.Stocks[ms.StockSymbol("TSLA")] = &ms.Stock{ms.StockSymbol("TSLA"), "Tesla", ms.StartingPrice}

Marketsim currently supports limit orders. Lets submit a few 100 share limit orders for AMZN.

buyOrder := &ms.Order{"AMZN", ms.BuyOrderType, ms.LimitOrderType, 100, 645.20, ms.OrderStatusOpen}
sellOrder := &ms.Order{"AMZN", ms.SellOrderType, ms.LimitOrderType, 100, 646.10, ms.OrderStatusOpen}

market.ReceiveOrder(buyOrder)
market.ReceiveOrder(sellOrder)

Now we should have two orders on the OrderBook. You can check the price of the orders with

price := market.GetPriceForSymbol("AMZN")

fmt.Printf("Bid price: %v, Ask price: %v", price.Bid, price.Offer)

You can also print the market object directly which will print a nice tabled output of all prices on the market. fmt.println(market)

If an order comes in that crosses the spread, the market will process the order, update the order book, and prices accordingly.

crossSpreadOrder := ms.Order{"AMZN", ms.SellOrderType,
ms.LimitOrderType, 50, 645.20, ms.OrderStatusOpen}
market.ReceiveOrder(crossSpreadOrder)

// This order will cross the spread to the first submited sell order, and 50 shares will be taken off the OrderBook

Examples

Simple example

The first example reproduces the walkthrough in this readme. It's a simple demonstration of creating a market, adding some stocks, and submitting a couple orders.

cd cmd/marketsim/examples go build ./examples

Simple Example

Nasdaq with 100,000 random orders

The second example is more complex. It instantiates all the stocks on the Nasdaq, and begins generating orders randomly at first, and then within 5% of the current price range. Orders are processed as they enter the book. At the end it prints the entire market.

cd cmd/marketsim/examples go build ./examples -example=full

Full Nasdaq Simulation

To-Do

  1. Support market orders and more sensible defaults.
  2. Better error reporting

Documentation

Index

Constants

View Source
const MaxPrice float64 = float64(1 << 10) // Max price in our market is $1M+/share
View Source
const MinPrice float64 = 0.0

Variables

View Source
var InvalidOrder = errors.New("Invalid order")

Functions

This section is empty.

Types

type Market

type Market struct {
	Stocks map[StockSymbol]*Stock
	Orders map[StockSymbol][]*Order
	// contains filtered or unexported fields
}

Establish the market

func NewMarket

func NewMarket() *Market

func (*Market) GetPriceForSymbol

func (m *Market) GetPriceForSymbol(s StockSymbol) StockPrice

func (*Market) ReceiveOrder

func (m *Market) ReceiveOrder(o *Order) error

func (Market) String

func (m Market) String() string

func (*Market) Symbols

func (m *Market) Symbols() []StockSymbol

The symbols in the market

type Order

type Order struct {
	Symbol    StockSymbol
	BuySell   OrderBuySellVal
	OrderType OrderTypeVal
	Shares    int
	Value     float64
	OrderStatus
}

Represents an order to buy or sell

func (Order) String

func (o Order) String() string

type OrderBuySellVal

type OrderBuySellVal int
const (
	BuyOrderType OrderBuySellVal = iota + 1
	SellOrderType
)

type OrderStatus

type OrderStatus int
const (
	OrderStatusOpen OrderStatus = iota + 1
	OrderStatusFilled
	OrderStatusPartial
	OrderStatusCancelled
)

type OrderTypeVal

type OrderTypeVal int
const (
	MarketOrderType OrderTypeVal = iota + 1
	LimitOrderType
)

func (OrderTypeVal) String

func (o OrderTypeVal) String() string

type SortedOrders

type SortedOrders []*Order

func (SortedOrders) Len

func (s SortedOrders) Len() int

Sort interface functions

func (SortedOrders) Less

func (s SortedOrders) Less(i, j int) bool

func (SortedOrders) Swap

func (s SortedOrders) Swap(i, j int)

type Stock

type Stock struct {
	Symbol StockSymbol
	Name   string
	Price  StockPrice
}

Equity representing one stock

func (Stock) String

func (s Stock) String() string

type StockPrice

type StockPrice struct {
	Bid   float64
	Offer float64
}

Represent the current price of a stock

var StartingPrice StockPrice = StockPrice{MinPrice, MaxPrice}

func (StockPrice) MidPrice

func (sp StockPrice) MidPrice() float64

type StockSymbol

type StockSymbol string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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