-
Notifications
You must be signed in to change notification settings - Fork 1
/
savedOrder.go
124 lines (109 loc) · 3.14 KB
/
savedOrder.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
"github.com/z-Wind/gotd"
"github.com/z-Wind/stock/stocker"
)
func savedOrder(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodGet:
getSavedOrders(w, req)
case http.MethodPost:
createSavedOrder(w, req)
case http.MethodDelete:
deleteSavedOrder(w, req)
}
}
func deleteSavedOrder(w http.ResponseWriter, req *http.Request) {
savedOrderIDQ := req.URL.Query().Get("savedOrderID")
if savedOrderIDQ == "" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
savedOrderID, err := strconv.ParseInt(savedOrderIDQ, 10, 64)
if err != nil {
http.Error(w, fmt.Sprintf("savedOrderID %s is not Int", savedOrderIDQ), http.StatusBadRequest)
return
}
td, ok := stockers["TDAmeritrade"]
if !ok {
http.Error(w, fmt.Sprintf("TDAmeritrade is not supported"), http.StatusBadRequest)
return
}
service := td.(*stocker.TDAmeritrade).Service
if _, err := service.SavedOrders.DeleteSavedOrder(accountID, savedOrderID).Do(); err != nil {
http.Error(w, errors.Wrapf(err, "td.SavedOrders.DeleteSavedOrder").Error(), http.StatusBadRequest)
return
}
}
func getSavedOrders(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
td, ok := stockers["TDAmeritrade"]
if !ok {
http.Error(w, fmt.Sprintf("TDAmeritrade is not supported"), http.StatusBadRequest)
return
}
service := td.(*stocker.TDAmeritrade).Service
orders, err := service.SavedOrders.GetSavedOrdersByPath(accountID).Do()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
encoder := json.NewEncoder(w)
err = encoder.Encode(orders)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
func createSavedOrder(w http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(req.Body)
var data struct {
Symbol string `json:"Symbol"`
AssetType string `json:"AssetType"`
Instruction string `json:"Instruction"`
Price float64 `json:"Price"`
Quantity float64 `json:"Quantity"`
}
err := decoder.Decode(&data)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
savedOrder := &gotd.SavedOrder{
Order: &gotd.Order{
Session: "NORMAL",
Duration: "GOOD_TILL_CANCEL",
OrderType: "LIMIT",
CancelTime: time.Now().AddDate(0, 4, 0).UTC().Format("2006-01-02"),
Price: data.Price,
OrderLegCollections: []*gotd.OrderLegCollection{
{
Instrument: &gotd.Instrument{
Symbol: strings.ToUpper(data.Symbol),
AssetType: data.AssetType,
},
Instruction: data.Instruction,
Quantity: data.Quantity,
},
},
},
}
td, ok := stockers["TDAmeritrade"]
if !ok {
http.Error(w, fmt.Sprintf("TDAmeritrade is not supported"), http.StatusBadRequest)
return
}
service := td.(*stocker.TDAmeritrade).Service
_, err = service.SavedOrders.CreateSavedOrder(accountID, savedOrder).Do()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}