-
Notifications
You must be signed in to change notification settings - Fork 35
/
backtest.js
executable file
·75 lines (64 loc) · 2.32 KB
/
backtest.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
#! /usr/local/bin/node
var series = require('run-series');
var n = require('numbro');
var rsa = require('./rsa.js');
var dates = require('./isodate.js');
var ta = require('./technical.js');
var sentiment = require('./sentiment.js');
var markets = require('./markets.js');
var quotes = require('./quotes.js');
var strategy = require('./strategy.js');
var actions = require('./actions.js');
var account = require('./account.js');
var orders = require('./orders.js');
var positions = require('./positions.js');
var earnings = require('./earnings.js');
var conf = require('./conf.js');
var opt = require('node-getopt').create([
['s' , 'strategy=ARG' , 'macd, sar, k39, slow_stoch (default k39)'],
['h' , 'help' , 'display this help'],
]).bindHelp().parseSystem();
global.backtest = true;
conf.trade = opt.options.trade;
conf.strategy = (opt.options.strategy) ? opt.options.strategy : 'default';
var clist = conf.list;
console.log("*** BACKTEST today: "+
dates.today+" "+(new Date).toLocaleTimeString());
global.quandl = new require('quandl')({ auth_token: conf.quandl_token });
// download backtest data
var downloads = [
function(cb) {
quotes.download(clist, cb);
}, function(cb) {
markets.download(cb);
}, function(cb) {
earnings.download(clist, cb);
}
];
series(downloads, function(err, results)
{
// run the backtest simulation
account.cash = conf.backtest.cash;
for (let i = 252; i > 0; i--) {
global.backtest_offset = i-1;
markets._hours = [ { next_open_date: quotes.get(clist[0])[0].date } ];
global.backtest_offset = i;
dates._today_date = quotes.get(clist[0])[0].date;
console.log("**Day:"+i+" "+dates.today);
if (conf.market_breadth) {
markets.analyse();
}
orders.triggerStops();
rsa.calculate(clist);
strategy.run(clist.sort(rsa.sort));
actions.align();
actions.distribute_cash();
actions.allocate_stops();
orders.backtest(actions.sell, actions.buy, actions.stop);
actions.clear();
}
// output the simulation results
let final_cash = (account.cash + positions.value);
console.log("Final cash: " + final_cash);
console.log("Last year profit: " + (final_cash / conf.backtest.cash - 1) * 100 + " %");
});