-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathorders.go
239 lines (213 loc) · 8.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package smartapigo
import (
"net/http"
)
// Order represents a individual order response.
type Order struct {
Variety string `json:"variety"`
OrderType string `json:"ordertype"`
ProductType string `json:"producttype"`
Duration string `json:"duration"`
Price string `json:"price"`
TriggerPrice string `json:"triggerprice"`
Quantity string `json:"quantity"`
DisclosedQuantity string `json:"disclosedquantity"`
SquareOff string `json:"squareoff"`
StopLoss string `json:"stoploss"`
TrailingStopLoss string `json:"trailingstoploss"`
TrailingSymbol string `json:"trailingsymbol"`
TransactionType string `json:"transactiontype"`
Exchange string `json:"exchange"`
SymbolToken string `json:"symboltoken"`
InstrumentType string `json:"instrumenttype"`
StrikePrice string `json:"strikeprice"`
OptionType string `json:"optiontype"`
ExpiryDate string `json:"expirydate"`
LotSize string `json:"lotsize"`
CancelSize string `json:"cancelsize"`
AveragePrice string `json:"averageprice"`
FilledShares string `json:"filledshares"`
UnfilledShares string `json:"unfilledshares"`
OrderID string `json:"orderid"`
Text string `json:"text"`
Status string `json:"status"`
OrderStatus string `json:"orderstatus"`
UpdateTime string `json:"updatetime"`
ExchangeTime string `json:"exchtime"`
ExchangeOrderUpdateTime string `json:"exchorderupdatetime"`
FillID string `json:"fillid"`
FillTime string `json:"filltime"`
}
// Orders is a list of orders.
type Orders []Order
// OrderParams represents parameters for placing an order.
type OrderParams struct {
Variety string `json:"variety"`
TradingSymbol string `json:"tradingsymbol"`
SymbolToken string `json:"symboltoken"`
TransactionType string `json:"transactiontype"`
Exchange string `json:"exchange"`
OrderType string `json:"ordertype"`
ProductType string `json:"producttype"`
Duration string `json:"duration"`
Price string `json:"price"`
SquareOff string `json:"squareoff"`
StopLoss string `json:"stoploss"`
Quantity string `json:"quantity"`
}
// OrderParams represents parameters for modifying an order.
type ModifyOrderParams struct {
Variety string `json:"variety"`
OrderID string `json:"orderid"`
OrderType string `json:"ordertype"`
ProductType string `json:"producttype"`
Duration string `json:"duration"`
Price string `json:"price"`
Quantity string `json:"quantity"`
TradingSymbol string `json:"tradingsymbol"`
SymbolToken string `json:"symboltoken"`
Exchange string `json:"exchange"`
}
// OrderResponse represents the order place success response.
type OrderResponse struct {
Script string `json:"script"`
OrderID string `json:"orderid"`
}
// Trade represents an individual trade response.
type Trade struct {
Exchange string `json:"exchange"`
ProductType string `json:"producttype"`
TradingSymbol string `json:"tradingsymbol"`
InstrumentType string `json:"instrumenttype"`
SymbolGroup string `json:"symbolgroup"`
StrikePrice string `json:"strikeprice"`
OptionType string `json:"optiontype"`
ExpiryDate string `json:"expirydate"`
MarketLot string `json:"marketlot"`
Precision string `json:"precision"`
Multiplier string `json:"multiplier"`
TradeValue string `json:"tradevalue"`
TransactionType string `json:"transactiontype"`
FillPrice string `json:"fillprice"`
FillSize string `json:"fillsize"`
OrderID string `json:"orderid"`
FillID string `json:"fillid"`
FillTime string `json:"filltime"`
}
// Trades is a list of trades.
type Trades []Trade
// Position represents an individual position response.
type Position struct {
Exchange string `json:"exchange"`
SymbolToken string `json:"symboltoken"`
ProductType string `json:"producttype"`
Tradingsymbol string `json:"tradingsymbol"`
SymbolName string `json:"symbolname"`
InstrumentType string `json:"instrumenttype"`
PriceDen string `json:"priceden"`
PriceNum string `json:"pricenum"`
GenDen string `json:"genden"`
GenNum string `json:"gennum"`
Precision string `json:"precision"`
Multiplier string `json:"multiplier"`
BoardLotSize string `json:"boardlotsize"`
BuyQuantity string `json:"buyquantity"`
SellQuantity string `json:"sellquantity"`
BuyAmount string `json:"buyamount"`
SellAmount string `json:"sellamount"`
SymbolGroup string `json:"symbolgroup"`
StrikePrice string `json:"strikeprice"`
OptionType string `json:"optiontype"`
ExpiryDate string `json:"expirydate"`
LotSize string `json:"lotsize"`
CfBuyQty string `json:"cfbuyqty"`
CfSellQty string `json:"cfsellqty"`
CfBuyAmount string `json:"cfbuyamount"`
CfSellAmount string `json:"cfsellamount"`
BuyAveragePrice string `json:"buyavgprice"`
SellAveragePrice string `json:"sellavgprice"`
AverageNetPrice string `json:"avgnetprice"`
NetValue string `json:"netvalue"`
NetQty string `json:"netqty"`
TotalBuyValue string `json:"totalbuyvalue"`
TotalSellValue string `json:"totalsellvalue"`
CfBuyAveragePrice string `json:"cfbuyavgprice"`
CfSellAveragePrice string `json:"cfsellavgprice"`
TotalBuyAveragePrice string `json:"totalbuyavgprice"`
TotalSellAveragePrice string `json:"totalsellavgprice"`
NetPrice string `json:"netprice"`
}
// Positions represents a list of net and day positions.
type Positions []Position
// ConvertPositionParams represents the input params for a position conversion.
type ConvertPositionParams struct {
Exchange string `url:"exchange"`
TradingSymbol string `url:"tradingsymbol"`
OldProductType string `url:"oldproducttype"`
NewProductType string `url:"newproducttype"`
TransactionType string `url:"transactiontype"`
Quantity int `url:"quantity"`
Type string `json:"type"`
}
// GetOrderBook gets user orders.
func (c *Client) GetOrderBook() (Orders, error) {
var orders Orders
err := c.doEnvelope(http.MethodGet, URIGetOrderBook, nil, nil, &orders, true)
return orders, err
}
// PlaceOrder places an order.
func (c *Client) PlaceOrder(orderParams OrderParams) (OrderResponse, error) {
var (
orderResponse OrderResponse
params map[string]interface{}
err error
)
params = structToMap(orderParams, "json")
err = c.doEnvelope(http.MethodPost, URIPlaceOrder, params, nil, &orderResponse, true)
return orderResponse, err
}
// ModifyOrder for modifying an order.
func (c *Client) ModifyOrder(modifyOrderParams ModifyOrderParams) (OrderResponse, error) {
var (
orderResponse OrderResponse
params map[string]interface{}
err error
)
params = structToMap(modifyOrderParams, "json")
err = c.doEnvelope(http.MethodPost, URIModifyOrder, params, nil, &orderResponse, true)
return orderResponse, err
}
// CancelOrder for cancellation of an order.
func (c *Client) CancelOrder(variety string, orderid string) (OrderResponse, error) {
var (
orderResponse OrderResponse
err error
)
params := make(map[string]interface{})
params["variety"] = variety
params["orderid"] = orderid
err = c.doEnvelope(http.MethodPost, URICancelOrder, params, nil, &orderResponse, true)
return orderResponse, err
}
// GetPositions gets user positions.
func (c *Client) GetPositions() (Positions, error) {
var positions Positions
err := c.doEnvelope(http.MethodGet, URIGetPositions, nil, nil, &positions, true)
return positions, err
}
// GetTradeBook gets user trades.
func (c *Client) GetTradeBook() (Trades, error) {
var trades Trades
err := c.doEnvelope(http.MethodGet, URIGetTradeBook, nil, nil, &trades, true)
return trades, err
}
// ConvertPosition converts position's product type.
func (c *Client) ConvertPosition(convertPositionParams ConvertPositionParams) error {
var (
params map[string]interface{}
err error
)
params = structToMap(convertPositionParams, "json")
err = c.doEnvelope(http.MethodPost, URIConvertPosition, params, nil, nil, true)
return err
}