-
Notifications
You must be signed in to change notification settings - Fork 4
/
RESTAPI.js
79 lines (66 loc) · 1.81 KB
/
RESTAPI.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* Author: Marius Helf
*
* RESTAPI strategy to be used in combination with GekkoPy:
* https://github.com/mariushelf/gekkopy
*/
const request = require('sync-request');
const log = require('../core/log.js');
// Let's create our own strategy
const strategy = {};
const makeAdviceLog = (type, price) => {
log.debug(`${type} at ${price}`);
console.log(`${type} at ${price}`);
};
// Prepare everything our strategy needs
strategy.init = function() {
log.debug('settings', this.settings);
const {
url,
} = this.settings;
this.url = url;
// init window size
var res = request('GET', url + '/window_size');
this.windowSize = JSON.parse(res.body)['window_size'];
this.requiredHistory = this.windowSize;
this.data = [];
this.currentAdvice = null;
this.count = 0;
};
// What happens on every new candle?
strategy.update = function(candle) {
this.count += 1;
this.data.push([
candle.open, candle.high, candle.low,
candle.close, candle.volume, candle.trades,
]);
if (this.data.length > this.windowSize) {
this.data.shift();
}
console.log(this.count, this.data.length);
if (this.data.length >= this.windowSize) {
res = request('POST', this.url + '/advice', {
headers: { 'content-type': 'application/json' },
body: JSON.stringify(this.data),
});
console.log(JSON.parse(res.body));
this.currentAdvice = JSON.parse(res.body)['advice'];
}
};
// For debugging purposes.
strategy.log = function() {
};
// Based on the newly calculated
// information, check if we should
// update or not.
strategy.check = function(candle) {
this.advice(this.currentAdvice);
};
// Optional for executing code
// after completion of a backtest.
// This block will not execute in
// live use as a live gekko is
// never ending.
strategy.end = function() {
};
module.exports = strategy;