-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.go
60 lines (50 loc) · 1.1 KB
/
book.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
package malgova
// AllocateCash book
func (b *Book) AllocateCash(Money float64) {
b.CashAllocated = Money
b.Cash = b.CashAllocated
}
// PlaceMarketOrder book
func (b *Book) placeMarketOrder(Qty int) {
b.PendingOrderQuantity = Qty
b.IsMarketOrder = true
}
// PlaceMarketOrder book
func (b *Book) placeLimitOrder(Qty int, Price float64) {
b.PendingOrderQuantity = Qty
b.IsMarketOrder = false
b.PendingOrderPrice = Price
}
// QuantityAffordable book
func (b *Book) QuantityAffordable(Price float64) int {
if Price <= b.Cash {
return int(b.Cash / Price)
}
return 0
}
// Buy Order
func (b *Book) Buy(Qty int) {
b.placeMarketOrder(Qty)
}
// Sell Order
func (b *Book) Sell(Qty int) {
b.placeMarketOrder(-Qty)
}
// InPosition check
func (b *Book) InPosition() bool {
return (b.Position != 0)
}
// IsOrderWaiting check
func (b *Book) IsOrderWaiting() bool {
return (b.PendingOrderQuantity != 0)
}
// IsBookClean check
func (b *Book) IsBookClean() bool {
return (!b.InPosition() && !b.IsOrderWaiting())
}
// Exit all position
func (b *Book) Exit() {
if b.Position != 0 {
b.placeMarketOrder(-b.Position)
}
}