Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Co-Authored-By: Kryptic-prog <[email protected]>
  • Loading branch information
Patrick-Ehimen and Kryptic-prog committed Feb 18, 2025
1 parent 66e77c0 commit 5964703
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cex-backend/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package main

import "fmt"

func main (){
func main() {
fmt.Println("Hello, World")
}
}
65 changes: 56 additions & 9 deletions cex-backend/orderbook/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,30 @@ import (
"time"
)

type Match struct {
Ask *Order
Bid *Order
SizeFilled float64
Price float64
}

type Order struct {
Size float64
Bid bool
Limit *Limit
Size float64
Bid bool
Limit *Limit
Timestamp int64
}

type Limit struct {
Price float64
Orders []*Order
Price float64
Orders []*Order
TotalVolume float64
}

func NewOrder(bid bool, size float64) *Order {
return &Order{
Size: size,
Bid: bid,
Size: size,
Bid: bid,
Timestamp: time.Now().UnixNano(),
}
}
Expand All @@ -32,7 +39,7 @@ func (o *Order) String() string {

func NewLimit(price float64) *Limit {
return &Limit{
Price: price,
Price: price,
Orders: []*Order{},
}
}
Expand All @@ -57,4 +64,44 @@ func (l *Limit) DeleteOrder(o *Order) {
type Orderbook struct {
Asks []*Limit
Bids []*Limit
}

AskLimits map[float64]*Limit
BidLimits map[float64]*Limit
}

func NewOrderbook() *Orderbook {
return &Orderbook{
Asks: []*Limit{},
Bids: []*Limit{},
AskLimits: make(map[float64]*Limit),
BidLimits: make(map[float64]*Limit),
}
}

func (ob *Orderbook) PlaceOrder(price float64, o *Order) []Match {
if o.Size > 0.0 {
ob.add(price, o)
}
return []Match{}
}

func (ob *Orderbook) add(price float64, o *Order) {
var limit *Limit

if o.Bid {
limit = ob.BidLimits[price]
} else {
limit = ob.AskLimits[price]
}

if limit == nil {
limit = NewLimit(price)
if o.Bid {
ob.Asks = append(ob.Asks, limit)
ob.BidLimits[price] = limit
} else {
ob.Asks = append(ob.Bids, limit)
ob.AskLimits[price] = limit
}
}
}
7 changes: 3 additions & 4 deletions cex-backend/orderbook/orderbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"testing"
)

func TestLimit(t *testing.T){
func TestLimit(t *testing.T) {
l := NewLimit(10_000)
buyOrderA := NewOrder(true, 1067)
buyOrderB := NewOrder(true, 3400)
buyOrderC := NewOrder(true, 1007)


l.AddOrder(buyOrderA)
l.AddOrder(buyOrderB)
l.AddOrder(buyOrderC)
Expand All @@ -22,5 +21,5 @@ func TestLimit(t *testing.T){
}

func TestOrderbook(t *testing.T) {

}
ob := NewOrderbook()
}

0 comments on commit 5964703

Please sign in to comment.