/
manual.go
404 lines (338 loc) · 8.61 KB
/
manual.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package main
// This program allows the user to manually interact with a server - the official TESTEX to start with,
// but the user can choose any server URL, API key, venue and stock. Type 'help' for available commands.
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
"strings"
"github.com/fohristiwhirl/gofighter" // go get -u github.com/fohristiwhirl/gofighter
)
const OFFICIAL_URL = "https://api.stockfighter.io/ob/api"
var order gofighter.ShortOrder
var info gofighter.TradingInfo
var functions map[string]func([]string)
var extrahelp map[string]string
func init() {
functions = make(map[string]func([]string))
functions["help"] = help
functions["execute"] = execute
functions["cancel"] = cancel
functions["status"] = status
functions["statusall"] = statusall
functions["statusstock"] = statusstock
functions["quote"] = quote
functions["orderbook"] = orderbook
functions["heartbeat"] = heartbeat
functions["checkvenue"] = checkvenue
functions["venues"] = venues
functions["stocks"] = stocks
functions["print"] = print
functions["local"] = local
functions["official"] = official
functions["account"] = account
functions["venue"] = venue
functions["symbol"] = symbol
functions["stock"] = symbol
functions["direction"] = direction
functions["buy"] = buy
functions["bid"] = buy
functions["sell"] = sell
functions["ask"] = sell
functions["ordertype"] = ordertype
functions["limit"] = limit
functions["market"] = market
functions["ioc"] = ioc
functions["fok"] = fok
functions["qty"] = qty
functions["price"] = price
functions["key"] = key
functions["url"] = url
functions["load"] = load
functions["reset"] = reset
extrahelp = make(map[string]string)
extrahelp["print"] = " <---- print current settings"
extrahelp["execute"] = " <---- execute order with current settings"
extrahelp["load"] = " <---- load info from a saved GM result from game_start.exe"
var args []string
reset(args)
}
func getline() string {
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
return scanner.Text()
}
func getlist() []string {
line := getline()
return strings.Fields(line)
}
func print_error_or_json(in interface{}, err error) {
if err != nil {
fmt.Println(err)
return
}
gofighter.PrintJSON(in)
}
func print_url() {
fmt.Printf("[URL : %s]\n", info.BaseURL)
}
func print_key() {
fmt.Printf("[KEY : %s]\n", info.ApiKey)
}
// ----------------------------------------------------------------------------------------------
// User-callable functions.
// All must be of this form:
//
// func whatever(args []string) {}
func help(args []string) {
var keys []string
for k := range functions {
keys = append(keys, k)
}
sort.Strings(keys)
fmt.Println("Known commands:")
for _, c := range(keys) {
fmt.Printf(" %s %s\n", c, extrahelp[c])
}
}
func execute(args []string) {
result, err := gofighter.Execute(info, order, nil)
print_error_or_json(result, err)
}
func cancel(args []string) {
if len(args) != 2 {
fmt.Println("Wrong number of args for cancel")
return
}
id, err := strconv.Atoi(args[1])
if err != nil {
fmt.Println(err)
return
}
result, err := gofighter.Cancel(info, id)
print_error_or_json(result, err)
}
func status(args []string) {
if len(args) != 2 {
fmt.Println("Wrong number of args for status")
return
}
id, err := strconv.Atoi(args[1])
if err != nil {
fmt.Println(err)
return
}
result, err := gofighter.GetStatus(info, id)
print_error_or_json(result, err)
}
func statusall(args []string) {
result, err := gofighter.StatusAllOrders(info)
print_error_or_json(result, err)
}
func statusstock(args []string) {
result, err := gofighter.StatusAllOrdersOneStock(info)
print_error_or_json(result, err)
}
func quote(args []string) {
result, err := gofighter.GetQuote(info)
print_error_or_json(result, err)
}
func orderbook(args []string) {
result, err := gofighter.GetOrderbook(info)
print_error_or_json(result, err)
}
func heartbeat(args []string) {
result, err := gofighter.CheckAPI(info)
print_error_or_json(result, err)
}
func checkvenue(args []string) {
result, err := gofighter.CheckVenue(info)
print_error_or_json(result, err)
}
func venues(args []string) {
result, err := gofighter.GetVenueList(info)
print_error_or_json(result, err)
}
func stocks(args []string) {
result, err := gofighter.GetStockList(info)
print_error_or_json(result, err)
}
func print(args []string) {
gofighter.PrintJSON(info)
gofighter.PrintJSON(order)
}
func local(args []string) {
var port int = 8000
var err error
if len(args) == 2 {
port, err = strconv.Atoi(args[1])
if err != nil {
port = 8000
}
}
info.BaseURL = fmt.Sprintf("http://127.0.0.1:%d/ob/api", port)
print_url()
}
func official(args []string) {
info.BaseURL = OFFICIAL_URL
print_url()
}
func account(args []string) {
if len(args) == 2 {
info.Account = string(args[1])
}
fmt.Printf("Account: %s\n", info.Account)
}
func venue(args []string) {
if len(args) == 2 {
info.Venue = string(args[1])
}
fmt.Printf("Venue: %s\n", info.Venue)
}
func symbol(args []string) {
if len(args) == 2 {
info.Symbol = string(args[1])
}
fmt.Printf("Symbol: %s\n", info.Symbol)
}
func direction(args []string) {
if len(args) == 2 {
order.Direction = string(args[1])
}
fmt.Printf("Direction: %s\n", order.Direction)
}
func buy(args []string) {
order.Direction = "buy"
fmt.Printf("Direction: %s\n", order.Direction)
}
func sell(args []string) {
order.Direction = "sell"
fmt.Printf("Direction: %s\n", order.Direction)
}
func ordertype(args []string) {
if len(args) == 2 {
order.OrderType = string(args[1])
}
fmt.Printf("OrderType: %s\n", order.OrderType)
}
func limit(args []string) {
order.OrderType = "limit"
fmt.Printf("OrderType: %s\n", order.OrderType)
}
func market(args []string) {
order.OrderType = "market"
fmt.Printf("OrderType: %s\n", order.OrderType)
}
func ioc(args []string) {
order.OrderType = "immediate-or-cancel"
fmt.Printf("OrderType: %s\n", order.OrderType)
}
func fok(args []string) {
order.OrderType = "fill-or-kill"
fmt.Printf("OrderType: %s\n", order.OrderType)
}
func qty(args []string) {
if len(args) == 2 {
order.Qty, _ = strconv.Atoi(string(args[1]))
}
fmt.Printf("Qty: %d\n", order.Qty)
}
func price(args []string) {
if len(args) == 2 {
order.Price, _ = strconv.Atoi(string(args[1]))
}
fmt.Printf("Price: %d\n", order.Price)
}
func key(args []string) {
if len(args) == 2 {
info.ApiKey = string(args[1])
}
print_key()
}
func url(args []string) {
if len(args) == 2 {
info.BaseURL = string(args[1])
for { // Remove trailing slashes
if len(info.BaseURL) > 0 && info.BaseURL[len(info.BaseURL) - 1] == '/' {
info.BaseURL = info.BaseURL[:len(info.BaseURL) - 1]
} else {
break
}
} // Insert "http://"
if strings.Index(info.BaseURL, "http://") == -1 && strings.Index(info.BaseURL, "https://") == -1 {
fmt.Println("WARNING: No protocol specified, inserting http:// (not https, set that manually if needed)")
info.BaseURL = "http://" + info.BaseURL
}
}
print_url()
}
func load(args []string) {
info = gofighter.GetUserSelection("known_levels.json")
gofighter.PrintJSON(info)
}
func reset(args []string) {
var err error
info.BaseURL = OFFICIAL_URL
info.ApiKey, err = gofighter.LoadAPIKey("api_key.txt")
if err != nil {
fmt.Println(err)
}
info.Account = "EXB123456"
info.Venue = "TESTEX"
info.Symbol = "FOOBAR"
order.Direction = "buy"
order.OrderType = "limit"
order.Qty = 100
order.Price = 5000
gofighter.PrintJSON(info)
gofighter.PrintJSON(order)
}
// ----------------------------------------------------------------------------------------------
func set_price_directly(s string) { // Called if the user types "$43.20" or similar
var dollars int
var cents int
n := s[1:]
dollars_and_cents := strings.Split(string(n), ".")
dollars, _ = strconv.Atoi(dollars_and_cents[0])
if len(dollars_and_cents) > 1 {
cent_str := dollars_and_cents[1]
if len(cent_str) > 2 {
cent_str = cent_str[:2]
}
if len(cent_str) == 1 {
cent_str += "0"
}
cents, _ = strconv.Atoi(cent_str)
}
order.Price = dollars * 100 + cents
fmt.Printf("Price: %d\n", order.Price)
}
func set_qty_directly(s string) { // Called if the user types "200" or similar
order.Qty, _ = strconv.Atoi(s)
fmt.Printf("Qty: %d\n", order.Qty)
}
func main() {
for {
fmt.Printf("> ")
ls := getlist()
if len(ls) == 0 {
continue
}
command := strings.ToLower(ls[0])
fn, ok := functions[command]
if ok {
fn(ls)
continue
}
if command[0] == '$' {
set_price_directly(string(command))
continue
}
if command[0] >= '0' && command[0] <= '9' {
set_qty_directly(string(command))
continue
}
}
}