-
Notifications
You must be signed in to change notification settings - Fork 2
/
history_test.go
234 lines (196 loc) · 5.9 KB
/
history_test.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
package main
import (
"encoding/csv"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/require"
"github.com/vadiminshakov/marti/entity"
"github.com/vadiminshakov/marti/services"
"github.com/vadiminshakov/marti/services/anomalydetector"
"github.com/vadiminshakov/marti/services/windowfinder"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"log"
"os"
"testing"
"time"
)
const (
file = "data.csv" // file with data for test
btcBalanceInWallet = "1" // BTC balance in wallet
usdtBalanceInWallet = "0"
klinesize = "1h" // klinesize for test
rebalanceHours = 3
klinesframe uint = 110 // klines*klinesframe = hours before stats recount
minWindowUSDT = 128 // ok window due to binance commissions
)
var dataHoursAgo int
func TestProfit(t *testing.T) {
if testing.Short() {
t.Skip("skipping historical test in short mode.")
}
t.Run("1 year", func(t *testing.T) {
dataHoursAgo = 8760 // 1 year
require.NoError(t, botrun(zap.InfoLevel))
})
t.Run("2 years", func(t *testing.T) {
dataHoursAgo = 17520 // 2 years
require.NoError(t, botrun(zap.InfoLevel))
})
}
func botrun(loglvl zapcore.Level) error {
l, err := zap.NewProduction()
if err != nil {
return err
}
log := l.Sugar()
lvl := zap.NewAtomicLevel()
lvl.SetLevel(loglvl)
pair := &entity.Pair{
From: "BTC",
To: "USDT",
}
prices, klines, rmFn, err := prepareData(file, pair)
if err != nil {
return err
}
defer rmFn()
var lastaction entity.Action = entity.ActionBuy
balanceBTC, _ := decimal.NewFromString(btcBalanceInWallet)
balanceUSDT, _ := decimal.NewFromString(usdtBalanceInWallet)
trader, tsFactory := createTradeServiceFactory(l, pair, prices, balanceBTC, balanceUSDT)
kline := <-klines
ts, _ := tsFactory([]*entity.Kline{&kline}, lastaction)
if err != nil {
return err
}
var counter uint
var kl []*entity.Kline
for {
counter++
if len(prices) == 0 || len(klines) == 0 {
break
}
kline := <-klines
if len(kl) >= int(klinesframe) {
kl = kl[1:]
}
kl = append(kl, &kline)
if decimal.NewFromInt(int64(counter)).Equal(decimal.NewFromInt(rebalanceHours)) || ts == nil {
counter = 0
// recreate trade service for 'klinesframe' day
ts, err = tsFactory(kl, lastaction)
if err != nil {
log.Debug("skip kline because insufficient volatility")
continue
}
}
te, err := ts.Trade()
if err != nil {
return err
}
if te == nil {
<-prices // skip price that not readed by trader
continue
}
if te.Action != entity.ActionNull {
lastaction = te.Action
}
log.Debug(te.String())
}
log.Infof("Deals: %d", trader.dealsCount)
log.Infof("Total balance of %s is %s (was %s)", pair.From, trader.balance1.String(), balanceBTC.String())
log.Infof("Total balance of %s is %s (was %s)", pair.To, trader.balance2.String(), trader.firstbalance2)
log.Infof("Total fee is %s", trader.fee.String())
var total decimal.Decimal
if trader.balance1.GreaterThan(decimal.NewFromInt(0)) {
total = trader.balance2.Sub(trader.fee)
} else {
total = trader.balance2.Sub(trader.firstbalance2).Sub(trader.fee)
}
log.Infof("Total profit is %s %s", total.String(), pair.To)
return nil
}
func prepareData(file string, pair *entity.Pair) (prices chan decimal.Decimal, klines chan entity.Kline, removeFile func(), _ error) {
collect, err := dataColletorFactory(file, pair)
if err != nil {
return nil, nil, nil, err
}
removeFile = func() {
os.Remove(file)
}
intervalHours := 100
for collectFromHours := dataHoursAgo; collectFromHours > 0; collectFromHours -= intervalHours {
if err = collect(collectFromHours, intervalHours, klinesize); err != nil {
return nil, nil, nil, err
}
}
prices, klines = makePriceChFromCsv(file)
return
}
func createTradeServiceFactory(logger *zap.Logger, pair *entity.Pair, prices chan decimal.Decimal,
balanceBTC, balanceUSDT decimal.Decimal) (
*traderCsv, func(klines []*entity.Kline, lastaction entity.Action) (*services.TradeService, error)) {
pricer := &pricerCsv{
pricesCh: prices,
}
trader := &traderCsv{
pair: pair,
balance1: balanceBTC,
balance2: balanceUSDT,
pricesCh: prices,
}
anomdetector := anomalydetector.NewAnomalyDetector(*pair, 30, decimal.NewFromInt(10))
return trader, func(klines []*entity.Kline, lastaction entity.Action) (*services.TradeService, error) {
buyprice, window, err := windowfinder.CalcBuyPriceAndWindow(klines, decimal.NewFromInt(minWindowUSDT))
if err != nil {
return nil, err
}
return services.NewTradeService(logger, *pair, balanceBTC, pricer, &detectorCsv{
lastaction: lastaction,
buypoint: buyprice,
window: window,
},
trader, anomdetector), nil
}
}
func makePriceChFromCsv(filePath string) (chan decimal.Decimal, chan entity.Kline) {
prices := make(chan decimal.Decimal, 1000)
var klines chan entity.Kline
go func() {
pricescsv, kchan := readCsv(filePath)
klines = kchan
for _, price := range pricescsv {
prices <- price // for pricer
prices <- price // for trader
}
}()
time.Sleep(300 * time.Millisecond)
return prices, klines
}
func readCsv(filePath string) ([]decimal.Decimal, chan entity.Kline) {
f, err := os.Open(filePath)
if err != nil {
log.Fatal("Unable to read input file "+filePath, err)
}
defer f.Close()
csvReader := csv.NewReader(f)
records, err := csvReader.ReadAll()
if err != nil {
log.Fatal("Unable to parse file as CSV for "+filePath, err)
}
prices := make([]decimal.Decimal, 0, len(records))
klines := make(chan entity.Kline, len(records))
for _, record := range records {
priceOpen, _ := decimal.NewFromString(record[0])
priceHigh, _ := decimal.NewFromString(record[1])
priceLow, _ := decimal.NewFromString(record[2])
priceClose, _ := decimal.NewFromString(record[3])
price := priceHigh.Add(priceLow).Div(decimal.NewFromInt(2))
prices = append(prices, price)
klines <- entity.Kline{
Open: priceOpen,
Close: priceClose,
}
}
return prices, klines
}