forked from JeffreyHyer/grandmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignals.js
171 lines (134 loc) · 4.71 KB
/
signals.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const cliParams = [
{ name: 'strategy', alias: 's', type: String, defaultOption: true }
]
const params = require('command-line-args')(cliParams)
if (undefined === params.strategy) {
console.log('[!] ERROR: You must specify a strategy')
process.exit()
}
require('dotenv').config()
const config = require(`./strategies/${params.strategy}/config`)
const Strategy = require(`./strategies/${params.strategy}/strategy`)
const strategy = new Strategy(Object.keys(config.symbols))
const Redis = require('redis')
const redisBars = Redis.createClient({
host: (process.env.REDIS_HOST || '127.0.0.1'),
port: (process.env.REDIS_PORT || 6379),
password: (process.env.REDIS_PASS || null),
db: (process.env.REDIS_DB || 0)
})
const redisSignals = redisBars.duplicate()
const mysql = require('mysql')
const db = mysql.createConnection({
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASS,
database: process.env.MYSQL_DB,
multipleStatements: true
})
const nodemailer = require('nodemailer')
const smtp = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
secure: process.env.SMTP_SECURE,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS
}
})
console.log('============================================================')
console.log((new Date()).toISOString())
console.log('')
let queries = []
let historyLoaded = false
Object.keys(config.symbols).forEach((symbol) => {
queries.push(`SELECT *, '${symbol}' AS symbol FROM \`${symbol.toLowerCase()}\` ORDER BY \`start\` DESC LIMIT ${config.historicBars}`)
})
db.query(queries.join(';'), (error, results) => {
if (error) {
console.log(`[!] MySQL ERROR: ${error.message}`)
redisBars.unsubscribe()
redisBars.quit()
redisSignals.quit()
db.end(() => { process.exit(3) })
return
}
if (queries.length > 1) {
results.forEach((result) => {
let bars = []
result.forEach((bar) => {
bars.unshift(bar)
})
strategy.addHistory(bars[0].symbol, bars)
})
} else {
let bars = []
results.forEach((bar) => {
bars.unshift(bar)
})
strategy.addHistory(bars[0].symbol, bars)
}
db.end(() => {
console.log(`[+] History Loaded`)
historyLoaded = true
})
})
redisSignals.on('connect', () => {
console.log(`[+] [Redis:Signals] Connected`)
})
redisSignals.on('error', (error) => {
console.log(`[!] [Redis:Signals] Error: ${error.message}`)
})
redisSignals.on('end', () => {
console.log(`[-] [Redis:Signals] Disconnected`)
})
redisBars.on('connect', () => {
console.log(`[+] [Redis:Bars] Connected`)
})
redisBars.on('error', (error) => {
console.log(`[!] [Redis:Bars] Error: ${error.message}`)
})
redisBars.on('end', () => {
console.log(`[-] [Redis:Bars] Disconnected`)
})
redisBars.on('message', (symbol, message) => {
if (historyLoaded === true) {
let bar = JSON.parse(message)
if (bar.symbol && strategy.symbols[bar.symbol.toUpperCase()]) {
strategy.addBar(bar.symbol.toUpperCase(), bar, onSignal)
}
}
})
redisBars.subscribe(Object.keys(config.symbols).join(' '))
process.on('SIGINT', () => {
redisBars.unsubscribe()
redisBars.quit()
redisSignals.quit()
console.log('[-] Exiting')
process.exit(0)
})
function onSignal(signal) {
if (signal.buy) {
let date = (new Date(signal.bar.start)).toISOString()
redisSignals.publish(`${params.strategy}_${signal.symbol}`, JSON.stringify({ strategy: params.strategy, symbol: signal.symbol, action: 'BUY', price: signal.bar.close }))
console.log(`[BUY]\t${params.strategy}\t${signal.symbol}\t${date}\t$${signal.bar.close.toFixed(2)}`)
email(`[BUY] | ${params.strategy} | ${signal.symbol} | ${signal.bar.close.toFixed(2)}`)
} else if (signal.sell) {
let date = (new Date(signal.bar.start)).toISOString()
redisSignals.publish(`${params.strategy}_${signal.symbol}`, JSON.stringify({ strategy: params.strategy, symbol: signal.symbol, action: 'SELL', price: signal.bar.close }))
console.log(`[SELL]\t${params.strategy}\t${signal.symbol}\t${date}\t$${signal.bar.close.toFixed(2)}`)
email(`[SELL] | ${params.strategy} | ${signal.symbol} | ${signal.bar.close.toFixed(2)}`)
}
}
function email(message) {
smtp.sendMail({
from: process.env.SMTP_FROM,
to: process.env.SMTP_TO,
subject: 'Trading Signal',
text: `${message}`
}, (error, info) => {
if (error) {
console.log(`[!] SMTP ERROR: ${error}`)
}
})
}