-
Notifications
You must be signed in to change notification settings - Fork 0
/
orders.go
91 lines (68 loc) · 1.55 KB
/
orders.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
81
82
83
84
85
86
87
88
89
90
91
package gomarket
import (
"github.com/garyburd/redigo/redis"
"log"
"strconv"
"strings"
"time"
)
type order interface {
execute() bool
cancel() bool
record() bool
GetSymbol() string
GetPrice() float64
}
type Order struct {
Symbol string
Quantity int64
OrderId int64
Price int64
Time time.Time
}
type SellOrder struct {
Order
}
type BuyOrder struct {
Order
}
func NewSellOrder(s string, q int64, price int64) SellOrder {
t := time.Now()
oid := NextOrderId(s)
return SellOrder{Order{Symbol: s, Quantity: q, OrderId: oid, Time: t.UTC(), Price: price}}
}
func NewBuyOrder(s string, q int64) BuyOrder {
t := time.Now()
oid := NextOrderId(s)
return BuyOrder{Order{Symbol: s, Quantity: q, OrderId: oid, Time: t.UTC()}}
}
func NextOrderId(symbol string) int64 {
r := NewRedisConnection()
id, err := redis.Int64(r.Do("INCR", redisKey(symbol+":uOrderId")))
if err != nil {
log.Fatal(err)
}
return id
}
func (o *Order) GetSymbol() string {
return strings.ToUpper(o.Symbol)
}
func (s *SellOrder) cancel() bool {
return true
}
func (s *SellOrder) execute() bool {
return true
}
func (s *SellOrder) Record() error {
r := NewRedisConnection()
orderIdStr := strconv.FormatInt(s.OrderId, 10)
// Add the order id to the sell orders for this symbol
r.Send("LPUSH", redisKey(s.GetSymbol()+":orders:sell"), orderIdStr)
// Push the order details into a hash
r.Send("HMSET", redis.Args{}.Add(redisKey(s.GetSymbol()+":orders:sell:"+orderIdStr)).AddFlat(s))
r.Flush()
if _, err := r.Receive(); err != nil {
return err
}
return nil
}