-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
107 lines (100 loc) · 3.23 KB
/
utils.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
package goibcp
import (
"errors"
"fmt"
"time"
"github.com/go-resty/resty/v2"
)
func logMsg(level int, fn string, msg string, err ...error) {
if level <= Settings.LogLevel {
switch level {
case 0:
fmt.Println("GOIBCP ERROR in function", fn, ":", msg, err)
case 1:
fmt.Println("GOBICP ERROR in function", fn, ":", msg, err)
case 2:
fmt.Println("GOIBCP", fn, ":", msg)
case 3:
fmt.Println("GOIBCP Debug:", fn, msg)
}
}
}
//GetEndpoint - Get an endpoint from IBCP and return a map
func (c *IBClient) GetEndpoint(endp string, res interface{}, qs ...string) error {
epURL := Settings.CPURL + endpoints[endp]
var req *resty.Request
if len(qs) > 0 {
req = rClient.R().SetResult(res).SetQueryString(qs[0])
} else {
req = rClient.R().SetResult(res).SetHeader("Accept", "application/json")
}
resp, err := req.Get(epURL)
if err != nil {
logMsg(ERROR, endp, "Failed to get", err)
return err
}
if resp.StatusCode() != 200 {
err = errors.New("HTTP " + fmt.Sprintf("%d", resp.StatusCode()) + "error")
logMsg(ERROR, endp, "StatusCode ", err)
return err
}
logMsg(DEBUG, endp, resp.String())
return nil
}
//PostEndpoint - Post to an endpoint from IBCP
func (c *IBClient) PostEndpoint(endp string, res interface{}) error {
epURL := Settings.CPURL + endpoints[endp]
resp, err := rClient.R().SetResult(res).SetHeader("Content-Type", "application/json").Post(epURL)
if err != nil {
logMsg(ERROR, endp, "Failed to post", err)
return err
}
logMsg(DEBUG, endp, resp.String())
return nil
}
//KeepAlive - Keeps the sesssion alive by tickeling the server every minute unless an error is encountered or session expires
//reauthorize session on nobridge error
func KeepAlive(c *IBClient) error {
KAStatus = true
var treply = IBTickle{}
var sessionInfo = IBSession{}
var err error
for {
time.Sleep(55 * time.Second)
err = c.GetSessionInfo(&sessionInfo)
//exit if http response is not 200 from sessionInfo endpoint.
//TODO: send disconnected event to a channel
if err != nil {
logMsg(ERROR, "KeepAlive", "No Active Session found")
KAStatus = false
return err
}
// fmt.Printf("%+v\n", sessionInfo)
time.Sleep(5 * time.Second)
err = c.GetEndpoint("sessionTickle", &treply)
// var l = IBAccountLedger{}
// c.GetAccountLedger(&l)
logMsg(INFO, "KeepAlive", fmt.Sprintf("%+v", treply))
if err != nil {
logMsg(ERROR, "KeepAlive", "Error on tickle, Reauthenticating as session is still valid")
c.Reauthenticate()
}
if treply.Iserver.Error != "" {
//try to reconnect if "no bridge error is recieved"
if treply.Iserver.Error == "no bridge" {
logMsg(ERROR, "KeepAlive", "No Bridge error on Tickle..trying to rauthenticate")
c.Reauthenticate()
}
//return errors.New(treply.Iserver.Error)
}
// if treply.Iserver.AuthStatus.Connected == false || treply.Iserver.AuthStatus.Authenticated == false {
// return errors.New("IB Session disconnected")
// }
c.UserID = sessionInfo.UserID
c.IsAuthenticated = treply.Iserver.AuthStatus.Authenticated
c.IsConnected = treply.Iserver.AuthStatus.Connected
c.IsCompeting = treply.Iserver.AuthStatus.Competing
c.Message = treply.Iserver.AuthStatus.Message
}
}
//TODO: create Helper methods to place simple market and limit orders for stocks and futures