forked from jeffersonlicet/price-prediction-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbacktest.js
More file actions
381 lines (300 loc) · 9.28 KB
/
Copy pathbacktest.js
File metadata and controls
381 lines (300 loc) · 9.28 KB
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
const { chunk, compact, pick, set, sortBy } = require('lodash');
const Big = require('big.js');
const fs = require('fs');
const ora = require('ora');
const Table = require('cli-table');
const cliProgress = require('cli-progress');
const useContract = require('./contract');
const strategies = require('./constants/strategies');
const { error } = require('console');
const BSC_PROVIDER_URL =
process.env.BSC_PROVIDER_URL || 'https://bsc-dataseed.binance.org/';
const PKS_CONTRACT_ADDRESS =
process.env.PKS_CONTRACT_ADDRESS ||
'0x18b2a687610328590bc8f2e5fedde3b582a49cda';
const fee = 0.03;
const contract = useContract(BSC_PROVIDER_URL, PKS_CONTRACT_ADDRESS);
function getHistoricalData() {
try {
const configFile = fs.readFileSync('./data.json');
return JSON.parse(configFile);
} catch (ex) {
return null;
}
}
async function getRoundData(round, bar) {
try {
const data = await contract.methods.rounds(round).call();
const closePriceB = new Big(data.closePrice);
const lockPriceB = new Big(data.lockPrice);
const bullAmountB = new Big(data.bullAmount);
const bearAmountB = new Big(data.bearAmount);
const totalAmount = new Big(data.totalAmount);
const bullPayout = totalAmount.div(bullAmountB).round(3).toString();
const bearPayout = totalAmount.div(bearAmountB).round(3).toString();
bar.increment();
return {
...pick(data, [
'epoch',
'startTimestamp',
'lockTimestamp',
'closeTimestamp',
'lockPrice',
'closePrice',
'totalAmount',
'bullAmount',
'bearAmount',
'rewardBaseCalAmount',
'rewardAmount',
]),
winner: closePriceB.gt(lockPriceB) ? 'bull' : 'bear',
bullPayout,
bearPayout,
};
} catch (e) {
return null;
}
}
async function fetchHistoricalData(oldData, spinner) {
let data = oldData || [];
spinner.text = 'Loading historical data';
const currentRound = parseInt(await contract.methods.currentEpoch().call());
let startRound = 100;
if (data && data.length > 0) {
data = sortBy(data, (round) => parseInt(round.epoch));
startRound = data[data.length - 1].epoch;
console.log(
`\n\n 🔎 Last saved round ${startRound}, current round: ${currentRound}\n`,
);
}
if (currentRound - startRound < 10) {
spinner.succeed('Using cached data');
return data;
}
const endRound = currentRound - 1;
const rounds = [];
for (let i = startRound; i < endRound; i++) {
rounds.push(i);
}
console.log(` 🔎 About to fetch ${rounds.length} rounds\n`);
const chunks = chunk(rounds, 100);
const groups = chunk(chunks, 10);
const bar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
spinner.stop();
bar.start(rounds.length, 0);
await Promise.all(
groups.map(async (group) => {
for (const chunk of group) {
data.push(
...compact(
await Promise.all(chunk.map((item) => getRoundData(item, bar))),
),
);
}
}),
);
bar.stop();
spinner.succeed('Historical data saved');
fs.writeFileSync('./data.json', JSON.stringify(data), 'utf8');
return data;
}
async function runBacktest(settings, onDone) {
const spinner = ora('Running backtest').start();
let historicalData = getHistoricalData();
historicalData = await fetchHistoricalData(historicalData, spinner);
const betAmount = new Big(settings.amountPerTrade);
let capital = new Big(settings.capitalAmount);
let capitalB;
const bothDirections = settings.strategy === strategies.BOTH_DIRECTIONS;
if (new Big(settings.capitalAmount).lt(betAmount)) {
error('\nBet amount must be lower than capital amount');
return onDone();
}
// Both directions wallets
if (bothDirections) {
capital = new Big(settings.capitalAmount).div(2);
capitalB = new Big(settings.capitalAmount).div(2);
if (new Big(settings.capitalAmount).lt(betAmount * 2)) {
error('\nBet amount must be lower than capital amount for each wallet');
return onDone();
}
spinner.text = `Generating two wallets with ${capital.toString()} BNB each`;
}
let wins = 0;
let losses = 0;
for (const round of historicalData) {
if (!bothDirections) {
capital = capital.minus(betAmount);
if (capital.lt(0)) {
capital = new Big(0);
break;
}
} else {
if (capitalB.lt(betAmount.times(2)) || capital.lt(betAmount.times(2))) {
break;
}
}
const bullAmount = new Big(round.bullAmount);
const bearAmount = new Big(round.bearAmount);
const bullPayout = new Big(round.bullPayout);
const bearPayout = new Big(round.bearPayout);
let result = 0;
spinner.stop();
switch (true) {
case settings.strategy === strategies.BIGGER_PAYOUT: {
const bet = bullAmount.gt(bearAmount) ? 'bull' : 'bear';
const biggerPayout = bullAmount.gt(bearAmount)
? bullPayout
: bearPayout;
if (round.winner === bet) {
result = 1;
capital = capital
// Add payout
.plus(betAmount.times(biggerPayout).times(1 - fee));
}
break;
}
case settings.strategy === strategies.MINOR_PAYOUT: {
const bet = bullAmount.gt(bearAmount) ? 'bear' : 'bull';
const minorPayout = bullPayout.gt(bearPayout) ? bearPayout : bullPayout;
if (round.winner === bet) {
result = 1;
capital = capital
// Add payout
.plus(betAmount.times(minorPayout).times(1 - fee));
}
break;
}
case settings.strategy === strategies.ALWAYS_BEAR: {
const bet = 'bear';
if (round.winner === bet) {
result = 1;
capital = capital
// Add payout
.plus(betAmount.times(bearPayout).times(1 - fee));
}
break;
}
case settings.strategy === strategies.ALWAYS_BULL: {
const bet = 'bull';
if (round.winner === bet) {
result = 1;
capital = capital
// Add payout
.plus(betAmount.times(bullPayout).times(1 - fee));
}
break;
}
case settings.strategy === strategies.BOTH_DIRECTIONS: {
// A bets for bull
// B bets for bear
let bullBetMultip;
let bearBetMultip;
// Add weight to minor payout
if (settings.payoutToWeight === 'LESS_PAYOUT') {
bullBetMultip = bullPayout.lt(bearPayout)
? parseInt(settings.weightValue)
: 1;
bearBetMultip = bearPayout.lt(bullPayout)
? parseInt(settings.weightValue)
: 1;
} else {
// Add weight to greater payout
bullBetMultip = bullPayout.gt(bearPayout)
? parseInt(settings.weightValue)
: 1;
bearBetMultip = bearPayout.gt(bullPayout)
? parseInt(settings.weightValue)
: 1;
}
const walletABetAmount = betAmount.times(bullBetMultip);
const walletBBetAmount = betAmount.times(bearBetMultip);
capital = capital.minus(walletABetAmount);
capitalB = capitalB.minus(walletBBetAmount);
if (round.winner === 'bull') {
capital = capital
// Add payout
.plus(walletABetAmount.times(bullPayout).times(1 - fee));
}
if (round.winner === 'bear') {
capitalB = capitalB
// Add payout
.plus(walletBBetAmount.times(bearPayout).times(1 - fee));
}
}
}
if (result) {
wins++;
} else {
losses++;
}
spinner.frame();
}
spinner.succeed('Simulation done');
const startingAmount = new Big(settings.capitalAmount);
let table;
if (settings.strategy === strategies.BOTH_DIRECTIONS) {
const resultingCapital = capital.plus(capitalB);
const startingCapital = new Big(startingAmount);
const pnl = resultingCapital
.minus(startingCapital)
.abs()
.times(startingCapital.gt(resultingCapital) ? -1 : 1);
table = new Table({
style: {
head: [pnl.gt(0) ? 'green' : 'red'],
},
head: [
'Strategy',
'Initial invested per wallet',
'Total invested',
'Total rounds played',
'PNL (BNB)',
'PNL %',
'Sentiment',
],
});
table.push([
settings.strategy,
startingAmount.div(2).toString(),
startingCapital.toString(),
historicalData.length.toString(),
pnl.toString(),
pnl.times(100).div(startingAmount),
pnl.gt(0) ? '🤑' : '😢',
]);
} else {
const pnl = capital
.minus(startingAmount)
.abs()
.times(startingAmount.gt(capital) ? -1 : 1);
table = new Table({
style: {
head: [pnl.gt(0) ? 'green' : 'red'],
},
head: [
'Strategy',
'Total invested',
'Total rounds played',
'Wins',
'Losses',
'PNL (BNB)',
'PNL %',
'Sentiment',
],
});
table.push([
settings.strategy,
startingAmount.toString(),
historicalData.length.toString(),
wins.toString(),
losses.toString(),
pnl.toString(),
pnl.times(100).div(startingAmount),
pnl.gt(0) ? '🤑' : '😢',
]);
}
console.log(table.toString());
onDone();
}
module.exports = runBacktest;