-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.go
126 lines (106 loc) · 3.25 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package market
import (
"fmt"
"time"
"github.com/vanclief/ez"
"github.com/vanclief/state/interfaces"
)
// ActionType determines if the action is to buy or to sell
type ActionType string
const (
// BuyAction is a purchase action
BuyAction ActionType = "buy"
// SellAction is a selling action
SellAction ActionType = "sell"
)
// OrderType determines the type of market order
type OrderType string
const (
// MarketOrder will buy or sell at whatever the current price is
MarketOrder OrderType = "market"
// ClosePosition will buy or sell at whatever the current price to close a position
ClosePosition OrderType = "close_position"
// LimitOrder will buy or sell at a specific price
LimitOrder OrderType = "limit"
// TakeProfit will buy or sell at a specific price
TakeProfit OrderType = "take_profit"
// StopLoss will buy or sell at a specific price
StopLoss OrderType = "stop_loss"
)
// OrderStatus - determines the status of the order
type OrderStatus string
const (
// FulfilledOrder - The entirety of the order was filled
FulfilledOrder OrderStatus = "fulfilled"
// PartialyFilledOrder - A the order has been filled partialy
PartialyFilledOrder OrderStatus = "partial_fill"
// UnfilledOrder - The order has not been filled
UnfilledOrder OrderStatus = "unfilled"
// CanceledOrder - The order has been cancelled
CanceledOrder OrderStatus = "canceled"
)
// Order - Set of instructions to purchase or sell an asset
type Order struct {
ID string `json:"id"`
Action ActionType `json:"action"`
Type OrderType `json:"type"`
Pair *Pair `json:"pair"`
Price float64 `json:"price"`
Volume float64 `json:"volume"`
ExecutedVolume float64 `json:"executed_volume"`
Fee float64 `json:"fee"`
Cost float64 `json:"cost"`
Status OrderStatus `json:"status"`
OpenTime time.Time `json:"open_time"`
CloseTime time.Time `json:"close_time"`
Trades []string `json:"trades"`
TakeProfit float64 `json:"take_profit"`
StopLoss float64 `json:"stop_loss"`
PositionID string `json:"position_id"`
ExecutionID string `json:"execution_id"`
}
// GetSchema returns the database schema for the Order model
func (o *Order) GetSchema() *interfaces.Schema {
return &interfaces.Schema{Name: "orders", PKey: "id"}
}
// GetID returns the ID from the Order model
func (o *Order) GetID() string {
return o.ID
}
// Update sets the value of the Order instance
func (o *Order) Update(i interface{}) error {
const op = "Order.Update"
order, ok := i.(*Order)
if !ok {
return ez.New(op, ez.EINVALID, "Provided interface is not of type Order", nil)
}
*o = *order
return nil
}
func (o *Order) String() string {
var pairStr string
if o.Pair != nil {
pairStr = o.Pair.String()
}
return fmt.Sprintf(
"ID: %s | Action: %s | Type: %s | Pair: %s | Price: %.4f | Volume: %.4f | ExecVolume: %.4f | Fee: $%.4f | Cost: $%.4f | Status: %s | OT: %s | CT: %s\n",
o.ID,
o.Action,
o.Type,
pairStr,
o.Price,
o.Volume,
o.ExecutedVolume,
o.Fee,
o.Cost,
o.Status,
o.OpenTime,
o.CloseTime,
)
}
func (o *Order) IsOpen() bool {
if o.Status == FulfilledOrder || o.Status == CanceledOrder {
return false
}
return true
}