-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
80 lines (70 loc) · 1.92 KB
/
index.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
80
const path = require('path')
const fs = require('fs');
const _ = require('highland')
const { createTrader } = require('devalpha')
/* Setup some constants */
const FILENAME = 'MSFT.csv'
const START_DATE = new Date('2012-12-01').getTime()
const END_DATE = new Date('2017-12-30').getTime()
/* Create a new stream and point it to the data */
const fileStream = fs.createReadStream(path.resolve(FILENAME))
/* Parse the stream */
const msftStream = _(fileStream)
.split()
.map((item) => {
const parsed = item.split(',')
return {
identifier: parsed[0],
timestamp: new Date(parsed[1]).getTime(),
open: parseFloat(parsed[2]),
high: parseFloat(parsed[3]),
low: parseFloat(parsed[4]),
close: parseFloat(parsed[5])
}
})
.filter((item) => item.timestamp >= START_DATE && item.timestamp <= END_DATE)
/* Denotes whether or not we've entered a position. */
let hasPosition = false
/* Initialize the stream! */
createTrader({
startCapital: 100000,
feeds: {
msftStream
},
dashboard: {
active: true
}
}, (context, action) => {
if (action.type === 'msftStream') {
/* Hmmm... */
const magicNumber = Math.random()
/* Enter a long position */
if (!hasPosition && magicNumber > 0.5) {
context.order({
identifier: 'MSFT',
price: action.payload.close,
quantity: 1000
})
hasPosition = true
}
/* Exit the long position */
if (hasPosition && magicNumber <= 0.5) {
context.order({
identifier: 'MSFT',
price: action.payload.close,
quantity: -1000
})
hasPosition = false
}
}
})
.errors((err) => {
/* Extract errors from the stream */
console.log('Whoops!', err.message)
})
.each(({ state, action }) => {
/* Consume the stream */
console.log('Received action:', action.type)
})
/* Open devalpha.io in your browser to start the backtest */
console.log('Waiting for DevAlpha...')