-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathexec.js
More file actions
77 lines (60 loc) · 1.94 KB
/
Copy pathexec.js
File metadata and controls
77 lines (60 loc) · 1.94 KB
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
'use strict'
process.env.DEBUG = '*'
const { RESTv2 } = require('bfx-api-node-rest')
const debug = require('debug')('bfx:hf:strategy-exec:example:exec')
const { SYMBOLS, TIME_FRAMES } = require('bfx-hf-util')
const { Manager } = require('bfx-api-node-core')
const WDPlugin = require('bfx-api-node-plugin-wd')
const EMACrossStrategy = require('./ema_cross_strategy')
const LiveStrategyExecution = require('../')
const API_KEY = '...'
const API_SECRET = '...'
const rest = new RESTv2({ url: 'https://api.bitfinex.com', transform: true })
const ws2Manager = new Manager({
apiKey: API_KEY,
apiSecret: API_SECRET,
transform: true,
plugins: [WDPlugin({
autoReconnect: true, // if false, the connection will only be closed
reconnectDelay: 5000, // wait 5 seconds before reconnecting
packetWDDelay: 10000 // set the watch-dog to a 10s delay
})]
})
const run = () => {
const strategy = EMACrossStrategy({
symbol: SYMBOLS.EOS_USD,
tf: TIME_FRAMES.ONE_DAY,
amount: 1,
margin: true
})
ws2Manager.onWS('open', {}, (state = {}) => debug('connected to ws2 API'))
ws2Manager.onceWS('event:auth:success', {}, async (authEvent, ws) => {
debug('authenticated')
debug('executing strategy...')
strategy.ws = ws
const strategyOptions = {
symbol: SYMBOLS.EOS_USD,
timeframe: TIME_FRAMES.ONE_DAY,
trades: true,
candleSeed: 5000
}
const liveExecutor = new LiveStrategyExecution({ strategy, ws2Manager, rest, strategyOptions })
liveExecutor.on('error', (err) => {
// handle errors
console.error(err)
})
await liveExecutor.execute()
process.on('SIGINT', async () => {
await liveExecutor.stopExecution() // close open positions if any
console.log(liveExecutor.generateResults()) // fetch strategy execution results
process.exit(0)
})
})
debug('opening socket...')
ws2Manager.openWS()
}
try {
run()
} catch (err) {
debug('error: %s', err.stack)
}