forked from zerodha/gokiteconnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtt.go
217 lines (186 loc) · 5.79 KB
/
gtt.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package kiteconnect
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/zerodha/gokiteconnect/v4/models"
)
// GTTType represents the available GTT order types.
type GTTType string
const (
// GTTTypeSingle is used to monitor a single trigger value
GTTTypeSingle GTTType = "single"
// GTTTypeOCO is used to monitor two trigger values
// where executing one cancels the other.
GTTTypeOCO GTTType = "two-leg"
)
// GTTs represents a list of GTT orders.
type GTTs []GTT
// GTTMeta contains information about the rejection reason
// received after GTT order was triggered.
type GTTMeta struct {
RejectionReason string `json:"rejection_reason"`
}
// GTTCondition represents the condition inside a GTT order.
type GTTCondition struct {
Exchange string `json:"exchange"`
Tradingsymbol string `json:"tradingsymbol"`
LastPrice float64 `json:"last_price"`
TriggerValues []float64 `json:"trigger_values"`
}
// GTT represents a single GTT order.
type GTT struct {
ID int `json:"id"`
UserID string `json:"user_id"`
Type GTTType `json:"type" url:""`
CreatedAt models.Time `json:"created_at"`
UpdatedAt models.Time `json:"updated_at"`
ExpiresAt models.Time `json:"expires_at"`
Status string `json:"status"`
Condition GTTCondition `json:"condition"`
Orders []Order `json:"orders"`
Meta GTTMeta `json:"meta"`
}
// Trigger is an abstraction over multiple GTT types.
type Trigger interface {
TriggerValues() []float64
LimitPrices() []float64
Quantities() []float64
Type() GTTType
}
type TriggerParams struct {
TriggerValue float64
LimitPrice float64
Quantity float64
}
// GTTSingleLegTrigger implements Trigger interface for the SingleLegTrigger.
type GTTSingleLegTrigger struct {
TriggerParams
}
func (t *GTTSingleLegTrigger) TriggerValues() []float64 {
return []float64{t.TriggerValue}
}
func (t *GTTSingleLegTrigger) LimitPrices() []float64 {
return []float64{t.LimitPrice}
}
func (t *GTTSingleLegTrigger) Quantities() []float64 {
return []float64{t.Quantity}
}
func (t *GTTSingleLegTrigger) Type() GTTType {
return GTTTypeSingle
}
// GTTOneCancelsOtherTrigger implements Trigger interface for the GTTOneCancelsOtherTrigger.
type GTTOneCancelsOtherTrigger struct {
Upper TriggerParams
Lower TriggerParams
}
func (t *GTTOneCancelsOtherTrigger) TriggerValues() []float64 {
return []float64{t.Lower.TriggerValue, t.Upper.TriggerValue}
}
func (t *GTTOneCancelsOtherTrigger) LimitPrices() []float64 {
return []float64{t.Lower.LimitPrice, t.Upper.LimitPrice}
}
func (t *GTTOneCancelsOtherTrigger) Quantities() []float64 {
return []float64{t.Lower.Quantity, t.Upper.Quantity}
}
func (t *GTTOneCancelsOtherTrigger) Type() GTTType {
return GTTTypeOCO
}
// GTTParams is a helper struct used to populate an
// actual GTT before sending it to the API.
type GTTParams struct {
Tradingsymbol string
Exchange string
LastPrice float64
TransactionType string
Trigger Trigger
}
func newGTT(o GTTParams) GTT {
var orders Orders
for i := range o.Trigger.TriggerValues() {
orders = append(orders, Order{
Exchange: o.Exchange,
TradingSymbol: o.Tradingsymbol,
TransactionType: o.TransactionType,
Quantity: o.Trigger.Quantities()[i],
Price: o.Trigger.LimitPrices()[i],
OrderType: OrderTypeLimit,
Product: ProductCNC,
})
}
return GTT{
Type: o.Trigger.Type(),
Condition: GTTCondition{
Exchange: o.Exchange,
LastPrice: o.LastPrice,
Tradingsymbol: o.Tradingsymbol,
TriggerValues: o.Trigger.TriggerValues(),
},
Orders: orders,
}
}
// GTTResponse is returned by the API calls to GTT API.
type GTTResponse struct {
TriggerID int `json:"trigger_id"`
}
// PlaceGTT constructs and places a GTT order using GTTParams.
func (c *Client) PlaceGTT(o GTTParams) (GTTResponse, error) {
var (
params = url.Values{}
gtt = newGTT(o)
orderResp GTTResponse
)
condition, err := json.Marshal(gtt.Condition)
if err != nil {
return orderResp, fmt.Errorf("error while parsing condition: %v", err)
}
orders, err := json.Marshal(gtt.Orders)
if err != nil {
return orderResp, fmt.Errorf("error while parsing orders: %v", err)
}
params.Add("type", string(gtt.Type))
params.Add("condition", string(condition))
params.Add("orders", string(orders))
err = c.doEnvelope(http.MethodPost, URIPlaceGTT, params, nil, &orderResp)
return orderResp, err
}
// ModifyGTT modifies the condition or orders inside an already created GTT order.
func (c *Client) ModifyGTT(triggerID int, o GTTParams) (GTTResponse, error) {
var (
params = url.Values{}
gtt = newGTT(o)
orderResp GTTResponse
)
condition, err := json.Marshal(gtt.Condition)
if err != nil {
return orderResp, fmt.Errorf("error while parsing condition: %v", err)
}
orders, err := json.Marshal(gtt.Orders)
if err != nil {
return orderResp, fmt.Errorf("error while parsing orders: %v", err)
}
params.Add("type", string(gtt.Type))
params.Add("condition", string(condition))
params.Add("orders", string(orders))
err = c.doEnvelope(http.MethodPut, fmt.Sprintf(URIModifyGTT, triggerID), params, nil, &orderResp)
return orderResp, err
}
// GetGTTs returns the current GTTs for the user.
func (c *Client) GetGTTs() (GTTs, error) {
var orders GTTs
err := c.doEnvelope(http.MethodGet, URIGetGTTs, nil, nil, &orders)
return orders, err
}
// GetGTT returns a specific GTT for the user.
func (c *Client) GetGTT(triggerID int) (GTT, error) {
var order GTT
err := c.doEnvelope(http.MethodGet, fmt.Sprintf(URIGetGTT, triggerID), nil, nil, &order)
return order, err
}
// DeleteGTT deletes a GTT order.
func (c *Client) DeleteGTT(triggerID int) (GTTResponse, error) {
var order GTTResponse
err := c.doEnvelope(http.MethodDelete, fmt.Sprintf(URIGetGTT, triggerID), nil, nil, &order)
return order, err
}