Skip to content

Commit

Permalink
Merge pull request #15 from rhnvrm/gtt
Browse files Browse the repository at this point in the history
GTT Orders
  • Loading branch information
vividvilla authored Nov 27, 2019
2 parents b65f2ab + f83bec0 commit 5fd2c13
Show file tree
Hide file tree
Showing 27 changed files with 782 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
go.sum
.chglog
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ go:
- 1.9.x
- 1.10.x
- 1.11.x
- release
- tip
- 1.12.x
- 1.13.x

install: go get -t -v .
script: go test -v -cover .
72 changes: 71 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,71 @@
# Changelog
<a name="unreleased"></a>
## [Unreleased]


<a name="v3.1.0"></a>
## [v3.1.0] - 2019-09-09
### Chore
- update examples and changelog
- modify examples
- add go 1.11 to travis config

### Feat
- implement gtt orders api
- add models for gtt

### Fix
- travis config, update go versions
- change type of instrument token in orders response struct to uint32
- access token and api key getting appended multiple times in ticker
- ticker SetRootURL() method didn't set root URL

### Test
- fix test for Cancel Order

### Tests
- update test package, gtt tests

### Pull Requests
- Merge pull request [#11](https://github.com/zerodhatech/gokiteconnect/issues/11) from rhnvrm/master
- Merge pull request [#10](https://github.com/zerodhatech/gokiteconnect/issues/10) from rhnvrm/master


<a name="v3.0.0"></a>
## v3.0.0 - 2018-09-03
### Chore
- add travis ci config

### Connect_test
- Refactor setting up mock responders

### Feat
- convert package to go module
- fix tests and make struct members of instrument public

### Fix
- fix goversion in travis
- add custom go get command in travis
- travis config
- remove models.PlainResponse from user and portfolio calls

### Refactor
- calculate depthitem info
- minor cosmetic change

### Test
- added tests for errors

### Tests
- market

### Pull Requests
- Merge pull request [#7](https://github.com/zerodhatech/gokiteconnect/issues/7) from zerodhatech/gomod
- Merge pull request [#5](https://github.com/zerodhatech/gokiteconnect/issues/5) from rhnvrm/markettest
- Merge pull request [#4](https://github.com/zerodhatech/gokiteconnect/issues/4) from rhnvrm/errors_test
- Merge pull request [#3](https://github.com/zerodhatech/gokiteconnect/issues/3) from rhnvrm/master
- Merge pull request [#1](https://github.com/zerodhatech/gokiteconnect/issues/1) from mr-karan/tests
- Merge pull request [#2](https://github.com/zerodhatech/gokiteconnect/issues/2) from zerodhatech/travis


[Unreleased]: https://github.com/zerodhatech/gokiteconnect/compare/v3.1.0...HEAD
[v3.1.0]: https://github.com/zerodhatech/gokiteconnect/compare/v3.0.0...v3.1.0
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ func main() {

Check [examples folder](https://github.com/zerodhatech/gokiteconnect/tree/master/examples) for more examples.

You can run the following after updating the API Keys in the examples:

```bash
go run examples/connect/basic/connect.go
```

## Run unit tests

```
Expand Down
7 changes: 7 additions & 0 deletions connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ const (
URICancelMFSIP string = "/mf/sips/%s" // "/mf/sips/{sip_id}"
URIGetMFHoldings string = "/mf/holdings"

// GTT endpoints
URIPlaceGTT string = "/gtt/triggers"
URIGetGTTs string = "/gtt/triggers"
URIGetGTT string = "/gtt/triggers/%d"
URIModifyGTT string = "/gtt/triggers/%d"
URIDeleteGTT string = "/gtt/triggers/%d"

URIGetInstruments string = "/instruments"
URIGetMFInstruments string = "/mf/instruments"
URIGetInstrumentsExchange string = "/instruments/%s" // "/instruments/{exchange}"
Expand Down
10 changes: 10 additions & 0 deletions connect_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kiteconnect

import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
Expand All @@ -21,6 +22,8 @@ const (

// Test New Kite Connect instance
func TestNewClient(t *testing.T) {
t.Parallel()

apiKey := "api_key"
client := New(apiKey)

Expand All @@ -31,6 +34,8 @@ func TestNewClient(t *testing.T) {

// Test all client setters
func TestClientSetters(t *testing.T) {
t.Parallel()

apiKey := "kitefront"
client := New(apiKey)

Expand Down Expand Up @@ -121,6 +126,8 @@ var MockResponders = [][]string{
[]string{http.MethodGet, URIGetMFSIPs, "mf_sips.json"},
[]string{http.MethodGet, URIGetMFSIPInfo, "mf_sip_info.json"},
[]string{http.MethodGet, URIGetMFHoldings, "mf_holdings.json"},
[]string{http.MethodGet, fmt.Sprintf(URIGetGTT, 123), "gtt_get_order.json"},
[]string{http.MethodGet, URIGetGTTs, "gtt_get_orders.json"},
[]string{http.MethodGet, URIGetInstruments, "instruments_all.csv"},
[]string{http.MethodGet, URIGetMFInstruments, "mf_instruments.csv"},
[]string{http.MethodGet, uriGetInstrumentsExchangeTest, "instruments_nse.csv"},
Expand All @@ -134,14 +141,17 @@ var MockResponders = [][]string{
[]string{http.MethodPut, URIModifyMFSIP, "mf_sip_info.json"},
[]string{http.MethodPut, URIModifyOrder, "order_response.json"},
[]string{http.MethodPut, URIConvertPosition, "positions.json"},
[]string{http.MethodPut, fmt.Sprintf(URIModifyGTT, 123), "gtt_modify_order.json"},

// POST endpoints
[]string{http.MethodPost, URIPlaceOrder, "order_response.json"},
[]string{http.MethodPost, URIPlaceMFOrder, "order_response.json"},
[]string{http.MethodPost, URIPlaceMFSIP, "mf_order_response.json"},
[]string{http.MethodPost, URIPlaceGTT, "gtt_place_order.json"},

// DELETE endpoints
[]string{http.MethodDelete, URICancelOrder, "order_response.json"},
[]string{http.MethodDelete, fmt.Sprintf(URIDeleteGTT, 123), "gtt_modify_order.json"},
}

// Test only function prefix with this
Expand Down
3 changes: 3 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

func TestGetErrorName(t *testing.T) {
t.Parallel()
tests := []struct {
name string
code int
Expand Down Expand Up @@ -62,6 +63,7 @@ func TestGetErrorName(t *testing.T) {
}

func TestError_Error(t *testing.T) {
t.Parallel()
type fields struct {
ErrorType string
Message string
Expand Down Expand Up @@ -91,6 +93,7 @@ func TestError_Error(t *testing.T) {
}

func TestNewError(t *testing.T) {
t.Parallel()
type args struct {
etype string
}
Expand Down
57 changes: 57 additions & 0 deletions examples/connect/advanced/connect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"context"
"fmt"
"log"
"net/http"

kiteconnect "github.com/zerodhatech/gokiteconnect"
)

const (
apiKey string = "my_api_key"
apiSecret string = "my_api_secret"
)

func main() {
// Create a new Kite connect instance
kc := kiteconnect.New(apiKey)

var (
requestToken string
)

// Login URL from which request token can be obtained
fmt.Println("Open the following url in your browser:\n", kc.GetLoginURL())

// Obtain request token after Kite Connect login flow
// Run a temporary server to listen for callback
srv := &http.Server{Addr: ":8080"}
http.HandleFunc("/api/user/callback/kite/", func(w http.ResponseWriter, r *http.Request) {
requestToken = r.URL.Query()["request_token"][0]
log.Println("request token", requestToken)
go srv.Shutdown(context.TODO())
w.Write([]byte("login successful!"))
return
})
srv.ListenAndServe()

// Get user details and access token
data, err := kc.GenerateSession(requestToken, apiSecret)
if err != nil {
fmt.Printf("Error: %v", err)
return
}

// Set access token
kc.SetAccessToken(data.AccessToken)
log.Println("data.AccessToken", data.AccessToken)

// Get margins
margins, err := kc.GetUserMargins()
if err != nil {
fmt.Printf("Error getting margins: %v", err)
}
fmt.Println("margins: ", margins)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"

"github.com/zerodhatech/gokiteconnect"
kiteconnect "github.com/zerodhatech/gokiteconnect"
)

const (
Expand Down
109 changes: 109 additions & 0 deletions examples/connect/gtt/connect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package main

import (
"context"
"log"
"net/http"

kiteconnect "github.com/zerodhatech/gokiteconnect"
)

const (
apiKey string = "my_api_key"
apiSecret string = "my_api_secret"
)

func main() {
// Create a new Kite connect instance
kc := kiteconnect.New(apiKey)

var (
requestToken string
)

// Login URL from which request token can be obtained
log.Println(kc.GetLoginURL())

// Obtained request token after Kite Connect login flow
srv := &http.Server{Addr: ":8080"}
http.HandleFunc("/api/user/callback/kite/", func(w http.ResponseWriter, r *http.Request) {
requestToken = r.URL.Query()["request_token"][0]
go srv.Shutdown(context.TODO())
w.Write([]byte("login successful!"))
return
})
srv.ListenAndServe()

// Get user details and access token
data, err := kc.GenerateSession(requestToken, apiSecret)
if err != nil {
log.Printf("Error: %v", err)
return
}

// Set access token
kc.SetAccessToken(data.AccessToken)

log.Println("Fetching GTTs...")
orders, err := kc.GetGTTs()
if err != nil {
log.Fatalf("Error getting GTTs: %v", err)
}
log.Printf("gtt: %v", orders)

log.Println("Placing GTT...")
// Place GTT
gttResp, err := kc.PlaceGTT(kiteconnect.GTTParams{
Tradingsymbol: "INFY",
Exchange: "NSE",
LastPrice: 800,
TransactionType: kiteconnect.TransactionTypeBuy,
Trigger: &kiteconnect.GTTSingleLegTrigger{
TriggerParams: kiteconnect.TriggerParams{
TriggerValue: 1,
Quantity: 1,
LimitPrice: 1,
},
},
})
if err != nil {
log.Fatalf("error placing gtt: %v", err)
}

log.Println("placed GTT trigger_id = ", gttResp.TriggerID)

log.Println("Fetching details of placed GTT...")

order, err := kc.GetGTT(gttResp.TriggerID)
if err != nil {
log.Fatalf("Error getting GTTs: %v", err)
}
log.Printf("gtt: %v", order)

log.Println("Modify existing GTT...")

gttModifyResp, err := kc.ModifyGTT(gttResp.TriggerID, kiteconnect.GTTParams{
Tradingsymbol: "INFY",
Exchange: "NSE",
LastPrice: 800,
TransactionType: kiteconnect.TransactionTypeBuy,
Trigger: &kiteconnect.GTTSingleLegTrigger{
TriggerParams: kiteconnect.TriggerParams{
TriggerValue: 2,
Quantity: 2,
LimitPrice: 2,
},
},
})
if err != nil {
log.Fatalf("error placing gtt: %v", err)
}

log.Println("modified GTT trigger_id = ", gttModifyResp.TriggerID)

gttDeleteResp, err := kc.DeleteGTT(gttResp.TriggerID)
if err != nil {
log.Fatalf("Error getting GTTs: %v", err)
}
log.Printf("gtt deleted: %v", gttDeleteResp)
}
4 changes: 2 additions & 2 deletions examples/ticker/ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

kiteconnect "github.com/zerodhatech/gokiteconnect"
"github.com/zerodhatech/gokiteconnect/ticker"
kiteticker "github.com/zerodhatech/gokiteconnect/ticker"
)

const (
Expand Down Expand Up @@ -53,7 +53,7 @@ func onNoReconnect(attempt int) {

// Triggered when order update is received
func onOrderUpdate(order kiteconnect.Order) {
fmt.Printf("Order: ", order.OrderID)
fmt.Printf("Order: %s", order.OrderID)
}

func main() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/zerodhatech/gokiteconnect

require (
github.com/gocarina/gocsv v0.0.0-20180809181117-b8c38cb1ba36
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135
github.com/google/go-querystring v1.0.0
github.com/gorilla/websocket v1.4.0
gopkg.in/jarcoal/httpmock.v1 v1.0.0-20180719183105-8007e27cdb32
)
Loading

0 comments on commit 5fd2c13

Please sign in to comment.