forked from azidyn/larptrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_live_minimal.js
72 lines (41 loc) · 1.88 KB
/
example_live_minimal.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
/*
example-live-minimal.js
Pulls the last 10 bars of historical data from BitMEX and then sits waiting for new bars
Prints them out to the console.
*/
const fs = require('fs');
const LiveFeed = require('./src/feed/Live');
// Settings for your backtest/trading
const RESOLUTION = '1m'; // '1m', '5m', '1h', '1d'
const RUN_LIVE = true; // enable live feed or not (system waits for each new bar)
const HISTORICAL_BARS = 10; // how many bars to download before running live/backtest (max 1000)
const MAX_HISTORICAL_BARS = 1000;
const feed = new LiveFeed();
let series = [];
console.log(`Pulling the last ${HISTORICAL_BARS} bars and then waiting for new data. Press CTRL+C to terminate.`);
function onclose( bar )
{
/*
*
* Your bags-to-riches strategy code goes here
*
*/
console.log( `${ bar.live ? 'LIVE => ' :'' }${bar.opentimestamp} open=${bar.open} high=${bar.high} low=${bar.low} close=${bar.close}`);
}
// Required system bootup boilerplate code
(async()=>{
feed.on('live', () => console.log('* Running live. Waiting for the current bar to close.') );
feed.on('bar', b => {
series.push( b );
// Limit memory usage
series = series.slice( -MAX_HISTORICAL_BARS );
// Write incoming data to disk if you wish
//fs.writeFileSync( './bars.json', JSON.stringify( series ) );
// Call the user strategy code
onclose( b );
} );
// `resolution`: (optional, default='5m' ) bar length; '1m', '5m', '1h', '1d'
// `warmup`: (optional) request N bars of historical data to get our system started or just backtest
// `offline`: (optional) just terminate after sending the historical bars ( no live processing )
await feed.start({ resolution: RESOLUTION, warmup: HISTORICAL_BARS, offline: !RUN_LIVE });
})();