-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
62 lines (58 loc) · 2.22 KB
/
main.js
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
'use strict';
const config = require('./config');
const KLine = require('./objects/k-line');
const moment = require('moment');
const round = n => Math.round(n * 10000) / 10000;
const isProvided = s => s !== undefined && s !== '';
function createOrders(gridQuant, currentPrice) {
const delta = (config.upperLimit - config.lowerLimit) / (gridQuant - 1);
const orders = [];
// sell order
for (let p = config.upperLimit; p >= currentPrice; p -= delta) {
orders.push({ price: round(p), type: 's', activate: true, init: true });
}
for (let p = config.lowerLimit; p <= currentPrice; p += delta) {
orders.push({ price: round(p), type: 'b', activate: true, init: false });
}
orders[orders.length - 1].activate = false;
orders.sort((a, b) => b.price - a.price);
return orders;
}
// get time from config
let start = undefined, end = undefined;
if (isProvided(config.start) && isProvided(config.end)) {
start = moment(config.start);
end = moment(config.end);
}
else if (isProvided(config.start && !isProvided(config.end))) {
start = moment(config.start);
end = moment(config.start).add(config.duration);
}
else if (isProvided(config.end && !isProvided(config.start))) {
end = moment(config.end);
start = moment(config.end).subtract(config.duration);
}
else {
end = moment();
start = moment().subtract(config.duration);
}
console.log('start:', start.toString());
console.log('end:', end.toString());
// get k-line
const kl = new KLine();
kl.init(start, end, config.pair, config.interval).then(() => {
let maxProfit = -Infinity, bestGridQuant = config.gridQuantRange[0];
for (let gridQuant = config.gridQuantRange[0]; gridQuant <= config.gridQuantRange[1]; gridQuant++) {
console.log('testing grid quantity =', gridQuant);
const orders = createOrders(gridQuant, kl.candles[0].open);
const profit = kl.backtest(orders);
if (profit > maxProfit) {
maxProfit = profit;
bestGridQuant = gridQuant;
}
}
const days = (end - start) / 86400000;
const annualReturn = round(maxProfit * 365 / days * 100);
console.log('Best grid quantity:', bestGridQuant);
console.log(`Estimated annual return: ${annualReturn}%`);
});