/
buy_low_sell_high_bot_no_ws.go
126 lines (104 loc) · 3.28 KB
/
buy_low_sell_high_bot_no_ws.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
package main
// This is about the dumbest thing that can conceivably make money. It's used
// in some of our unofficial little PvP games as a really-slow market maker.
//
// The strategy: if we have negative shares, try buying at current price - 50
// if we have positive shares, try selling at current price + 50
import (
"fmt"
"math/rand"
"sync"
"time"
"github.com/fohristiwhirl/gofighter" // go get -u github.com/fohristiwhirl/gofighter
)
// This is set up to run on a server clone on localhost...
const (
ACCOUNT = "BLSHBOTS"
VENUE = "TESTEX"
SYMBOL = "FOOBAR"
KEY = "blshkey"
BASE_URL = "http://127.0.0.1:8000/ob/api" // No trailing slashes please
)
var UnsafeQuote gofighter.Quote
var Quote_MUTEX sync.Mutex
// -----------------------------------------------------------------------------------------------
func order_cancel_report(info gofighter.TradingInfo, order gofighter.ShortOrder, move_chan chan gofighter.Movement) {
res, err := gofighter.Execute(info, order, nil)
if err != nil {
fmt.Println(err)
move_chan <- gofighter.Movement{} // For consistency, send message even on failure
return
}
time.Sleep(5 * time.Second)
id := res.Id
res, err = gofighter.Cancel(info, id)
if err != nil {
fmt.Println(err)
move_chan <- gofighter.Movement{} // For consistency, send message even on failure
return
}
move_chan <- gofighter.MoveFromOrder(res)
return
}
func quote_updater(info gofighter.TradingInfo) {
for {
localquote, err := gofighter.GetQuote(info) // this takes ages so can't lock before doing it
if err != nil {
time.Sleep(500 * time.Millisecond)
continue
}
Quote_MUTEX.Lock()
UnsafeQuote = localquote
Quote_MUTEX.Unlock()
time.Sleep(500 * time.Millisecond)
}
}
func main() {
info := gofighter.TradingInfo{
Account: ACCOUNT,
Venue: VENUE,
Symbol: SYMBOL,
ApiKey: KEY,
BaseURL: BASE_URL,
WebSocketURL: WS_URL,
}
go quote_updater(info)
pos := gofighter.Position{}
move_chan := make(chan gofighter.Movement)
for {
Quote_MUTEX.Lock()
localquote := UnsafeQuote
Quote_MUTEX.Unlock()
if localquote.Last == -1 {
fmt.Printf("Waiting for market action to start...\n")
time.Sleep(1 * time.Second)
continue
}
lastprice := localquote.Last
update_position:
for {
select {
case move := <- move_chan:
pos.UpdateFromMovement(move)
default:
break update_position
}
}
pos.Print(lastprice)
var order gofighter.ShortOrder
order.OrderType = "limit"
order.Qty = 50 + rand.Intn(50)
if pos.Shares > 0 || (pos.Shares == 0 && rand.Intn(2) == 0) {
order.Direction = "sell"
order.Price = lastprice + 50
} else {
order.Direction = "buy"
order.Price = lastprice - 50
}
if order.Price < 0 {
order.Price = 0
}
go order_cancel_report(info, order, move_chan)
time.Sleep(500 * time.Millisecond)
}
}