forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orders.go
69 lines (61 loc) · 1.34 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
package main
const (
limitOrder = iota
marketOrder
)
// Orders variable holds an array of pointers to order structs
var Orders []*Order
// Order struct holds order values
type Order struct {
OrderID int
Exchange string
Type int
Amount float64
Price float64
}
// NewOrder creates a new order and returns a an orderID
func NewOrder(Exchange string, amount, price float64) int {
order := &Order{}
if len(Orders) == 0 {
order.OrderID = 0
} else {
order.OrderID = len(Orders)
}
order.Exchange = Exchange
order.Amount = amount
order.Price = price
Orders = append(Orders, order)
return order.OrderID
}
// DeleteOrder deletes orders by ID and returns state
func DeleteOrder(orderID int) bool {
for i := range Orders {
if Orders[i].OrderID == orderID {
Orders = append(Orders[:i], Orders[i+1:]...)
return true
}
}
return false
}
// GetOrdersByExchange returns order pointer grouped by exchange
func GetOrdersByExchange(exchange string) []*Order {
orders := []*Order{}
for i := range Orders {
if Orders[i].Exchange == exchange {
orders = append(orders, Orders[i])
}
}
if len(orders) > 0 {
return orders
}
return nil
}
// GetOrderByOrderID returns order pointer by ID
func GetOrderByOrderID(orderID int) *Order {
for i := range Orders {
if Orders[i].OrderID == orderID {
return Orders[i]
}
}
return nil
}