/
buy_low_sell_high_bot.go
100 lines (78 loc) · 3.11 KB
/
buy_low_sell_high_bot.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
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"
"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
WS_URL = "ws://127.0.0.1:8000/ob/api/ws"
)
// -----------------------------------------------------------------------------------------------
func order_and_cancel(info gofighter.TradingInfo, order gofighter.ShortOrder) {
res, err := gofighter.Execute(info, order, nil)
if err != nil {
fmt.Println(err)
return
}
time.Sleep(5 * time.Second)
id := res.Id
gofighter.Cancel(info, id)
}
func main() {
info := gofighter.TradingInfo{
Account: ACCOUNT,
Venue: VENUE,
Symbol: SYMBOL,
ApiKey: KEY,
BaseURL: BASE_URL,
WebSocketURL: WS_URL,
}
// The market and position will be watched by 2 goroutines. In order to get info
// back from them, we create 2 channels that we can use to request the current
// state from them. We request the state by sending... a channel, of course.
market_queries := make(chan chan gofighter.Market)
go gofighter.MarketWatch(info, market_queries)
position_queries := make(chan chan gofighter.Position)
go gofighter.PositionWatch(info, position_queries)
for {
market := gofighter.GetMarket(market_queries) // Behind the scenes, this sends a channel
// and gets the response through it.
// The .Last member of the quote gets set to -1 if not present in the JSON from the server,
// which usually means there is no activity yet...
if market.Quote.Last == -1 {
fmt.Printf("Waiting for market action to start...\n")
time.Sleep(1 * time.Second)
continue
}
pos := gofighter.GetPosition(position_queries) // This works like .GetMarket() above, but for position
pos.Print(market.Quote.Last) // The argument here is the price to use when calculating NAV
var order gofighter.ShortOrder
order.OrderType = "limit"
order.Qty = 50 + rand.Intn(50)
// Buy if short. Sell if long. If neither, flip a coin...
if pos.Shares > 0 || (pos.Shares == 0 && rand.Intn(2) == 0) {
order.Direction = "sell"
order.Price = market.Quote.Last + 50
} else {
order.Direction = "buy"
order.Price = market.Quote.Last - 50
}
if order.Price < 0 {
order.Price = 0
}
go order_and_cancel(info, order)
time.Sleep(500 * time.Millisecond)
}
}