-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
252 lines (227 loc) · 8 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/byronhallett/goanda"
"github.com/joho/godotenv"
)
// Some constants for OANDA orders
const (
market string = "MARKET"
fireOrKill string = "FOK"
immediateOrCancelled string = "IOC"
defaultFill string = "DEFAULT"
)
type botDatum struct {
Name string `json:"name"`
Account string `json:"account"`
Params botParams `json:"params"`
}
type botData []botDatum
type botParams struct {
CandleGranularity string `json:"candleGranularity"`
CandleCount string `json:"candleCount"`
MomentumPeriod int `json:"momentumPeriod"`
SMAPeriod int `json:"SMAPeriod"`
RsiPeriod int `json:"rsiPeriod"`
StDevPeriod int `json:"stDevPeriod"`
VolumeFactor float64 `json:"volumeFactor"`
TakeProfitFactor float64 `json:"takeProfitFactor"`
StopLossFactor float64 `json:"stopLossFactor"`
}
func startOandaConnection(accountID string) (*goanda.OandaConnection, string) {
// Load the env
if godotenv.Load() != nil { // err
log.Fatal("No api keys")
}
key := os.Getenv("OANDA_API_KEY")
oanda := goanda.NewConnection(accountID, key, false)
return oanda, accountID
}
func getCurrencies(connection *goanda.OandaConnection, accountID string) *goanda.AccountInstruments {
ret := goanda.AccountInstruments{}
instruments := connection.GetAccountInstruments(accountID)
for _, inst := range instruments.Instruments {
if inst.Type == "CURRENCY" {
ret.Instruments = append(ret.Instruments, inst)
}
}
return &ret
}
func getInstrumentsWithoutPositions(connection *goanda.OandaConnection, fromInstruments *goanda.AccountInstruments) *goanda.AccountInstruments {
ret := goanda.AccountInstruments{}
// O(n) to store the open trades in a map
keys := map[string]bool{}
for _, t := range connection.GetOpenTrades().Trades {
keys[t.Instrument] = true
}
// O(n) to check which instruments are open
for _, instrument := range fromInstruments.Instruments {
if !keys[instrument.Name] {
ret.Instruments = append(ret.Instruments, instrument)
}
}
return &ret
}
func averagePointer(candles *[]goanda.Candle) (result float64) {
for _, c := range *candles {
result += c.Mid.Close
}
result /= math.Max(float64(len(*candles)), 1)
return
}
func average(candles []goanda.Candle) (result float64) {
result = averagePointer(&candles)
return
}
func computeMomentum(candles *goanda.BidAskCandles, period int) float64 {
endIndex := len(candles.Candles) - 1
var halfPeriod int
if period > endIndex {
halfPeriod = endIndex / 2
} else {
halfPeriod = period / 2
}
firstHalf := candles.Candles[endIndex-2*halfPeriod : endIndex-halfPeriod]
lastHalf := candles.Candles[endIndex-halfPeriod : endIndex]
return averagePointer(&lastHalf) - averagePointer(&firstHalf)
}
func computeRSI(candles *goanda.BidAskCandles, period int) float64 {
return 1
}
func computeStandardDeviation(candles *goanda.BidAskCandles, period int) float64 {
return 2.0
}
func toFixed(num float64, precision int) float64 {
output := math.Pow(10, float64(precision))
return float64(int(num*output)) / output
}
func placeOrder(connection *goanda.OandaConnection, instrument *goanda.Instrument, units int, takeProfit float64, stopLoss float64) *goanda.OrderResponse {
// Use precision of instrument to constuct tp and sl string
prec := instrument.DisplayPrecision
tp := toFixed(takeProfit, prec)
sl := toFixed(stopLoss, prec)
response := connection.CreateOrder(goanda.OrderPayload{Order: goanda.OrderBody{
Instrument: instrument.Name,
Type: market,
Units: units,
TimeInForce: fireOrKill,
PositionFill: defaultFill,
TakeProfitOnFill: &goanda.OnFill{Price: fmt.Sprintf("%f", tp)},
StopLossOnFill: &goanda.OnFill{Price: fmt.Sprintf("%f", sl)},
}})
fmt.Println("===============================================================")
fmt.Println(fmt.Sprintf("inst: %s Units: %d price: %s TP: %f SL: %f",
instrument.Name, units, response.OrderFillTransaction.Price, tp, sl))
fmt.Println("===============================================================")
return &response
}
func goLong(connection *goanda.OandaConnection, instrument *goanda.Instrument, lastCandle *goanda.Candle, params *botParams) {
tradeVol := int((1 / lastCandle.Bid.Close) * params.VolumeFactor)
if tradeVol <= 0 {
return
}
tp := lastCandle.Ask.Close + lastCandle.Ask.Close*params.TakeProfitFactor
sl := lastCandle.Bid.Close - lastCandle.Bid.Close*params.StopLossFactor
placeOrder(connection, instrument, tradeVol, tp, sl)
}
func goShort(connection *goanda.OandaConnection, instrument *goanda.Instrument, lastCandle *goanda.Candle, params *botParams) {
tradeVol := int((1 / lastCandle.Ask.Close) * params.VolumeFactor)
if tradeVol <= 0 {
return
}
tp := lastCandle.Bid.Close - lastCandle.Bid.Close*params.TakeProfitFactor
sl := lastCandle.Ask.Close + lastCandle.Ask.Close*params.StopLossFactor
placeOrder(connection, instrument, -tradeVol, tp, sl)
}
func analyseAndTrade(connection *goanda.OandaConnection, instrument goanda.Instrument, params *botParams, wg *sync.WaitGroup) {
defer wg.Done()
candles := connection.GetBidAskCandles(instrument.Name, params.CandleCount, params.CandleGranularity)
lastIndex := len(candles.Candles) - 1
// Assess the candles for statistics
momentum := computeMomentum(&candles, params.MomentumPeriod)
// rsi := computeRSI(&candles, params.rsiPeriod)
simpleAverage := average(candles.Candles[lastIndex-params.SMAPeriod:])
lastCandle := candles.Candles[lastIndex]
// Use std dev to find the bolinger band, we can then know how far away from
// the mean we should be, and how far to set to TP
// stDev := computeStandardDeviation(&candles, params.stDevPeriod)
// If score is high, enter following block
if simpleAverage > lastCandle.Ask.Close && momentum > 0 {
goLong(connection, &instrument, &lastCandle, params)
} else if simpleAverage < lastCandle.Bid.Close && momentum < 0 {
goShort(connection, &instrument, &lastCandle, params)
}
}
func stripParse(s string, p string) int64 {
i, err := strconv.ParseInt(strings.Replace(s, p, "", 1), 0, 0)
if err != nil {
log.Fatal("Bad granularity format")
}
return i
}
func granularityToDuration(granularity string) time.Duration {
if strings.HasPrefix(granularity, "S") {
return time.Second * time.Duration(stripParse(granularity, "S"))
}
if strings.HasPrefix(granularity, "M") {
return time.Minute * time.Duration(stripParse(granularity, "M"))
}
if strings.HasPrefix(granularity, "H") {
return time.Hour * time.Duration(stripParse(granularity, "H"))
}
if strings.HasPrefix(granularity, "D") {
return time.Hour * time.Duration(24) * time.Duration(stripParse(granularity, "D"))
}
return time.Minute
}
// runBot can be run on its own thread to allow testing bots simultaneously
func runBot(botDatum botDatum, wg *sync.WaitGroup) {
defer wg.Done()
connection, accountID := startOandaConnection(botDatum.Account)
instruments := getCurrencies(connection, accountID)
params := &botDatum.Params
for {
noPos := getInstrumentsWithoutPositions(connection, instruments)
fmt.Println("Running", botDatum.Name, "on", len(noPos.Instruments), "instruments")
var wg sync.WaitGroup
wg.Add(len(noPos.Instruments))
for _, instrument := range noPos.Instruments {
// Get the candles of interest
go analyseAndTrade(connection, instrument, params, &wg)
}
wg.Wait()
newNoPos := getInstrumentsWithoutPositions(connection, instruments)
fmt.Println("Placed", len(noPos.Instruments)-len(newNoPos.Instruments), "trades")
fmt.Println("Waiting for next candle")
time.Sleep(granularityToDuration(params.CandleGranularity))
}
}
func loadBots() (bots botData) {
fmt.Println("Loading bots")
file, err := os.Open("bots.json")
if err != nil {
log.Fatal("Missing bots.json")
}
defer file.Close()
byteValue, _ := ioutil.ReadAll(file)
json.Unmarshal(byteValue, &bots)
return
}
func main() {
bots := loadBots()
var wg sync.WaitGroup
for _, bot := range bots {
wg.Add(1)
go runBot(bot, &wg)
}
wg.Wait()
}