forked from Nitecon/iqfeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
181 lines (151 loc) · 6.1 KB
/
writer.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
package iqfeed
import (
"fmt"
"math"
"os"
"strings"
"time"
)
// Write performs a write on the channel data which will be picked up by the writer concurrently and written to iqfeed.
func (c *IQC) Write(data string) {
c.Conn.Write([]byte(data))
}
// WriteBackup does as the name suggests and write the []byte data directly to a file for re-use later.
func (c *IQC) writeBackup(d []byte) {
if !c.CreateBackup {
return
}
if _, err := os.Stat(c.BackupFile); os.IsNotExist(err) {
os.Create(c.BackupFile)
}
f, err := os.OpenFile(c.BackupFile, os.O_APPEND|os.O_RDWR, 0644)
if err != nil {
fmt.Printf("Could not open file for writing: %s\n", err.Error())
return
}
defer f.Close()
_, err = f.Write(d)
if err != nil {
fmt.Printf("Could not write data to file: %s\n", err.Error())
return
}
}
// SetProtocol Changes the current connection's protocol (ex: 5.2).
func (c *IQC) SetProtocol(protocol string) {
c.Write("S,SET PROTOCOL," + protocol + "\r\n")
}
// SetClientName does as the name implies and sets the client message which will also be available in stats.
func (c *IQC) SetClientName(name string) {
c.Write("S,SET CLIENT NAME," + name + "\r\n")
}
// WatchSymbol will issue a command to start watching a symbol, this will return a fundamental and update message with the quotes.
func (c *IQC) WatchSymbol(symbol string) {
c.Write("w" + symbol + "\r\n")
}
// WatchOptionSymbol tracks a new symbol based on contract date (for option chains), contractDate indicates the date for the option contract and isCall indicates whether it is a call / put contract.
func (c *IQC) WatchOptionSymbol(symbol string, value float64, contractDate time.Time, isCall bool) string {
// Final format should be something like: MSFT1220J30.5
ydm := contractDate.Format("0602")
_, rem := math.Modf(value)
pv := fmt.Sprintf("%.0f", value)
if rem > 0 {
pv = fmt.Sprintf("%.1f", value)
}
if isCall {
ydm = ydm + c.getCallChar(contractDate)
} else {
ydm = ydm + c.getPutChar(contractDate)
}
tSym := fmt.Sprintf("%s%s%s", symbol, ydm, pv)
c.WatchSymbol(tSym)
return tSym
}
// LevelIIWatch Begins a trades only watch on a symbol for Level 2 updates.
func (c *IQC) LevelIIWatch(symbol string) {
c.Write("w" + symbol + "\r\n")
}
// TradeOnlyWatch Begins a trades only watch on a symbol for Level 1 updates.
func (c *IQC) TradeOnlyWatch(symbol string) {
c.Write("t" + symbol + "\r\n")
}
// UnwatchSymbol Terminates Level 1 updates for the symbol specified.
func (c *IQC) UnwatchSymbol(symbol string) {
c.Write("r" + symbol + "\r\n")
}
// ForceRefresh Forces a refresh from the server for the symbol specified.
func (c *IQC) ForceRefresh(symbol string) {
c.Write("f" + symbol + "\r\n")
}
// RequestTime Requests a Time Stamp message be sent.
func (c *IQC) RequestTime() {
c.Write("T\r\n")
}
// DisableTSUpdates Disables once per second timestamps
func (c *IQC) DisableTSUpdates() {
c.Write("S,TIMESTAMPSOFF\r\n")
}
// EnableTSUpdates Timestamps default to on, but in the event you have stopped them manually, this will restart them into the stream.
func (c *IQC) EnableTSUpdates() {
c.Write("S,TIMESTAMPSON\r\n")
}
// RegionWatch Begins watching a symbol for Level 1 Regional updates.
func (c *IQC) RegionWatch(symbol string) {
c.Write("S,REGON," + symbol + "\r\n")
}
// RegionWatchOff Stops watching a symbol for Level 1 Regional updates.
func (c *IQC) RegionWatchOff(symbol string) {
c.Write("S,REGOFF," + symbol + "\r\n")
}
// NewsOn Turns on streaming news headlines.
func (c *IQC) NewsOn() {
c.Write("S,NEWSON\r\n")
}
// NewsOff Turns off streaming news headlines.
func (c *IQC) NewsOff() {
c.Write("S,NEWSOFF\r\n")
}
// RequestStats Request a S,STATS message to give you information about the feed status.
func (c *IQC) RequestStats() {
c.Write("S,REQUEST STATS\r\n")
}
// ReqFundamentalFNames Request a list of all available fundamental message field names.
func (c *IQC) ReqFundamentalFNames() {
c.Write("S,REQUEST STATS\r\n")
}
// ReqAllUpdateFNames Request a list of all available summary/update message field names for the currently set IQFeed protocol.
func (c *IQC) ReqAllUpdateFNames() {
c.Write("S,REQUEST ALL UPDATE FIELDNAMES\r\n")
}
// ReqCurrentUpdateFNames Request a list of field names in the current fieldset for this connection.\
// Result: You will receive a S,CURRENT UPDATE FIELDNAMES,[FIELD 1 NAME],[FIELD 2 NAME],...[FIELD N NAME],<LF> message that contains currently selected summary/update fields.
func (c *IQC) ReqCurrentUpdateFNames() {
c.Write("S,REQUEST CURRENT UPDATE FIELDNAMES\r\n")
}
// SelectUpdateFields Change your fieldset for this connection. This fieldset applies to all summary and update messages you receive on this connection. (Comma seperated list of field names).
func (c *IQC) SelectUpdateFields(fields ...string) {
c.Write("S,SELECT UPDATE FIELDS," + strings.Join(fields, ",") + "\r\n")
}
// RequestListedMarkets will request a list of all the listed markets from the feed.
func (c *IQC) RequestListedMarkets() {
c.Write("SLM\r\n")
}
// SetLogLevels Change the logging levels for IQFeed. Level Docs: http://www.iqfeed.net/dev/api/docs/IQConnectLogging.cfm.
func (c *IQC) SetLogLevels(levels ...string) {
c.Write("S,SET LOG LEVELS," + strings.Join(levels, ",") + "\r\n")
}
// RequestWatches Request a list of all symbols currently watched on this connection.
func (c *IQC) RequestWatches() {
c.Write("S,REQUEST WATCHES\r\n")
}
// UnwatchAllSymbols Unwatch all currently watched symbols.
func (c *IQC) UnwatchAllSymbols() {
c.Write("S,UNWATCH ALL\r\n")
}
// Connect Tells IQFeed to initiate a connection to the Level 1 server. This happens automatically upon launching the feed unless the ProductID and/or Product version have not been set. This message is ignored if the feed is already connected.
func (c *IQC) Connect() {
c.Write("S,CONNECT\r\n")
}
// Disconnect Tells IQFeed to disconnect from the Level 1 server. This happens automatically as soon as the last client connection to IQConnect is terminated and the ClientsConnected value in the S,STATS message returns to zero (after having incremented above zero). This message is ignored if the feed is already disconnected.
func (c *IQC) Disconnect() {
c.Write("S,DISCONNECT\r\n")
}