Thursday, 5 September 2013

Sorting share trading books with Golang


Golang's sort package provides a set of primitives that allows us to define the order of our own structures. Furthermore we can define various ways of sorting the structures by extending the base collection.

Let say we have we're storing orders in a share trading book.

// order struct
type Order struct {
    Ind      string // buy sell indicator
    Epic     string
    Price    float64
    Quantity float64
    Time     int64
}

// base orders array type
type Orders []Order

Now, so that we can use the sort package we need to define a few simple functions.

// default length func
func (o Orders) Len() int {
    return len(o)
}

// default swap func
func (o Orders) Swap(i, j int) {
    o[i], o[j] = o[j], o[i]
}

So that we can sort Orders in many ways we need to extend the base structure.

// specific buy order stuct
type BuyOrders struct {
    Orders
}

// specific sell order struct
type SellOrders struct {
    Orders
}

Now, we need to specify the less function for each type. Buy orders are sorted price descending, time ascending and sell orders are sorted price ascending, time ascending.

// Define the order of buy orders
func (o BuyOrders) Less(i, j int) bool {

    // we've assumed its the same stock and currency
    // buy orders are reversed

    if o.Orders[j].Price < o.Orders[i].Price {
        return true
    }

    if o.Orders[j].Price > o.Orders[i].Price {
        return false
    }

    // same price, check time priority
    // because of this element we cannot simply call reverse
    return o.Orders[i].Time < o.Orders[j].Time
}

// define the order of sell orders
func (o SellOrders) Less(i, j int) bool {

    if o.Orders[i].Price < o.Orders[j].Price {
        return true
    }

    if o.Orders[i].Price > o.Orders[j].Price {
        return false
    }

    // same price, check time priority
    return o.Orders[i].Time < o.Orders[j].Time
}

To sort an array of Orders in buy sequence we can now simply call

sort.Sort(BuyOrders{orders})

or alternatively in sell sequence

sort.Sort(SellOrders{orders})

The link shows an example of this in action by inserting orders into an order book and printing the level 1 price and level 2 prices in the correct sequence.