-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
385 lines (339 loc) · 10.2 KB
/
main.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
package main
import (
"bufio"
"fmt"
"math"
"os"
"strconv"
"strings"
)
var STANDARD_DEVIATION_MULTIPLIER = 2
var SMA_PERIOD = 100
type botStatistics struct {
SMA []float64
FMA []float64
EMA []float64
UPPER_BB []float64
LOWER_BB []float64
actionOrder string
}
type botState struct {
timeBank int
maxTimeBank int
timePerMove int
candleInterval int
candleFormat []string
candlesTotal int
candlesGiven int
initialStack int
transactionFee float64
date int
buyingPrice int
stacks map[string]float64
charts map[string]Charts
stats botStatistics
}
type Candle struct {
pair string
date int
high float64
low float64
open float64
close float64
volume float64
}
type Charts struct {
pair []string
date []int
high []float64
low []float64
open []float64
close []float64
volume []float64
}
func main() {
botState := botState{
timeBank: 0,
maxTimeBank: 0,
timePerMove: 0,
candleInterval: 0,
candleFormat: []string{},
candlesTotal: 0,
candlesGiven: 0,
initialStack: 0,
transactionFee: 0,
date: 0,
buyingPrice: 0,
charts: make(map[string]Charts),
stacks: make(map[string]float64),
}
runBot(&botState)
}
func runBot(botState *botState) {
stdin := bufio.NewReader(os.Stdin)
for {
text, _ := stdin.ReadString('\n')
var string_cleared string = text
string_cleared = strings.Replace(string_cleared, "\n", "", -1)
parseInput(string_cleared, botState)
}
}
func parseInput(text string, botState *botState) {
stringSlice := strings.Split(text, " ")
if strings.Compare(stringSlice[0], "settings") == 0 {
update_settings(stringSlice[1], stringSlice[2], botState)
}
if strings.Compare(stringSlice[0], "update") == 0 {
if stringSlice[1] == "game" {
update_game(stringSlice[2], stringSlice[3], botState)
}
}
if stringSlice[0] == "action" {
handle_action(botState)
}
}
func handle_action(botState *botState) {
dollars := botState.stacks["USDT"]
btc := botState.stacks["BTC"]
get_moving_average(botState, botState.charts["USDT_BTC"].close, botState.charts["USDT_BTC"].high, botState.charts["USDT_BTC"].low)
compute_bollinger_bands(botState, botState.charts["USDT_BTC"].close)
handle_signals(botState, botState.charts["USDT_BTC"].close)
current_closing_price := botState.charts["USDT_BTC"].close[len(botState.charts["USDT_BTC"].close)-1]
affordable := (dollars / current_closing_price)
fmt.Println(dollars)
fmt.Println(current_closing_price)
fmt.Println(dollars / current_closing_price)
fmt.Println(affordable)
fmt.Printf("My stacks are USDT: %v and BTC: %v. The current closing price is %v . So I can afford %v\n", dollars, btc, current_closing_price, affordable)
if botState.stats.actionOrder == "SELL" && btc > 0 {
fmt.Printf("sell USDT_BTC %v\n", btc)
fmt.Fprintf(os.Stderr, "sell USDT_BTC %v at %v", btc, current_closing_price)
botState.stats.actionOrder = ""
} else if botState.stats.actionOrder == "BUY" && dollars > 0 {
fmt.Printf("buy USDT_BTC %v\n", (dollars / current_closing_price))
fmt.Fprintf(os.Stderr, "buy USDT_BTC %v", dollars/current_closing_price)
botState.stats.actionOrder = ""
} else {
fmt.Printf("no_moves\n")
}
}
func get_moving_average(botState *botState, close []float64, highs []float64, lows []float64) {
get_slow_moving_average(botState, close, highs, lows)
}
func handle_signals(botState *botState, close []float64) {
// if closes[-2] < self.UPPER_BB[len(self.UPPER_BB)-2] and closes[-1] > self.UPPER_BB[len(self.UPPER_BB)-1]:
// self.actionOrder = "SELL"
if len(botState.stats.UPPER_BB) > 1 {
if close[len(close)-2] < botState.stats.UPPER_BB[len(botState.stats.UPPER_BB)-2] && close[len(close)-1] > botState.stats.UPPER_BB[len(botState.stats.UPPER_BB)-1] {
botState.stats.actionOrder = "SELL"
}
}
if len(botState.stats.LOWER_BB) > 1 {
if close[len(close)-2] > botState.stats.LOWER_BB[len(botState.stats.LOWER_BB)-2] && close[len(close)-1] < botState.stats.LOWER_BB[len(botState.stats.LOWER_BB)-1] {
botState.stats.actionOrder = "BUY"
}
}
}
func compute_bollinger_bands(botState *botState, close []float64) {
get_lower_band(botState, close)
get_upper_band(botState, close)
}
func get_lower_band(botState *botState, close []float64) {
STOCK_SMA := botState.stats.SMA[len(botState.stats.SMA)-1]
var deviationArray []float64
i := len(close) - 1
j := 0
for j != SMA_PERIOD+1 {
deviationArray = append(deviationArray, close[i])
i = i - 1
j = j + 1
}
SMA_STANDARD_DEVIATION := StdDev(deviationArray)
temp_LOWER_BB := STOCK_SMA - float64(STANDARD_DEVIATION_MULTIPLIER)*SMA_STANDARD_DEVIATION
botState.stats.LOWER_BB = append(botState.stats.LOWER_BB, temp_LOWER_BB)
}
func get_upper_band(botState *botState, close []float64) {
STOCK_SMA := botState.stats.SMA[len(botState.stats.SMA)-1]
var deviationArray []float64
i := len(close) - 1
j := 0
for j != SMA_PERIOD+1 {
deviationArray = append(deviationArray, close[i])
i = i - 1
j = j + 1
}
SMA_STANDARD_DEVIATION := StdDev(deviationArray)
temp_UPPER_BB := STOCK_SMA + float64(STANDARD_DEVIATION_MULTIPLIER)*SMA_STANDARD_DEVIATION
botState.stats.UPPER_BB = append(botState.stats.UPPER_BB, temp_UPPER_BB)
}
func get_slow_moving_average(botState *botState, close []float64, highs []float64, lows []float64) {
i := len(close) - 1
temp_SMA := 0.0
TP := 0.0
j := 0
// Average closing value over the last SMA period
for j != SMA_PERIOD {
TP = (highs[i] + lows[i] + close[i]) / 3
temp_SMA = temp_SMA + TP
i = i - 1
j = j + 1
}
botState.stats.SMA = append(botState.stats.SMA, temp_SMA/float64(SMA_PERIOD))
}
func update_settings(key string, value string, botState *botState) {
if strings.Compare(key, "timebank") == 0 {
maxTimeBank, err := strconv.Atoi(value)
handle_errors(err)
timeBank, err := strconv.Atoi(value)
handle_errors(err)
botState.maxTimeBank = maxTimeBank
botState.timeBank = timeBank
}
if strings.Compare(key, "time_per_move") == 0 {
timePerMove, err := strconv.Atoi(value)
handle_errors(err)
botState.timePerMove = timePerMove
}
if strings.Compare(key, "candle_interval") == 0 {
candleInterval, err := strconv.Atoi(value)
handle_errors(err)
botState.candleInterval = candleInterval
}
if strings.Compare(key, "candle_format") == 0 {
candleFormat := strings.Split(value, ",")
botState.candleFormat = candleFormat
}
if strings.Compare(key, "candles_total") == 0 {
candles_total, err := strconv.Atoi(value)
handle_errors(err)
botState.candlesTotal = candles_total
}
if strings.Compare(key, "candles_given") == 0 {
candlesGiven, err := strconv.Atoi(value)
handle_errors(err)
botState.candlesGiven = candlesGiven
}
if strings.Compare(key, "initial_stack") == 0 {
initialStack, err := strconv.Atoi(value)
handle_errors(err)
botState.initialStack = initialStack
}
if strings.Compare(key, "transaction_fee_percent") == 0 {
transactionFee, err := strconv.ParseFloat(value, 64)
handle_errors(err)
botState.transactionFee = transactionFee
}
}
func update_game(key string, value string, botState *botState) {
if strings.Compare(key, "next_candles") == 0 {
new_candles := strings.Split(value, ";")
tmp_date := strings.Split(value, ",")
date, err := strconv.Atoi(tmp_date[1])
handle_errors(err)
botState.date = date
for _, candle_str := range new_candles {
candle_infos := strings.Split(candle_str, ",")
update_charts(candle_infos[0], candle_str, botState)
}
}
if strings.Compare(key, "stacks") == 0 {
tmp_date := strings.Split(value, ",")
for _, candle_str := range tmp_date {
candle_infos := strings.Split(candle_str, ":")
update_stacks(candle_infos[0], candle_infos[1], botState)
}
}
}
func update_stacks(key string, value string, botState *botState) {
valFloat, err := strconv.ParseFloat(value, 64)
handle_errors(err)
botState.stacks[key] = valFloat
}
func update_charts(pair string, new_candle_str string, botState *botState) {
if len(botState.charts) == 0 {
botState.charts[pair] = Charts{}
}
new_candle_obj := initCandle(botState.candleFormat, new_candle_str)
addCandle(new_candle_obj, botState, pair)
}
func addCandle(candle Candle, botState *botState, pair string) {
if entry, ok := botState.charts[pair]; ok {
// Then we modify the copy
entry.date = append(entry.date, candle.date)
entry.open = append(entry.open, candle.open)
entry.high = append(entry.high, candle.high)
entry.low = append(entry.low, candle.low)
entry.close = append(entry.close, candle.close)
entry.volume = append(entry.volume, candle.volume)
// Then we reassign map entry
botState.charts[pair] = entry
}
}
func initCandle(format []string, intel string) Candle {
newCandle := Candle{}
tmp := strings.Split(intel, ",")
for i, key := range format {
value := tmp[i]
if key == "pair" {
newCandle.pair = value
}
if strings.Compare(key, "date") == 0 {
date, err := strconv.Atoi(value)
handle_errors(err)
newCandle.date = date
}
if key == "high" {
high, err := strconv.ParseFloat(value, 64)
handle_errors(err)
newCandle.high = high
}
if key == "low" {
low, err := strconv.ParseFloat(value, 64)
handle_errors(err)
newCandle.low = low
}
if key == "open" {
open, err := strconv.ParseFloat(value, 64)
handle_errors(err)
newCandle.open = open
}
if key == "close" {
close, err := strconv.ParseFloat(value, 64)
handle_errors(err)
newCandle.close = close
}
if key == "volume" {
volume, err := strconv.ParseFloat(value, 64)
handle_errors(err)
newCandle.volume = volume
}
}
return newCandle
}
func handle_errors(err error) {
if err != nil {
fmt.Println(err)
os.Exit(2)
}
}
func Variance(xs []float64) float64 {
if len(xs) == 0 {
return math.NaN()
} else if len(xs) <= 1 {
return 0
}
// Based on Wikipedia's presentation of Welford 1962
// (http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm).
// This is more numerically stable than the standard two-pass
// formula and not prone to massive cancellation.
mean, M2 := 0.0, 0.0
for n, x := range xs {
delta := x - mean
mean += delta / float64(n+1)
M2 += delta * (x - mean)
}
return M2 / float64(len(xs)-1)
}
func StdDev(xs []float64) float64 {
return math.Sqrt(Variance(xs))
}