-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.go
80 lines (64 loc) · 1.35 KB
/
order.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package marketsim
import (
"fmt"
)
// Represents an order to buy or sell
type Order struct {
Symbol StockSymbol
BuySell OrderBuySellVal
OrderType OrderTypeVal
Shares int
Value float64
OrderStatus
}
type OrderBuySellVal int
const (
BuyOrderType OrderBuySellVal = iota + 1
SellOrderType
)
type OrderTypeVal int
const (
MarketOrderType OrderTypeVal = iota + 1
LimitOrderType
)
type OrderStatus int
const (
OrderStatusOpen OrderStatus = iota + 1
OrderStatusFilled
OrderStatusPartial
OrderStatusCancelled
)
func (o OrderTypeVal) String() string {
switch o {
case MarketOrderType:
return "Market"
case LimitOrderType:
return "Limit"
default:
return "Unknown"
}
}
func (o Order) String() string {
var buySellTypeString string
if o.BuySell == BuyOrderType {
buySellTypeString = "Buy"
} else {
buySellTypeString = "Sell"
}
if o.OrderType == MarketOrderType {
return fmt.Sprintf("%v: %v shares of %v at market price.", buySellTypeString, o.Shares, o.Symbol)
} else {
return fmt.Sprintf("%v: %v shares of %v at $%v.", buySellTypeString, o.Shares, o.Symbol, o.Value)
}
}
type SortedOrders []*Order
// Sort interface functions
func (s SortedOrders) Len() int {
return len(s)
}
func (s SortedOrders) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s SortedOrders) Less(i, j int) bool {
return s[i].Value < s[j].Value
}